Export from Evernote to Yojimbo with AppleScript

Tags: , , , ,

Change Log:

  • 1.02 (October 19, 2009) BUGFIX FOR ADDITIONAL CASE OF “Can’t get item 1 of {}” ERROR (“find notes”/quotation issue)
  • 1.01 (October 18, 2009) BUG FIXES (“Can’t get item 1 of {}” ERROR, UPDATED/FIXED GROWL NOTIFICATIONS)
  • 1.00 (October 2, 2009) INITIAL RELEASE OF SCRIPT

Here’s the code and the download link… Dive in, check it out, and tell me how to make it better in the comment thread below!

And remember:

  1. These scripts are being provided for the benefit of the Mac community. While they work wonderfully for me, I can not take responsibility for any data you might lose (or if your laptop turns into a pumpkin or something…);
  2. “Community” means “participation”. Please share with the rest of us *how* you use these scripts in your work flow, how you think they could be impro­ved (and feel free to help someone if they don’t understand something!)
(*
http://veritrope.com
Evernote to Yojimbo Exporter
Version 1.02
October 19, 2009

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/tips/evernote-yojimbo-export

Installation: Just double-click on 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.02    BUGFIX FOR ADDITIONAL CASE OF "Can’t get item 1 of {}" ERROR ("find notes"/quotation issue)
1.01       BUG FIXES ("Can’t get item 1 of {}" ERROR, UPDATED/FIXED GROWL NOTIFICATIONS)
1.00    INITIAL RELEASE
*)

property theKind : "Evernote"
property theNum : "0"
property CleanTitle : ""
property itemexists : ""
property noteToExport : ""
property theString : ""
(* CHECK FOR GROWL *)
if appIsRunning("GrowlHelperApp") then
    set isRunning to true
    tell application "GrowlHelperApp"
        set allNotificationsList to {"Success Notification", "Failure Notification"}
        set enabledNotificationsList to {"Success Notification", "Failure Notification"}
        register as application ¬
            "Evernote to Yojimbo" all notifications allNotificationsList ¬
            default notifications enabledNotificationsList ¬
            icon of application "Evernote"
    end tell
end if

(*MAIN PROGRAM *)
set myPath to (path to home folder)
tell application "Evernote"
    try
        (* EVERNOTE NOTEBOOK SELECTION SUBROUTINE *)
        set listOfNotebooks to {}
        set EVNotebooks to every notebook --GET THE NOTEBOOK LIST
        repeat with currentNotebook in EVNotebooks
            set currentNotebookName to (the name of currentNotebook)
            copy currentNotebookName to the end of listOfNotebooks
        end repeat

        (*SORT THE FOLDER LIST *)
        set Folders_sorted to my simple_sort(listOfNotebooks)

        (*USER SELECTION FROM NOTEBOOK LIST *)
        set SelNotebook to choose from list of Folders_sorted with title "Select Notebook For Export" with prompt ¬
            "Available Evernote Notebooks" OK button name "OK"
        set EVnotebook to item 1 of SelNotebook
        set listofNotes to {} (*PREPARE TO GET EVERNOTE'S LIST OF NOTES *)
        set allNotes to (find notes "notebook:\"" & EVnotebook & "\" ")
        repeat with currentNote in allNotes
            set currentNoteName to (the title of currentNote)
            copy currentNoteName to the end of listofNotes
        end repeat

        (*SORT THE NOTEBOOK LIST *)
        set Notes_sorted to my simple_sort(listofNotes)

        (*PICK THE NOTES FROM THE LIST *)
        set SelNotes to ¬
            choose from list of Notes_sorted with title ¬
                "Select Notes" with prompt "Multiple Selections Allowed" OK button name "Export Notes" cancel button name "Cancel" with multiple selections allowed
        -- NOTE: ALL UNTITLED NOTES MAY SHOW UP AS SINGLE "UNTITLED" ENTRY

        if (SelNotes is not false) then

            (*TEMP FILES PROCESSED ON THE DESKTOP*)
            tell application "Finder"
                if (the folder "Temp Export From Evernote" exists) then set SaveLoc to (the folder "Temp Export From Evernote")
                if not (the folder "Temp Export From Evernote" exists) then
                    set SaveLoc to (make new folder at (path to desktop folder) with properties {name:"Temp Export From Evernote"})
                end if
            end tell

            (*DO THE CONVERSION*)
            my ENExport(EVnotebook, SelNotes, SaveLoc)
        end if
        (*GROWL *)
        my GrowlNotify(theNum, theKind)
        set TotalNum to 0
        (* ERROR HANDLING *)
    on error errText number errNum
        if isRunning is true then
            if errNum is -128 then
                tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
                    notify with name ¬
                        "Failure Notification" title ¬
                        "User Cancelled" description ¬
                        "Failed to export!" application name "Evernote to Yojimbo"
                end tell
            else -- IF NOT CANCELLED
                tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
                    notify with name ¬
                        "Failure Notification" title ¬
                        "Import Failure" description "Failed to export " & theKind & " item due to the following error:  " & errNum & " (" & errText & ¬
                        ")" application name "Evernote to Yojimbo"
                end tell
            end if -- ENDING GROWL ERROR NOTIFY
        else if isRunning is false 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 appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

on ENExport(EVnotebook, SelNotes, SaveLoc)
    (*FIND FOLDER FOR NOTE*)
    tell application "Evernote"
        set theNum to 0
        repeat with SelNote in SelNotes
            set theString to SelNote
            set theOriginalString to "\""
            set theNewString to ""
            set StripQuotes to my replaceString(theString, theOriginalString, theNewString)
            set noteToExport to (find notes "notebook:\"" & EVnotebook & "\" intitle:\"" & StripQuotes & "\"")
            set noteID to (local id of item 1 of noteToExport)
            set noteName to (title of item 1 of noteToExport)
            (*PREPARE HTML FOLDER *)
            tell application "Finder"
                --activate
                set SingQuote to "-e s/'//g"
                set CleanTitle to do shell script ("echo " & quoted form of SelNote & "  | 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")
                        set ResourceFolder to ((ArchiveInput & ":" & CleanTitle & ".resources") as string)
                        set theFile to (every document file of folder ResourceFolder whose ¬
                            name extension is "PDF")
                        set thePDF to POSIX path of (theFile as alias)
                        (*LOOK FOR PDF *)
                        set itemexists to my PDFDetect(theFile)
                    end try
                end tell

                (*IMPORT PDF IF PRESENT... *)
                tell application "Yojimbo" (*PREPARE YOJIMBO*)
                    if itemexists is true then
                        set resultRecord to import thePDF ¬

                        (*.. OR CONVERT TO WEBARCHIVE FILE*)
                    else
                        tell application "Finder"
                            set ExportedNote to POSIX path of ((ArchiveInput & ":" & CleanTitle & ".html") as string)
                            do shell script ("textutil -convert webarchive " & (quoted form of ExportedNote))
                            set Make_Archive to POSIX path of ((ArchiveInput & ":" & CleanTitle & ".webarchive") as string)
                        end tell
                        set resultRecord to import Make_Archive
                    end if
                end tell
            end tell --Finder
            set theNum to theNum + 1
            set itemexists to ""
        end repeat --SelNotes

        (*DELETE THE TEMP FILE/FOLDER *)
        tell application "Finder" to delete SaveLoc
    end tell --EV
end ENExport

--PDF CHECK
on PDFDetect(theFile)
    try
        set PDFalias to theFile as alias
        return true
    on error
        return false
    end try
end PDFDetect

--REPLACE SUBROUTINE
on replaceString(theString, theOriginalString, theNewString)
    set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, theOriginalString}
    set theStringParts to text items of theString
    if (count of theStringParts) is greater than 1 then
        set theString to text item 1 of theStringParts as string
        repeat with eachPart in items 2 thru -1 of theStringParts
            set theString to theString & theNewString & eachPart as string
        end repeat
    end if
    set AppleScript's text item delimiters to od
    return theString
end replaceString

--SORT SUBROUTINE
on simple_sort(my_list)
    set the index_list to {}
    set the sorted_list to {}
    repeat (the number of items in my_list) times
        set the low_item to ""
        repeat with i from 1 to (number of items in my_list)
            if i is not in the index_list then
                set this_item to item i of my_list as text
                if the low_item is "" then
                    set the low_item to this_item
                    set the low_item_index to i
                else if this_item comes before the low_item then
                    set the low_item to this_item
                    set the low_item_index to i
                end if
            end if
        end repeat
        set the end of sorted_list to the low_item
        set the end of the index_list to the low_item_index
    end repeat
    return the sorted_list
end simple_sort
--GROWL
on GrowlNotify(theNum, theKind)
    tell application "GrowlHelperApp" -- GROWL SUCCESS
        set Plural_Test to (theNum) as number
        if Plural_Test is greater than 1 then
            notify with name ¬
                "Success Notification" title ¬
                "Import Success" description "Successfully exported " & theNum & " " & theKind & ¬
                " items to Yojimbo" application name ¬
                "Evernote to Yojimbo"

        else if Plural_Test is equal to 1 then
            notify with name ¬
                "Success Notification" title ¬
                "Import Success" description "Successfully exported " & theNum & " " & theKind & ¬
                " item to Yojimbo" application name ¬
                "Evernote to Yojimbo"
        end if
    end tell
end GrowlNotify

DOWNLOAD THE COMPILED SCRIPT FILE HERE!

Was this script helpful to you? Please Donate!


Found a bug in the script?

Click Here To Submit Your Bug Report!

Is there a script to do the reverse?

Click here for the Yojimbo to Evernote Script!

Interested in what other scripts are available for Evernote?

Click here to see the latest list!

Export from Evernote to Yojimbo with AppleScript

Pages: 1 2

14 Comments / Jump to comment form

14 Responses to “Export from Evernote to Yojimbo with AppleScript”

  1. Fred Zelders Fred Zelders says:

    Great script! It saved me a lot of time.

    Thanks!

  2. jordan jordan says:

    I keep getting the same error on a PDF: “Can’t get file 1 of {}”

    • Justin Justin says:

      Hi Jordan,

      A quick suggestion — make sure that the initial “Select Evernote Notebook” is visible by looking for the bouncing Evernote icon in your Dock and, if you see it, clicking on it. Often that error results from that window “timing out”.

      If you’ve received that error after selecting a notebook, then let me know which versions of Evernote and Yojimbo you’re using and I’ll take a deeper look.

      But, if your problem is solved, let me know that as well! :D

      • Justin Justin says:

        One more thing, Jordan — This *might be* related to the way Evernote handles items in the Trash.

        Try to empty Evernote’s trash and let me know if this solves the problem!

  3. Justin Justin says:

    Fix is posted in release version 1.02!

  4. AB AB says:

    The only quibble, and forgive me for not being a coder or I’d submit a patch, is that if you’re silly enough to clip the same note twice, the script always craps out, I’m guessing because of some information that’s duplicated (like the title). So that would call for a check and the addition of a -n to the end of the filename if true. You could also do duplicates checking, I suppose, but why bother? The key is to have the script run without interruption. Thanks for a fine script.

    • Justin Justin says:

      AB,

      That’s not a “quibble” — it’s a very constructive suggestion!

      I presume that when you say “-n”, you mean that a number should be appended to the Title (what I would call an “n+1″ count”) — the final result being something like “Title #2″, “Title #3″, etc. ?

      If this is something that you and others think would be helpful, I’ll code it into to the next revision!

      Anyone else agree? Additional suggestions?

      • Jonathan Jonathan says:

        Having the script handle duplicate titles gracefully would really be helpful.

        Also, though, the script keeps spitting out an error for most of my notes, and I think I’ve pinned down the reason: It seems that you built in a workaround for quotation marks in titles, but I think the issue with quotation marks extends to note bodies, as well. The error 1728 I’m getting seems to occur on notes with quotation marks anywhere in their bodies, even if they don’t have them in the title. It would really be great if you could fix this, as then this script would be incredibly useful for me. I would fix it myself, but I don’t know AppleScript.

        Also, one last question: You say there’s a bug with handling of links to audio files–does that apply to audio notes I’ve recorded with the Evernote iPhone app? If so, that is another bug that I would really need to be fixed before this app is truly useful. If all these things get fixed, I would definitely be willing to donate. Thanks a lot for all your great work.

        Jonathan

        • Justin Justin says:

          AB and Jonathan,

          A brief update:
          My first step of script debugging is to try and replicate the conditions which caused the thing to fail in the first place.

          After working with a few different samples, here’s the latest…

          – I was only able to reproduce your quotation-in-the-title errors *some of the time*… I think I’ve zeroed in on where the thing chokes and it should be fixed in the next revision.

          – I was *NOT* able to reproduce the quotation-in-the-body errors. Was there any punctuation in the title of the note?

          – While the script doesn’t have the “n+1″ duplicate naming feature yet, duplicate notes do not cause the script to fail on my test computer. For me, Yojimbo just creates a new note with the same name. Anyone else having the script stop when it sends a duplicate note to Yojimbo?

          – Finally, “the audio note issue” is, in part, a limitation of Yojimbo and Evernote themselves.

          As far as I can tell, even though Yojimbo wasn’t designed to be a media manager per se, a note will allow you to create a link to an audio file if you drop the file into it. Evernote will allow you to export an audio note but, given the way this script is designed to create a temporary folder which is deleted after export, it would probably be better to create a custom script to do what you want.

          That said, I think there will be compelling reasons to keep audio notes in Evernote in 2010. A recent interview with Phil Libin (CEO of Evernote) suggests that built-in voice transcription is on the way. Once those voice notes get added to a searchable index….. ;)

          • Jonathan Jonathan says:

            Justin,

            -Once I get home, I will check about other punctuation in the titles of the notes it chokes on. I could be wrong about it being the quotation marks in the bodies, but in a (pretty) superficial test, it seemed like all the notes it was choking on had quotation marks in the bodies and all the ones it wasn’t choking on didn’t. But I will check on other punctuation and I will also create new test notes to try to replicate it.

            -Since I haven’t been able to export my notes yet due to the above issue, I don’t know how it handles duplicates; I was only basing my comment on AB’s implication. But if you’re saying it just creates notes with the same title with no problems, that’s actually the behavior I would prefer over the sequential numbering scheme.

            -Regarding the audio notes: The whole reason I want to export my notes from Evernote to Yojimbo in the first place is because I have over a thousand notes in Evernote that I never bothered to tag, and I want to get them organized. Yojimbo has a much better tagging UI for what I need, because it lets me apply groups of tags in batch, something it seems Evernote can’t do. Being able to do that will save me hours of work. So what I want to do is export all my notes from Evernote to Yojimbo, tag them in Yojimbo, then export them from Yojimbo back to Evernote. And many of my notes that need to be tagged are audio notes. Do you think it would be possible to implement some sort of capability for this in the script? I don’t even care if it’s a workaround–it could create a dummy note in Yojimbo that won’t even play the audio file, as long as I can add tags to the note and be able to export the note back into Evernote and have it be fully functional once in Evernote.

            Also, do you think my whole plan of using Yojimbo just to tag is foolish? Does any metadata on the notes (like creation date, modification date, creation location from the iPhone app, etc.) get lost in either of the two legs of the transfer when using your scripts? Is there a way you could modify the scripts to preserve that data? That would be amazing.

            Thanks a lot for all your help.

            Jonathan

  5. Justin Justin says:

    With Andrè Fincato’s help, I think we’ve found a bug in the way the script handles typography. Great work, Andrè!

    I’m going to try to code a work-around as soon as possible — stay tuned!

  6. Justin Justin says:

    Jonathan,

    Let me know about the punctuation when you have a moment. User reports like yours really help me to find and fix things much more quickly!

    As pertains to your Yojimbo=>Evernote=>Yojimbo concept, it’s certainly *possible* to do it that way, but personally I wouldn’t recommend it.

    Let start here — Have you already tried Evernote’s built-in way to add a tag to multiple notes? Many people don’t realize that you can do this by:

    Selecting all the notes you’d like to add the same tag to, and;
    Dragging-and-dropping them onto the tag in the sidebar’s Tag List.

    The virtue of this approach is, of course, it keeps everything in Evernote and reduces the risk of your other metadata being changed (like creation date, creation location, etc.).

    I have also been pushing for Evernote to add some additional support for tagging and attachments in AppleScript and I’m optimistic that it’ll happen eventually. (I actually have a skeleton of a “Bulk Tagger Script” ready to go in anticipation!) A brief post from you to the Evernote forum saying “Hey — Could you make AppleScript a priority?” might help us get there sooner…. squeaky wheels and such! ;)

    If the drag-and-drop approach isn’t going to do the trick, let me know more about *how* you need to tag and maybe I can help you brainstorm an alternate approach.

rss Subscribe to Comments RSS Feed

Tweetbacks

  1. Twitter Comment


    @Veritrope Thanks, but I ended up with EagleFiler instead.

    Posted using Chat Catcher

  2. Twitter Comment


    Is it normal to have problems exporting notes from Evernote to Yojimbo using this? [link to post]

    Posted using Chat Catcher

  3. Twitter Comment


    @Veritrope Thanks so much.

    Posted using Chat Catcher

  4. Twitter Comment


    Stumbled on: “Export from #Evernote to #Yojimbo with AppleScript” [link to post] This is awesome. Thanks #Veritrope for this.

    Posted using Chat Catcher

  5. Twitter Comment


    @Veritrope It worked. Well now I can go on with the exporting-importing work. Thank you.

    Posted using Chat Catcher


Trackbacks/Pingbacks

  1. Links to Evernote Applescripts and Accessories-- Updated Regularly! | Veritrope
  2. Export from Yojimbo to Evernote with AppleScript | Veritrope
  3. Export from Yojimbo to Evernote with AppleScript | Veritrope

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>