Evernote to MacJournal Exporter

Description

Version 3.3 of the Evernote Client may have errors which prevent this AppleScript from working -- Click here for the latest update on the situation.

Updated on January 4, 2012 to Version 2.0 (Beta 1)

This Evernote to MacJournal Exporter AppleScript is a complete re-write of the 1.x version. To use it, just select the items you'd like to export from Evernote and run the script. PDFs are added directly and Non-PDF items will be transferred to MacJournal as "Web Archives" in order to preserve as much formatting as possible. Evernote meta information (such as tags, location data, creation date) should also come along for the ride!

Some caveats: This version doesn't have any error handling, GROWL notifications, and I haven't tested it thoroughly (hence the "Beta 1" designation).

Please let me know how it works for you in the comment thread below -- Hope you enjoy!

The Code

(*
Veritrope.com
Evernote to MacJournal Exporter
Version 2.0 (Beta 1)
January 4, 2012

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/evernote-to-macjournal-exporter

// CHANGELOG:

2.00     (Beta 1) REWRITE
1.01     BUG FIXES ("Can’t get item 1 of {}" )
1.00    INITIAL RELEASE

// RECOMMENDED INSTALLATION INSTRUCTIONS:

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/NAME OF APP
--Set up your keyboard shortcut
*)


(*
======================================
// USER SWITCHES (YOU CAN CHANGE THESE!)
======================================
*)


(*
======================================
// OTHER PROPERTIES (USE CAUTION WHEN CHANGING)
======================================
*)


property itemexists : ""

(*
======================================
// MAIN PROGRAM
======================================
*)


--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
set SaveLoc to (SaveLoc as alias)

--SELECT JOURNAL FOR IMPORT
tell application "MacJournal"
    activate
    set SelJournal to (choose journal with prompt "Choose Journal")
end tell

--DO THE CONVERSION
my ev_Export(SaveLoc, SelJournal)

--SILENT DELETE OF TEMP FOLDER
my trashfolder(SaveLoc)

(*
======================================
// UTILITY SUBROUTINES
======================================
*)


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

--ADD LEADING ZEROS
on add_leading_zeros(this_number, max_leading_zeros)
    set the threshold_number to (10 ^ max_leading_zeros) as integer
    if this_number is less than the threshold_number then
        set the leading_zeros to ""
        set the digit_count to the length of ((this_number div 1) as string)
        set the character_count to (max_leading_zeros + 1) - digit_count
        repeat character_count times
            set the leading_zeros to (the leading_zeros & "0") as string
        end repeat
        return (leading_zeros & (this_number as text)) as string
    else
        return this_number as text
    end if
end add_leading_zeros

--SILENT DELETE OF TEMP FOLDER (THANKS MARTIN MICHEL!)
on trashfolder(SaveLoc)
    try
        set trashfolderpath to ((path to trash) as Unicode text)
        set srcfolderinfo to info for (SaveLoc as alias)
        set srcfoldername to name of srcfolderinfo
        set SaveLoc to quoted form of POSIX path of SaveLoc
        set counter to 0
        repeat
            if counter is equal to 0 then
                set destfolderpath to trashfolderpath & srcfoldername & ":"
            else
                set destfolderpath to trashfolderpath & srcfoldername & " " & counter & ":"
            end if
            try
                set destfolderalias to destfolderpath as alias
            on error
                exit repeat
            end try
            set counter to counter + 1
        end repeat
        set destfolderpath to quoted form of POSIX path of destfolderpath
        set command to "ditto " & SaveLoc & space & destfolderpath
        do shell script command
        -- this won't be executed if the ditto command errors
        set command to "rm -r " & SaveLoc
        do shell script command
        return true
    on error
        return false
    end try
end trashfolder

(*
======================================
// MAIN HANDLER SUBROUTINES
======================================
*)

on ev_Export(SaveLoc, SelJournal)
    --GET SELECTION
    tell application "Evernote"
        set SelNotes to selection
        set exported_Notes to {}
       
        repeat with SelNote in SelNotes
           
            --GATHER NOTE INFORMATION
            set the_Title to title of (item 1 of SelNote)
            set the_Notebook to notebook of (item 1 of SelNote)
            set the_URL to source URL of (item 1 of SelNote)
            set the_Notelink to note link of (item 1 of SelNote)
            set the_Lat to latitude of (item 1 of SelNote)
            set the_Long to longitude of (item 1 of SelNote)
           
            --GATHER TAG INFORMATION
            set ev_Tags to tags of (item 1 of SelNote)
            set the_Tags to {}
            repeat with ev_Tag in ev_Tags
                copy (name of ev_Tag) to the end of the_Tags
            end repeat
           
            --GATHER AND FORMAT TIME INFORMATION
            set creation_Date to creation date of (item 1 of SelNote)
            set the_Year to (year of creation_Date) as integer
            set the_Month to my add_leading_zeros((month of creation_Date as integer), 1)
            set the_Date to my add_leading_zeros((day of creation_Date as integer), 1)
            set the_Hour to my add_leading_zeros((hours of creation_Date as integer), 1)
            set the_Min to my add_leading_zeros((minutes of creation_Date as integer), 1)
            set the_Sec to my add_leading_zeros((seconds of creation_Date as integer), 1)
            set cre_Date to (the_Year & the_Month & the_Date & "T" & the_Hour & the_Min & the_Sec) as string
           
            --CREATE THE RECORD FOR EXPORT
            copy {the_Title, (name of the_Notebook), cre_Date, creation_Date, the_Tags, the_URL, the_Notelink, the_Lat, the_Long} to end of exported_Notes
        end repeat
       
        --BEGIN EXPORT FROM EVERNOTE
        set export_Count to (count of exported_Notes)
        set export_Increment to 1
       
        repeat with exported_Note in exported_Notes
           
            --RESET NOTE VALUES FROM EXPORTED NOTE LIST
            set mj_Title to ""
            set mj_Notebook to ""
            set mj_FormattedCreation to ""
            set mj_Creation to ""
            set mj_Tags to ""
            set mj_URL to ""
            set mj_Notelink to ""
            set mj_Lat to ""
            set mj_Long to ""
            set mj_Annotation to ""
            set itemexists to ""
           
            --PULL NOTE VALUES FROM EXPORTED NOTE LIST
            set mj_Title to (item 1 of exported_Note)
            set mj_Notebook to (item 2 of exported_Note)
            set mj_FormattedCreation to (item 3 of exported_Note)
            set mj_Creation to (item 4 of exported_Note)
            set mj_Tags to (item 5 of exported_Note)
            set mj_URL to (item 6 of exported_Note)
            set mj_Notelink to (item 7 of exported_Note)
            set mj_Lat to (item 8 of exported_Note)
            set mj_Long to (item 9 of exported_Note)
            set mj_Annotation to ("Original Item: " & mj_Notelink)
           
            --CHOOSE URL OR NOTE LINK FOR MJ LINK
            if mj_URL is missing value then set mj_URL to ""
            if mj_URL is not "" then
                set mj_Link to mj_URL
            else
                set mj_Link to mj_Notelink
            end if
           
            --ADJUST LAT/LONG, IF NECESSARY
            if mj_Lat is missing value then set mj_Lat to 0
            if mj_Long is missing value then set mj_Long to 0
           
            --CREATE INCREMENTAL SAVE LOCATION
            set export_Folder to (SaveLoc & export_Increment as string)
            set export_Note to {}
           
            --FIND THE NOTE IN EVERNOTE
            set the_Notes to (find notes "created:" & mj_FormattedCreation & " intitle:"" & mj_Title & """)
           
            --IF UNTITLED, ZERO IN ON CORRECT NOTE
            repeat with the_Note in the_Notes
                if creation date of the_Note is equal to mj_Creation then
                    copy (item 1 of the_Note) to the end of export_Note
                end if
            end repeat
           
            --EXPORT THE NOTE FROM EVERNOTE AS HTML
            export export_Note ¬
                to export_Folder ¬
                format HTML
           
            --BEGIN PROCESSING ITEM FOR MACJOURNAL 
            tell application "Finder"
                try
                    --GET EXPORTED FILE
                    set exported_Note to (every file of folder export_Folder whose ¬
                        name extension is "html")
                    set exported_POSIX to (POSIX path of (exported_Note as alias))
                    set exported_Filename to (name of (item 1 of exported_Note))
                   
                    --TRIM EXTENSION
                    set old_TID to AppleScript's text item delimiters
                    set AppleScript's text item delimiters to "."
                    set folder_Name to (first text item of exported_Filename)
                    set AppleScript's text item delimiters to old_TID
                   
                    --LOOK  FOR RESOURCE FOLDER
                    set ResourceFolder to ((export_Folder & ":" & folder_Name & ".resources") as string)
                   
                    --LOOK FOR PDF
                    set theFile to (every document file of folder ResourceFolder whose ¬
                        name extension is "PDF")
                    set thePDF to POSIX path of (theFile as alias)
                    set itemexists to my PDFDetect(theFile)
                end try
            end tell
           
            --IMPORT PDF IF PRESENT...
            tell application "MacJournal"
                if itemexists is true then
                    tell SelJournal
                        set resultObject to make new journal entry with properties ¬
                            {name:mj_Title, content file:thePDF, date:mj_Creation, link:mj_Link, annotation:mj_Annotation, location latitude:mj_Lat, location longitude:mj_Long}
                        repeat with mj_Tag in mj_Tags
                            add tag mj_Tag to resultObject
                        end repeat
                    end tell
                   
                    --OR CONVERT HTML TO A WEBARCHIVE FILE
                else
                    tell application "Finder"
                        do shell script ("textutil -convert webarchive " & (quoted form of exported_POSIX))
                        set mj_Archive to POSIX path of ((export_Folder & ":" & folder_Name & ".webarchive") as string)
                    end tell
                    tell SelJournal
                        set resultObject to make new journal entry with properties ¬
                            {name:mj_Title, content file:mj_Archive, date:mj_Creation, link:mj_Link, annotation:mj_Annotation, location latitude:mj_Lat, location longitude:mj_Long}
                        repeat with mj_Tag in mj_Tags
                            add tag mj_Tag to resultObject
                        end repeat
                    end tell
                end if
            end tell
            set export_Increment to (export_Increment + 1)
        end repeat
    end tell
end ev_Export

9 Responses to “Evernote to MacJournal Exporter”

  1. korm November 10, 2012 at 7:00 am #

    Justin, the body of the entry this creates in MacJournal cannot be accessed — cannot click into it, add or edit text, etc. The “editable” checkbox for the entry is checked, so we should be able to make changes or edit. (MacJournal 6.0.7b1 on 10.8.2 — it’s beta, so this is probably a MJ issue.)

    • Justin Lancy November 10, 2012 at 10:16 am #

      I’m not able to test this at the moment, but it might have to do with items being imported as “Web Archives”. I chose this route to preserve formatting, but it may mean that MJ can’t edit.

  2. korm November 11, 2012 at 6:47 am #

    I think that’s it Justin. Thanks for the script.

  3. Calion May 2, 2014 at 4:26 pm #

    Can we fix this? This script isn’t worth very much to me if I can’t edit the result.

    • Justin Lancy May 18, 2014 at 10:52 am #

      This is a pretty old script but, if enough people raise their hand, I’ll circle back around and take a look to see if there is a new way of doing this. 🙂

  4. korm May 18, 2014 at 11:13 am #

    I’m interested. Maybe use Brett Terpstra’s Marky? The DEVONthink browser clipper is now using Marky, for example.

    • Justin Lancy May 31, 2014 at 7:36 am #

      Sorry for the delay in responding! I’ll look at this and see what is possible…

  5. Lasse June 16, 2014 at 9:42 am #

    Is there an upcoming version for Evernote 5.5.1?

    • Justin Lancy June 17, 2014 at 3:20 am #

      Can’t promise when an update will be ready, but I am working on one! 🙂