Export Selected Evernote Items as RTFD Files

Description

This script exports a rich text copy of selected Evernote items and saves them in RTFD format. It also leaves an HTML copy of the note (and its resources) inside a sub-folder named after the note (in case you find it useful!) Just highlight the notes you want to export in Evernote and then run the script (you can highlight multiple notes). Tell the script where you'd like the files saved and it will go to work!

Automatic Growl detection has also been enabled which will save you a trip to the Script Editor to enable/disable it.

The RTFD conversion works with most notes, but not with PDFs! The script exports any available text information to the RTFD file and places attached files in a resource folder by the same name at your export location. Notes with typographic symbols in the title will sometimes halt the conversion (see an example in the picture below).  I've done some work on having the script automatically eliminate the offending characters but, if you still have problems with a particular note, just edit the symbols out of the title and you should be fine!

The Code

(*
◸ Veritrope.com
Export Selected Evernote Items as RTFD Files
VERSION 1.4
June 15, 2014

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/export-selected-evernote-items-as-rtfd-files/
Installation: Just run the script!

FastScripts Installation (Optional, but recommended):
--Download and Install FastScripts from http://www.red-sweater.com/fastscripts/index.html
--Copy script or an Alias to ~/Library/Scripts/Applications/Evernote
--Set up your keyboard shortcut

CHANGELOG:
1.40     UPDATED TO MAKE RTF CREATION DATE MATCH EVERNOTE ITEM
1.30    UPDATED TO UTILIZE NEW EVERNOTE APPLESCRIPT SELECTION AVAILABILITY
1.20    UPDATED TO FIX A COSMETIC ISSUE, CHANGE PATH FOR RTFD FILES
1.10     UPDATED TO USE NATIVE HTML EXPORT, CREATE RTFD FILES
1.00    INITIAL RELEASE
*)


property clean_text : ""
property CleanTitle : ""
property theNum : "0"
property RTFPath : ""


(* CHECK FOR GROWL *)
tell application "System Events"
    set processnames to name of every process
end tell

if "GrowlHelperApp" is in processnames then
    tell application "GrowlHelperApp"
        set the allNotificationsList to {"Success Notification", "Failure Notification"}
        set the enabledNotificationsList to {"Success Notification", "Failure Notification"}
        register as application ¬
            "Evernote to RTFD" all notifications allNotificationsList ¬
            default notifications enabledNotificationsList ¬
            icon of application "Evernote"
    end tell
end if

(*MAIN PROGRAM *)

tell application "Evernote"
    try
        (*PICK THE NOTES FROM THE LIST *)
        set SelNotes to the selection
       
        if (SelNotes is not false) then
            (*PICK EXPORT DESTINATION*)
            set SaveLoc to choose folder with prompt "Choose The Destination Folder or Volume:" default location (path to desktop folder)
           
            (*DO THE CONVERSION*)
            my ENEXExport(SelNotes, SaveLoc)
           
            if "GrowlHelperApp" is in processnames then -- GROWL SUCCESS
                tell application "GrowlHelperApp"
                    notify with name ¬
                        "Success Notification" title ¬
                        "Successful Export to RTF!" description ¬
                        "Number of Notes Exported: " & theNum application name "Evernote to RTFD"
                end tell
            end if
        else
            if "GrowlHelperApp" is in processnames then
                tell application "GrowlHelperApp" -- GROWL FAILURE FOR NO SELECTION
                    notify with name ¬
                        "Failure Notification" title ¬
                        "Export Failure" description ¬
                        "No notes selected!" application name "Evernote to RTFD"
                end tell
            else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR NO SELECTION
                display dialog "No headline or tab selected!" with icon 0
            end if
        end if
       
       
        (* ERROR HANDLING *)
    on error errText number errNum
        if "GrowlHelperApp" is in processnames then
            tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
                notify with name ¬
                    "Failure Notification" title ¬
                    "Import Failure" description "Export Failed due to the following error:  " & errText & return & return & "Note Name: " & CleanTitle ¬
                    application name "Evernote to RTFD"
            end tell
        else if "GrowlHelperApp" is not in processnames then -- NON-GROWL ERROR MSG. FOR ERROR
            display dialog "Item Failed to Import: " & errNum & return & errText with icon 0
        end if
    end try
end tell



(* SUBROUTINES *)
on ENEXExport(SelNotes, SaveLoc)
    (*FIND FOLDER FOR NOTE*)
    tell application "Evernote"
        set theNum to 0
        repeat with SelNote in SelNotes
            set noteToExport to (contents of SelNote)
            set NoteID to (local id of item 1 of noteToExport)
            set noteName to (title of item 1 of noteToExport)
            set EVnotebook to (name of notebook) of item 1 of noteToExport
           
            set noteCreation to (creation date of item 1 of noteToExport)
           
            (*PREPARE HTML FOLDER *)
            tell application "Finder"
                set SingQuote to "-e s/'//g"
                set CleanTitle to do shell script ("echo " & quoted form of noteName & "  | sed -e 's/:/ /g' -e 's/"//g' -e 's/—/ /g' " & (quoted form of SingQuote)) --CLEAN OUT COLONS FOR FILE PATH (SOUNDS GROSSER THAN IT IS)
                set ArchiveInput to ((SaveLoc as Unicode text) & CleanTitle)
               
                (*NEW HTML EXPORT *)
                tell application "
Evernote"
                    export {noteToExport} ¬
                        to ArchiveInput ¬
                        format HTML
                end tell
               
                tell application "
Finder"
                    (*LOOK FOR RESOURCE FOLDER *)
                    try
                        set EVNote to (every file of folder ArchiveInput whose ¬
                            name extension is "
html")
                        set name of (item 1 of EVNote) to (CleanTitle & "
.html")
                    end try
                end tell
               
                (*PREPARE FILENAME *)
                set InputFile to ("
" & SaveLoc & CleanTitle & ":" & CleanTitle & ".html") as string
                set ImportPOSIX to (quoted form of (POSIX path of InputFile))
                set SaveRTFLocation to ("
" & SaveLoc & CleanTitle & ".rtfd") as string
                set RTFPath to (quoted form of (POSIX path of SaveRTFLocation))
               
                (*CONVERT THE FILE*)
                set clean_text to do shell script ("
textutil  -convert rtfd -output " & RTFPath & " " & ImportPOSIX & "")
                set fileCreated to my dateUSToUnix(noteCreation)
                do shell script "
touch -t " & fileCreated & " " & RTFPath
            end tell
            set theNum to theNum + 1
        end repeat
    end tell
end ENEXExport

--CONVERT CREATION DATE TO UNIX FORMAT
on dateUSToUnix(noteCreation)
    set y to (year of noteCreation)
    set mo to (month of noteCreation) * 1
    if mo < 10 then set mo to "
0" & mo
    set d to (day of noteCreation)
    if d < 10 then set d to "
0" & d
    set h to (hours of noteCreation)
    if h < 10 then set h to "
0" & h
    set m to (minutes of noteCreation)
    if m < 10 then set m to "
0" & m
    set s to (seconds of noteCreation)
    if s < 10 then set s to "
0" & s
    return (y & mo & d & h & m & "
." & s) as string
end dateUSToUnix

29 Responses to “Export Selected Evernote Items as RTFD Files”

  1. h April 30, 2011 at 10:03 pm #

    Dear, Justin.
    What a great script!

    I tried to put source URL onto “Spotlight comment” box of a converted RTFD.
    Is this possible?

    I was able to do it only on a converted HTML file.
    (write down only these 2 lines)

    set noteURL to (source URL of item 1 of noteToExport) as text
    set comment of (item 1 of EVNote) to noteURL

    Please tell me how I can do that.

    • Justin Lancy May 1, 2011 at 2:29 pm #

      Since you’ve added the “set noteURL” line towards the top of the script, go to this part towards the end:

      (*CONVERT THE FILE*)
      set clean_text to do shell script ("textutil  -convert rtfd -output " & RTFPath & " " & ImportPOSIX & "")

      After this line, try adding this bit of code:

      if noteURL is not missing value then
          set RTF_File to file SaveRTFLocation
          set comment of RTF_File to noteURL
      end if

      Does that work for you?

      • h May 2, 2011 at 3:10 am #

        IT WORKS!!!

        Thank you very much for your support.
        This script must be the best important one for my work.!!

        (because source URL is always need) !!!

  2. h May 2, 2011 at 10:13 am #

    I have only one more question.
    set NoteLatitude to (latitude of item 1 of noteToExport)
    set NoteLongitude to (longitude of item 1 of noteToExport)
    set NoteAltitude to (altitude of item 1 of noteToExport)

    I made it the meta information of positions too.
    But I cannot get tags information from selection.
    set NoteTag to (tag of item 1 of noteToExport)
    set NoteTag to (tags of item 1 of noteToExport)

    Is this difficult?

    • Justin Lancy May 2, 2011 at 2:50 pm #

      It’s only a little more difficult… but let me use this as an opportunity to demonstrate something that may help you in your future scripts:

      In AppleScript, some pieces of information are seen as text… just words or names. Other pieces — like a list of tags, for example — are seen differently.

      In order to make a list into text, we must “coerce” it into text form. In this particular case, we’ll want to come up with two bits of code: One to make a list of the tag names, and one to coerce that list into a text string that will look nice in the Spotlight comments.

      First, let’s make our list:

      --Make Tag List
      set NoteTags to (tags of item 1 of noteToExport)
      set list_of_Tags to {}

      repeat with NoteTag in NoteTags
          copy (name of NoteTag) to the end of list_of_Tags
      end repeat

      What this does is:

      1. Gets the Tags from your Evernote item
      2. Prepares a variable (“list_of_Tags”) to receive our list
      3. Uses a repeat to go through all the tags and copy their name to the end of the list

      I’d like to point out a detail for your attention:

      set NoteTags to (tags of item 1 of noteToExport)

      I changed your code to use the plural form NoteTags instead of NoteTag deliberately. By doing so, it makes it easier to write something that will go through a list one item at a time, as in:

      repeat with NoteTag in NoteTags

      Now that we have our list of tag names, let’s coerce it into text form:

      -- Convert list_of_Tags to text
      set old_delim to AppleScript's text item delimiters
      set AppleScript's text item delimiters to ", "
      set list_of_Tags to list_of_Tags as text
      set AppleScript's text item delimiters to old_delim

      AppleScript calls whatever separates pieces of text a text item delimiter. By default, AppleScript’s text item delimiter is {“”} (basically an empty string), but AppleScript also lets you change that value to whatever you want… and this is a useful strategy to break up pieces of text in different ways!

      What I’m doing in the code above is:

      1. Setting a value (old_delim) to hold on to AppleScript’s default text item delimiter so that it’s easy to change back to it when I’m done
      2. Changing the AppleScript text item delimiter to “, “. This puts a comma and a space after each tag. (You could, of course, change this to whatever you would prefer.)
      3. Running the list through again — but this time with the new delimiters.
      4. Changing AppleScript’s text item delimiters back to their original value (old_delim)

      Now when you add the List of Tags to your Spotlight comments, it should create something there like “Tag 1, Tag 2, Tag 3″…

      Give it a try and tell me how it works out for you!

      • h May 2, 2011 at 6:27 pm #

        Fantastic…

        I have been wainting for using evernote files on finder and other applications too, to keep information from original.
        Thank you so much your kind.

        This is my custamize.

                        if noteURL is missing value then
                            set noteURL to "No URL"
                        end if
                        if NoteLatitude is missing value then
                            set NoteLatitude to "No Geotag"
                        end if
                        if NoteLongitude is missing value then
                            set NoteLongitude to ""
                        end if

        and,

                        set the clipboard to noteURL & return & NoteLatitude & space & NoteLongitude & return & list_of_Tags & return & EVcre & return & return
                        open RTF_File
                        delay 0.2
                        tell application "System Events" to keystroke "v" using {command down}
                        tell application "System Events" to keystroke "s" using {command down}
                        tell application "System Events" to keystroke "w" using {command down}

        Then I can put all information on the top lines of the exported RTFD.!!!

        Thank you for GREAT script!!!!

  3. Steve Jamieson June 10, 2011 at 6:21 pm #

    Folks,

    Is there any way this script can make the creation date of the resulting rtf files the same as the creation date of the EN note? For the moment the rtf creation date is set to today.

    Thanks for your thoughts.

    Steve J

    • Justin Lancy June 13, 2011 at 2:28 pm #

      Hi Steve!

      I worked up some code that should change the creation date of the RTF files to match the Evernote items. (Resource folders still have today’s date). Believe it or not, this simple task is actually not very easy to do via AppleScript and I had to step up my script-fu a notch or two to make it happen. 😉

      So try the latest version… and please tell me if it works for you!

  4. polocanada July 23, 2011 at 4:40 pm #

    Thank you. Helpful!
    I guess the only note is I would put for newbies.. to download the script use Safari browser only. Link won’t download using other browser.

    • Justin Lancy July 23, 2011 at 5:22 pm #

      Is anybody else having problems getting the Source Code with a browser other than Safari?

      • Becky February 5, 2012 at 8:27 pm #

        I copy and pasted the script into “script editor” on mac. It works, in that it exports my files, but all of them somehow have a black background, so that you can’t see any text until you change that. Any reason that would be happening?

        Thanks.

        • Justin Lancy February 5, 2012 at 8:38 pm #

          Hi Becky,

          Can you tell me which version of Mac OS X you’re running? (I’ve heard of a related bug with certain versions and it might help me get to the bottom of your issue!)

          • Becky February 5, 2012 at 9:02 pm #

            I am running 10.5.8

            Thanks for looking into it!
            -Becky

          • Justin Lancy February 5, 2012 at 9:35 pm #

            Okay — I just finished re-reading some old notes on the problem:
            Are you using a version of Safari that is less than 5.01? If so, upgrade Safari, reboot your system, and try again.

            If you *are* using a more recent version, then please let me know and I’ll see what else can be done (if anything).

          • Becky February 5, 2012 at 9:48 pm #

            I’m a little confused – what does the browser version matter in terms of the script? I have a safari version higher than what you mentioned, but I am currently running Chrome. Regardless, I got the script by copying and pasting the text into the script editor (perhaps that was my mistake?)

            Thanks again.
            Becky

          • Justin Lancy February 5, 2012 at 9:57 pm #

            Seems counter-intuitive, doesn’t it? 😉

            The short answer is that, for many people, a systemwide bug was caused by some code in Safari. When Safari was updated, it got fixed.

            Assuming that you aren’t going to update from 10.5.x anytime soon, let’s take this out of the comment thread and I’ll contact you directly to see if there is anything else I can do to troubleshoot via email.

          • Kaleo June 29, 2012 at 7:12 pm #

            I encounter the same problem of the black background in 10.5.8. Did you find a fix for this?

          • Justin Lancy June 29, 2012 at 7:14 pm #

            Sadly, the only fix seems to be to upgrade to 10.6 or above.

            🙁

  5. MacGyver November 16, 2011 at 1:28 pm #

    Great script !

    But I found me with two little problems:
    1- Export the URL metadata (noteURL).
    2- And try to not export a HTML file.

    I need only RTF with text and metadata (tags and URL).

    I run this script under:

    MAC OS X 10.6.5 Snow Leopard
    Evernote 2.2.3
    Growl 1.2.2

    Problem 1 – noteURL
    I added the code that said in that comment.

    But, then Growl notify this message:
    Import Failure

    Export Failed due to the following error:
    The variable noteURL is not defined.

    Problem 2 – no HTML
    I commented the lines of code that I think export the HTML file, but the final result are that not create the RTFD file correctly.

    And a genius plus, that create only RTF (text, tags and URL) instead RTFD, for open directly and fast by Word !

    • Justin Lancy November 18, 2011 at 12:28 pm #

      Just to confirm — you already added the AppleScript that was in h’s comment before mine? (By the way, I just edited his comment to make the code stand out more clearly). My guess is that you were missing that piece, which was giving you the noteURL not defined error.

      Also — I’m not sure if I understand “Problem 2” completely. Are you saying that you’d like to alter the script to export plain RTF files?

      If so, you might try experimenting with the settings in the line which converts the document. I am using textutil, which is one of those beautiful little apps that is already on every Mac and that so few people know about (because you run it from the command line).

      For example, you could change the line this way to make it spit out plain RTF files (which are missing all the images, etc. of course):

      (*CONVERT THE FILE*)
      set clean_text to do shell script ("textutil  -convert rtf -output " & RTFPath & " " & ImportPOSIX & "")

      Check out the textutil manual page for more options… and have fun!

  6. MacGyver December 4, 2011 at 1:07 pm #

    Problem 1 – noteURL

    Yes! I forgot put the following lines:
    set noteURL to (source URL of item 1 of noteToExport) as text
    set comment of (item 1 of EVNote) to noteURL

    But, If I put the first line (set noteURL…), before the second line, show an error from Applescript Editor:
    Sintax error: was expected “,” but found property

    Well, If I put this line before of the line –> (*PREPARE HTML FOLDER *)
    didn’t show the error.
    Then, I can run the script, but NOT include the URL metadata in the RTFD file.

    Where I must put the first line (set noteURL…), for export URL metadata in the RTFD file ?

    Problem 2 – no HTML
    I’d like to alter the script to NOT export or create the HTML files.
    Only created the RTFD files.

    Thanks for the “textutil” tip.

    • Drew October 25, 2012 at 6:52 pm #

      Hi, would also be interested to know how we could do away with the HTML versions, i.e. either:

      a) Not produce the HTML version at all in the first place, or,
      b) use the script to automatically “clean up” after itself and delete the created folder and HTML file.

      Thanks! Cool script. 🙂

  7. alwaro May 18, 2012 at 12:48 am #

    Very very interesting!! thanks

  8. Drew October 25, 2012 at 6:38 pm #

    Thanks, this is just what I was looking for. 🙂

  9. Jose de la Parra June 17, 2013 at 3:06 pm #

    Thanks! You saved me some long boring hours.

  10. Leger March 10, 2014 at 4:25 am #

    Hi,

    I’ve been trying the code, but the same error comes over and over, whether I copy-paste the code or use the direct link: syntax error expected end of line but found “””

    The error comes on this line: “Evernote to RTFD” all notifications allNotificationsList ¬

    Can you please help me? I am not good technically, but I really need to be able to export my notes on rtf….

    Thanks!

    Btw, your posts are great!

    • Justin Lancy March 14, 2014 at 2:40 am #

      Hey there – Feel free to send me a Bug Report with more details and I’ll take a look.

  11. kenl April 19, 2014 at 10:45 pm #

    Leger, I ran into what I suspect was the exact same problem as you. I worked around it by copying & pasting the text into a new blank applescript document. Then, this is the important part, compile the script. The compilation will change it all from commented looking text to the actual script-looking text with the proper color coding and bolding. Also, I don’t have growl so I had to comment out all the growl-related code, but that’s no biggie.

    I’m using Applescript Editor 2.5.1 on OS X 10.8.5 if that makes any diff.

    Justin, thank you for your work on this.