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 […]

(*
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

This script exports a copy of all the entries in a selected MacJournal and saves them into Evernote while providing Growl Notification of its progress! FYI – Although MacJournal uses tagging, it isn’t currently possible to read those tags via AppleScript. Therefore, you’ll need to tag your items once they’ve been imported into Evernote. Change […]

(*
http://veritrope.com
MacJournal to Evernote Exporter
Version 1.0
October 2, 2009

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/tips/macjournal-evernote-export
           
Installation: Just double-click on the script!

CHANGELOG:
1.00    INITIAL RELEASE
*)


property theNum : "0"
property EVnotebook : ""
property thename : ""
property theText : ""

(* 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 ¬
            "MacJournal to Evernote" all notifications allNotificationsList ¬
            default notifications enabledNotificationsList ¬
            icon of application "Evernote"
    end tell
end if

(*MAIN PROGRAM *)
tell application "MacJournal"
    set theNum to 0
    try
        set this_selection to my Journal_List()
        set EVnotebook to my Notebook_List()
        repeat with this_entry in this_selection
            if the (content file of this_entry) is not equal to missing value then
                set theText to (content file of this_entry)
            else
                set theText to (the plain text content of this_entry)
            end if
            set thename to (the name of this_entry)
            set theDate to (the date of this_entry)
           
            tell application "Evernote" -- CREATE THE NOTE FROM ITEM
                set n to create note from file theText ¬
                    title thename ¬
                    notebook EVnotebook ¬
                    created theDate
            end tell
           
            set theNum to theNum + 1
        end repeat
       
        if "GrowlHelperApp" is in processnames then
            tell application "GrowlHelperApp" -- GROWL SUCCESS
                notify with name ¬
                    "Success Notification" title ¬
                    "Import Success" description "Successfully exported " & theNum & ¬
                    " Notes to the " & EVnotebook & " notebook in Evernote" application name "MacJournal to Evernote"
            end tell
        else
            if "GrowlHelperApp" is in processnames then
                tell application "GrowlHelperApp" -- GROWL FAILURE FOR NO SELECTION
                    notify with name ¬
                        "Failure Notification" title ¬
                        "Import Failure" description ¬
                        "No headline or tab selected!" application name "MacJournal to Evernote"
                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 "Failed to export " & return & thename & ¬
                    "\"  due to the following error: " & return & errText ¬
                    application name "MacJournal to Evernote"
            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 *)
--EVERNOTE NOTEBOOK SELECTION SUBROUTINE
on Notebook_List()
    tell application "Evernote"
        activate
        set listOfNotebooks to {} (*PREPARE TO GET EVERNOTE'S LIST OF NOTEBOOKS *)
        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
        set Folders_sorted to my simple_sort(listOfNotebooks) (*SORT THE LIST *)
        set SelNotebook to choose from list of Folders_sorted with title "Select Evernote Notebook" with prompt ¬
            "Current Evernote Notebooks" OK button name "OK" cancel button name "New Notebook" (*USER SELECTION FROM NOTEBOOK LIST *)
        if (SelNotebook is false) then (*CREATE NEW NOTEBOOK OPTION *)
            set userInput to ¬
                text returned of (display dialog "Enter New Notebook Name:" default answer "")
            set EVnotebook to userInput
        else
            set EVnotebook to item 1 of SelNotebook
        end if
    end tell
end Notebook_List

--MACJOURNAL JOURNAL SELECTION SUBROUTINE
on Journal_List()
    tell application "MacJournal"
        activate
        every journal entry in (choose journal)
    end tell
end Journal_List

--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