Monday, October 17, 2011

Exporting RedNotebook notes to Evernote using Applescript

I've recently switched my work environment from Ubuntu to Mac OS. While I was using Ubuntu, I used RedNotebook for taking notes about the various things I was working on, code snippets, to do lists, etc. I am now using Evernote, which has some nice features and better userability (in my opinion). Sadly, there is no easy way to import my notes from RedNotebook.

What I've done to accomplish this is to use Mac's Applescript and I pieced together a hacky, but workable, way to do just this.  It's not entirely pretty, and I'm sure it could be done better, but it does work with one caveat and some instructions (it's unfortunately not an entirely automatic procedure).  So here it is for anyone else who wants an already working way to do the same thing.

First off, the caveat:  The very first note imported into Evernote ends up being an HTML attachment, instead of the text.  And the formatting gets a little bit screwy in the imported files, but in my opinion, that's acceptable since what I want is to have this information for future reference.


  1. Create a new directory, this will be your input directory, and export the notebooks from RedNotebook you want imported as HTML.  This will create one large XML file for all the notes for each notebook you want imported.
  2. Create a new directory, this will be your output directory, and save this file to it.  This file is the top portion of the xml file that will make up each of the individual notes in the evernote notes.
  3. Download the applescript or copy and paste from below to your computer.
  4. Run the script, you will be prompted for the following:
    1. The header file, which is the template file you downloaded to your output directory.
    2. The output directory you created.
    3. The input directory containing the html files form RedNotebook
  5. Once the script is done, it is necessary to manually open the first exported file in your export directory and copy and paste this into the first note imported in Evernote, as mentioned earlier.
What the script does:

  1. Creates a new notebook in Evernote with the same name as the HTML file it is importing.  If you have multiple HTML files, it will create a new notebook for each of these files.
  2. For each HTML file, it will create a new note in the new notebook.  The new note will be named the date the note was created and the creation date of the note will be modified to reflect this, so this metadata is not lost.
Here is the applescript code:


set listOfNames to {}
set fileTemplate to choose file with prompt "Select the template file"
set templateLines to paragraphs of (read fileTemplate)

set outputFolder to choose folder with prompt "Select the output folder"
set createDate to current date
set createFileName to ""

set filelist to my getFileList(choose folder with prompt "Select the source folder")

repeat with currentFile in filelist
 set headerSkipped to false -- skip the RedNotebook header
 set headerContents to false -- drop the content between the top of the file and the RedNotebook header
 
 set newFileLines to {templateLines}
 set listFileNames to {}
 -- read all the lines in the file
 set myLines to readFile(currentFile as alias)
 repeat with nextLine in myLines
  set startTagLoc to (offset of "

" in nextLine) if startTagLoc > 0 then -- found header tag if not headerSkipped then set headerSkipped to true -- skip the initial RedNotebook header else set commaLoc to (offset of "," in nextLine) set endTagLoc to (offset of "

" in nextLine) set dateString to text (commaLoc + 2) thru (endTagLoc - 1) of nextLine -- set the create date and the filename to use set createDate to my convertDate(dateString) set createFileName to text (startTagLoc + 4) thru (endTagLoc - 1) of nextLine set outputFileName to outputFolder & createFileName & ".html" set listFileNames to listFileNames & outputFileName if not headerContents then set headerContents to true else if (count of newFileLines) > 0 then my writeFile(newFileLines, outputFileName, currentFile, createDate, false) my exportToEvernote(currentFile, outputFileName, createDate) -- reset the list set newFileLines to {templateLines} end if end if end if else set newFileLines to newFileLines & nextLine end if headerSkipped createDate createFileName end repeat my writeFile(newFileLines, outputFileName, currentFile, createDate, true) end repeat on getFileList(inputFolder) tell application "Finder" set filelist to every file of the folder inputFolder end tell return filelist end getFileList on readFile(myFile) tell application "Finder" set myLines to paragraphs of (read myFile) end tell return myLines end readFile on convertDate(inputString) return date (inputString) end convertDate on writeFile(newFileLines, newFileName, currentFile, createDate, isEof) -- the following should actually be an end-div, end-body and end-html tags set templateEnd to "" set myFile to currentFile as alias set finalText to {} set end of finalText to newFileLines set outFile to newFileName as text if not isEof then set finalText to finalText & templateEnd end if set AppleScript's text item delimiters to ASCII character 10 set myText to finalText as text set AppleScript's text item delimiters to "" set myFile to open for access outFile with write permission write myText to myFile close access myFile return end writeFile on exportToEvernote(currentFile, newFileName, createDate) set AppleScript's text item delimiters to ":" set noteTitle to item 2 of newFileName set notebookName to last text item of (currentFile as text) set AppleScript's text item delimiters to "" tell application "Evernote" set theItem to create note title noteTitle created createDate notebook notebookName from file (newFileName as text) end tell return end exportToEvernote

No comments:

Post a Comment