Evernote Importer: TXT OR RTF FILE

Description

This script was originally published here as a downloadable app -- publishing the source code here in the Code Library just in case anyone wants to modify it. Note -- Evernote can import Text and RTF files natively and, generally speaking, the quality of their import process has been improving since this was first published. I'd recommend trying to add your text / rich text files normally before using this AppleScript.

FEATURES

This AppleScript takes advantage of the fact that Evernote is much better at working with HTML files than text or rich text. Once you have selected the files that you want to import, the app will convert them into HTML format and then batch import them into your default Evernote notebook. If you have GROWL installed, the app will notify you when the import is complete.

CURRENT LIMITATIONS AND OPEN ISSUES

It works only with RTF and Text files currently and no tagging or notebook selection is currently enabled.

The Code

(*
http://veritrope.com
Evernote Importer: TXT OR RTF FILE
Version 1.03
May 26, 2010

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/tips/evernote-importer-txt-or-rtf-files
           
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.03    TEMPORARY REMOVAL OF RTF/TXT VALIDATOR WHILE REWORKING CODE
1.02     BUG FIXES (UNDERLINING, GROWL APP)
1.01    ADDITIONAL TUNING
1.00    INITIAL RELEASE
*)


(*
======================================
//  PROPERTIES
======================================
*)

property isRunning : "false"
property theNum : "0"

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

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

tell application "Finder"
    try
        set exportedFiles to (choose file with prompt "Choose RTF or TXT Files (Multiple Selections Allowed)" with multiple selections allowed)
       
        repeat with exportedFile in exportedFiles
            set thefileName to the name of exportedFile
            set filePOSIX to quoted form of (POSIX path of exportedFile)
            set preHTML to (do shell script "textutil  -convert html -stdout " & filePOSIX & "")
            set firstScrub to my replaceString(preHTML, "<br>", "")
            set secondScrub to my replaceString(firstScrub, "<p class="p1">", "")
            set thirdScrub to my replaceString(secondScrub, "<p class="p2">", "")
            set fourthScrub to my replaceString(thirdScrub, "<p class="p3">", "")
            set fifthScrub to my replaceString(fourthScrub, "<p class="p4">", "")
            set sixthScrub to my replaceString(fifthScrub, "<p class="p5">", "")
            set seventhScrub to my replaceString(sixthScrub, "<span class="s1">", "<u>")
            set eighthScrub to my replaceString(seventhScrub, "</span>", "</u>")
            set theHTML to my replaceString(eighthScrub, "</p>", "<br>")
            tell application "Evernote"
                set importedNote to (create note with html theHTML title thefileName)
                set theNum to theNum + 1
            end tell
        end repeat
        (*NOTIFICATIONS*)
        if isRunning is true then
            my GrowlNotify(theNum)
        end if
        set theNum 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 "Import to Evernote"
                end tell
            else
                tell application "GrowlHelperApp" -- GROWL FAILURE FOR ERROR
                    notify with name ¬
                        "Failure Notification" title ¬
                        "Import Failure" description "Failed to export the item due to the following error:  " & errNum & " (" & errText & ¬
                        ")" application name "Import to Evernote"
                end tell
            end if
        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


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


--APP DETECT
on appIsRunning(appName)
    tell application "System Events" to (name of processes) contains appName
end appIsRunning

--GROWL
on GrowlNotify(theNum)
    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 & ¬
                " items to the default notebook in Evernote" application name ¬
                "Import to Evernote"
           
        else if Plural_Test is equal to 1 then
            notify with name ¬
                "Success Notification" title ¬
                "Import Success" description "Successfully exported " & theNum & ¬
                " item to the default notebook in Evernote" application name ¬
                "Import to Evernote"
        end if
    end tell
end GrowlNotify

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

3 Responses to “Evernote Importer: TXT OR RTF FILE”

  1. Mark Whipple June 22, 2011 at 5:56 pm #

    I did not want the extension included as part of the note title in Evernote, so I added the following line of code to your script:

    set thefileName to text 1 thru ((offset of "." in thefileName) - 1) of thefileName

    This code was placed immediately after the original line:

    set thefileName to the name of exportedFile

    Worked great. Thanks again!

  2. JMichael November 19, 2011 at 11:42 pm #

    Thanks for sharing.

    I opened in the Script Editor, and when I tried to save I got the msg “Locate GrowlHelperApp”.

    I can’t find “GrowlHelperApp”.

    Where is it?