2011-08-01

Export Notes from Ipod Touch or Iphone / Ipad

Here's how I exported the notes from the Notes app on my Ipod Touch.  Unfortunately this time you'll have to be a pro to follow along.

First, I needed to find the file where the information is stored.  The notes are kept in a single sqlite3 database file.

While my Linux box can mount the ipod as a filesystem, it seemed to be mounted to a subfolder on the ipod where I couldn't get access to other folders above.  After googling for a half hour for a solution I gave up and plugged it into my Windows box instead.

In iTunes I made a backup of the ipod, right-clicking it in the list on the left and selecting "backup" from the context menu.  You'll probably want to disable encryption if you are using it.  Next I found the backup files here with Explorer:
C:\Documents and Settings\USERNAME
    ...\Application Data\Apple Computer\MobileSync\Backup
You will find several folders here, choose the one with the latest Date Modified set to a few minutes ago.  Inside there will be hundreds of .mdddata files inside.  There are a few ways to figure out which file is the one we are looking for.  At first I poked around with the find command:
find "SQLite format 3" *.mddata
This was helpful, but then I realized I had entered a few notes recently.  So I sorted the files in Explorer, again by Date Modified.  I looked at the most recent files with Notepad++ (which will open them without issue, even though they have many nonprinting characters inside.)

On the third file, I found the "SQLite format 3" header, and after scrolling down the text of my Notes.  Now we're getting somewhere.  I copied off the file and renamed it to notes.db.

Here is a python script I found online purporting to export the data, but it didn't work.  Maybe it is for a different version?  This is a first generation ipod touch with iOS v3.x on it.

I then decided to copy the file back to the Linux box and investigate it there, as it is so easy to get sqlite installed. Installing sqlite and running the script could probably be done on Windows without too much trouble. To install:
sudo apt-get install sqlite3
sudo apt-get install python-pysqlite2
After investigating the db file in sqlite I was able to fix and improve the script and have included it below.  Save it as export_notes.py and give it your notes.db file as an argument.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'''
    Extract notes from an iphone or ipod touch.
'''
import sys, os
import sqlite3
invalid_chars = r'<>:"/\|?*'
html = u'''<html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
%s
</body></html>
'''
outdir = 'out'

def valid_fname(fname):
    return ''.join(c for c in fname if c not in invalid_chars)

# make dir
if not os.path.exists(outdir):
    os.makedirs(outdir)

c = sqlite3.connect( sys.argv[1]
                     if len(sys.argv) > 1
                     else sys.exit('Filename param required.') )

titles = dict(c.execute('select rowid, title  from Note order by rowid;'))
bodies = dict(c.execute('select note_id, data from note_bodies order by rowid;'))


for note_id, data in bodies.items():
    title = titles.get(note_id, 'id_%s' % note_id)
    print 'exporting:', title
    fname = os.path.join(outdir, valid_fname('%s.html' % title))

    with open(fname, 'wb') as outf:
        outf.write( (html % data).encode('utf-8') )

c.close()

Upon execution, it will create an out folder, which will contain an html file for each note.  I never used formatting in any of my notes (not even sure how) but unfortunately they are stored as html.  I would prefer plain text, but not going to bother to write a scraper.  Hope this helps someone out there.

5 comments:

  1. How do you do the last part?
    "give it the your notes.db file as an argument."

    ReplyDelete
  2. Run at a terminal without the > char:

    > export_notes.py notes.db

    ReplyDelete
  3. Man, I really wish someone would make this into an app or something. Could really do with exporting all my iPhone notes somehow.

    If you want those HTML files converted to .txt, I would highly recommend this piece of software: http://www.softinterface.com/Convert-Doc/Convert-Doc.htm

    I used it to convert my HTML notes to .txt once I'd exported notes from Evernote as HTML. Did the job nicely - saved me loads of time.

    ReplyDelete
  4. If you don't mind doing it by hand (not in an automated "batch") fashion you can use Firefox to save each file as plain text.

    Another choice is Open/LibreOffice. It can be automated by command line, though it is a bit complex to get started. I used to use an OO script to build my resume, converting it to .pdf etc. Worked well.

    ReplyDelete
  5. Your script has an error with how it deals with large titles. People using the iPod touch, who are lazy (like me), sometimes don't put titles on their notes.. This causes the iPod touch to take as much of the body of the note as it can, and assign it that title. So you might get a title as long as "Today I need to go to the store and pick up some milk and frito's and after that I will..."

    Translate that into a DB file, and run it through your script will cause an error, where it won't be able to find the large titled html file to write to.

    titles don't matter too much to me, so I edited the code to have the title as a succession of numbers instead.

    fixed: http://codepad.org/OOI62a7H

    ReplyDelete