NetNewsWire to Evernote — The Batch Send Applescript

Change Log:

  • 1.10 (October 13, 2009) **EXPORT URL LIST, BUG FIXES, UPDATED TAGGING, REMOVED EXTRA CONFIRMATIONS
  • 1.01 (October 27, 2008) ADDED SOURCE URL TO NOTES
  • 1.00 (October 14, 2008) INITIAL RELEASE OF SCRIPT

AppleScript ninjas — dive in to the code and, if you have any suggestions, leave them 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 cannot 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 us *how* you use these scripts in your workflow, how you think they could be improved, and feel free to help your fellow ninjas if they don’t understand something!

(*
http://veritrope.com
Batch Send -- NNW to Evernote
Version 1.1
October 13th, 2009

Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/tips/netnewswire-to-evernote-batch-send-applescript

This script is primarily an adaptation of Daniel N. Byler's excellent DEVONthink script.
http://bylr.net/3/2008/06/archive-newsfeeds-in-devonthink-pro-via-netnewswire/

Installation: Just drop it into the NetNewsWire Script Folder!

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/NetNewsWire
--Set up your keyboard shortcut

CHANGELOG:
1.10       EXPORT URL LIST, BUG FIXES, UPDATED TAGGING, REMOVED EXTRA CONFIRMATIONS
1.01    ADDED "SOURCE URL" FUNCTIONALITY
1.00    INITIAL RELEASE

*)
property showAlert : true --if true, will display success/failure alerts
property maxPause : 1
property criteriaList : {"All Items in Folder", "Flagged Items Folder", "Unread Items in Folder", "URLs of Tabs"}
property successCount : 0
property selectCriterion : ""
property every_headline : ""
property every_URL : ""
property abortScript : false
property useGrowl : ""
property growlAppName : "NetNewsWire to Evernote"
property allNotifications : {"General", "Error"}
property enabledNotifications : {"General", "Error"}
property iconApplication : "Evernote.app"
property NNWTag : ""
property NNWTitle : "URL LIST"
property NNWURL : ""
property NNWBody : ""
property NNWTabs : {}
property EVnotebook : ""
property postaction : ""


-- CHECK FOR GROWL
tell application "System Events"
    set processnames to name of every process
end tell
if "GrowlHelperApp" is in processnames then
    set useGrowl to true
    tell application "GrowlHelperApp"
        register as application growlAppName all notifications allNotifications ¬
            default notifications enabledNotifications icon of application iconApplication
    end tell
end if


(*MAIN PROGRAM *)
my main()

on main()
    set oldDelims to AppleScript's text item delimiters
    tell application "NetNewsWire"
        activate
        my chooseArchiveCriterion()
        if selectCriterion is "false" then
            return
        end if
        my selectMatchingArticles(selectCriterion)
        set the_count to (count of every_headline) + (count of every_URL)
        if the_count is 0 then
            set abortScript to true
            display dialog "No valid content selected." with title "NetNewsWire to Evernote" buttons {"OK"} default button 1 ¬
                with icon path to resource "appIcon.icns" in bundle (path to application "NetNewsWire")
            return
        end if
        set postaction to my choosePostAction(selectCriterion)
        if postaction is "false" then
            return
        end if
        activate
        (* DEACTIVATED WARNING -- DELETE COMMENT MARKERS TO REACTIVATE
        my warnArchiveTotal(every_headline)
        *)

        --"TAGGING AND BAGGING" SUBROUTINE
        my TagandBag(EVnotebook)
        activate
        if every_headline is not "" then
            my alertStart(every_headline, NNWTag, EVnotebook)
            repeat with i in every_headline
                my subArchiveMain(i, NNWTag, EVnotebook)
                my subPostAction(i, postaction)
                --  my pauseScript()
            end repeat
        else if every_URL is not "" then
            set i to every_URL
            my subArchiveMain(i, NNWTag, EVnotebook)
            set successCount to the_count
        end if
        activate
        my alertFinish(the_count, EVnotebook, successCount)
        set successCount to 0
    end tell
end main


(* SUBROUTINES *)
on chooseArchiveCriterion()
    tell application "NetNewsWire"
        activate
        set selectCriterion to ¬
            (choose from list criteriaList default items "All" with title "NetNewsWire to Evernote" with prompt "Archive which items?") as string
        return
    end tell
end chooseArchiveCriterion

on selectMatchingArticles(selectCriterion)
    tell application "NetNewsWire"
        if selectCriterion is "All Items in Folder" then
            set every_headline to every headline of selectedSubscription
        else if selectCriterion is "Flagged Items Folder" then
            set every_headline to every headline of (first subscription whose display name is "Flagged Items")
        else if selectCriterion is "Unread Items in Folder" then
            set every_headline to every headline of selectedSubscription whose isRead is false
        else if selectCriterion is "URLs of Tabs" then
            set every_URL to URLs of tabs
        end if
        if every_headline is not "" then
            return every_headline
        else
            return every_URL
        end if
    end tell
end selectMatchingArticles

on choosePostAction(selectCriterion)
    tell application "NetNewsWire"
        if selectCriterion is "All Items in Folder" then
            set postaction to (choose from list {"Unflag", "Mark read", "None"} ¬
                default items "None" with title "NetNewsWire to Evernote" with prompt "Post-Archive Action?") as string
        else if selectCriterion is "Flagged Items Folder" then
            set postaction to (choose from list {"Unflag", "None"} ¬
                default items "None" with title "NetNewsWire to Evernote" with prompt "Post-Archive Action?") as string
        else if selectCriterion is "Unread Items in Folder" then
            set postaction to (choose from list {"Mark read", "None"} ¬
                default items "None" with title "NetNewsWire to Evernote" with prompt "Post-Archive Action?") as string
        end if
    end tell
    return postaction
end choosePostAction

on subArchiveMain(i, NNWTag, EVnotebook)
    tell application "NetNewsWire"
        try -- HACK FOR MISSING BODY TEXT IN SOME ITEMS!
            set NNWTitle to title of i
            set NNWURL to URL of i
            if (description of i) exists then
                set NNWBody to (description of i)
            else
                set NNWBody to (summary of i)
            end if
        end try

        -- CREATE THE NOTE FROM HEADLINE
        tell application "Evernote"
            try
                if EVnotebook is "" and NNWTitle is not "URL LIST" then
                    set n to create note with html NNWBody title NNWTitle tags NNWTag
                    set source URL of n to NNWURL
                    set successCount to successCount + 1

                else if EVnotebook is not "" and NNWTitle is not "URL LIST" then
                    set n to create note with html NNWBody title NNWTitle notebook EVnotebook tags NNWTag
                    set source URL of n to NNWURL
                    set successCount to successCount + 1

                else
                    tell application "NetNewsWire"
                        set export_folder to POSIX path of (path to desktop folder as text)
                        set t to export_folder & "tabs.html"
                        export tabs as HTML to file t
                        set NNWBody to POSIX path of t
                        set NNWTitle to "Exported Tabs From NetNewsWire"

                        tell application "Evernote"
                            set n to create note from file NNWBody title NNWTitle tags NNWTag
                        end tell

                        tell application "Finder" to delete file t

                    end tell --NNW
                end if
            end try
        end tell --EV
    end tell --NNW

end subArchiveMain

on subPostAction(i, postaction)
    tell application "NetNewsWire"
        if postaction is "Unflag" then
            set isFlagged of i to false
        else if postaction is "Mark read" then
            set isRead of i to true
        end if
    end tell
end subPostAction

on pauseScript()
    set rn to (random number (maxPause)) as text
    delay rn
end pauseScript

--"TAGGING AND BAGGING" SUBROUTINE
on TagandBag(EVnotebook)
    tell application "NetNewsWire"
        set defaultTag to (display name of selectedSubscription) --DEFAULT TAG IS SELECTED SUBSCRIPTION NAME/FOLDER
        display dialog "" & ¬
            "Enter Your Tags Below:" & return & return & "     • Multiple Tags Separated By Colons or Commas" & return & return & "     • Default Tag is The Highlighted Subscription Folder." with title "NetNewsWire to Evernote" default answer defaultTag buttons {"Send to Default Notebook", "Select or Create Notebook", "Cancel"} default button "Send to Default Notebook" cancel button ¬
            "Cancel" with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
        set dialogresult to the result
        set tagText to text returned of dialogresult
        set ButtonSel to button returned of dialogresult
        set theDelims to {":", ","}
        set NNWTag to my Tag_List(tagText, theDelims)
        if ButtonSel is "Select or Create Notebook" then set EVnotebook to my Notebook_List()
    end tell
end TagandBag

--TAG SELECTION SUBROUTINE
on Tag_List(userInput, theDelims)
    set oldDelims to AppleScript's text item delimiters
    set theList to {userInput}
    repeat with aDelim in theDelims
        set AppleScript's text item delimiters to aDelim
        set newList to {}
        repeat with anItem in theList
            set newList to newList & text items of anItem
        end repeat
        set theList to newList
    end repeat
    return theList
    set AppleScript's text item delimiters to oldDelims
end Tag_List

--EVERNOTE NOTEBOOK SELECTION SUBROUTINE
on Notebook_List()
    tell application "Evernote"
        activate
        (*GET THE NOTEBOOK LIST *)
        set listOfNotebooks to {}
        set EVNotebooks to every notebook
        repeat with currentNotebook in EVNotebooks
            set currentNotebookName to (the name of currentNotebook)
            copy currentNotebookName to the end of listOfNotebooks
        end repeat

        (*SORT THE 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 "NetNewsWire to Evernote" with prompt ¬
            "Current Evernote Notebooks" OK button name "OK" cancel button name "New Notebook"

        (*CREATE NEW NOTEBOOK OPTION *)
        if (SelNotebook is false) then
            set userInput to ¬
                text returned of (display dialog "Enter New Notebook Name:" default answer "" with title "NetNewsWire to Evernote" with icon path to resource "Evernote.icns" in bundle (path to application "Evernote"))

            set EVnotebook to userInput
        else
            set EVnotebook to item 1 of SelNotebook
        end if
    end tell
end Notebook_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


on warnArchiveTotal(every_headline)
    tell application "NetNewsWire"
        activate
        set result to display dialog "You're trying to archive " & return & (count of every_headline) & ¬
            " articles. Continue?" buttons {"Cancel", "Continue"} giving up after 60 default button 2 with title "NetNewsWire to Evernote" with icon path to resource "appIcon.icns" in bundle (path to application "NetNewsWire")
        if button returned of result is equal to "Cancel" then
            set abortScript to true
            return
        end if
    end tell
end warnArchiveTotal

on alertStart(the_count, NNWTag, EVnotebook)
    activate
    if the_count is 0 then
        set alertName to "Error"
        set alertTitle to "Script failure"
        set alertText to "No valid content to archive"
        my notify(alertName, alertTitle, alertText)
        return

        (* DEACTIVATED ALERT -- DELETE COMMENT MARKERS TO REACTIVATE
        else
        activate
        set alertName to "General"
        set alertTitle to "Export to Evernote"
        set alertText to "Attempting to archive " & (count of every_headline) & " headlines into " & EVnotebook
        my notify(alertName, alertTitle, alertText)
        return
        *)

    end if
end alertStart

on alertFinish(the_count, EVnotebook, successCount)
    activate
    if successCount is 0 then
        set alertName to "Error"
        set alertTitle to "Script failure"
        set alertText to "Error: No Items Archived"
        my notify(alertName, alertTitle, alertText)
        return
    else
        set alertName to "General"
        set alertTitle to "Export to Evernote Successful!"
        set alertText to ¬
            (successCount & " of " & the_count & " Items Archived!") as string
        my notify(alertName, alertTitle, alertText)
        return
    end if
end alertFinish

on notify(alertName, alertTitle, alertText)
    if showAlert is false then
        return
    else if useGrowl is true then
        tell application "GrowlHelperApp"
            notify with name alertName title alertTitle application name "NetNewsWire to Evernote" description alertText
        end tell
    else
        display dialog alertText with icon path to resource "appIcon.icns" in bundle (path to application "NetNewsWire")
    end if
end notify

DOWNLOAD THE SCRIPT FILE HERE

Did this script help you out? Feel free to make a donation!


Found a bug in the script?

Click Here To Submit Your Bug Report!

Interested in what other scripts are available for NetNewsWire and Evernote? Click here to see the latest list!

22 Responses to “NetNewsWire to Evernote — The Batch Send Applescript”

  1. Dan Byler October 26, 2008 at 11:54 am #

    Justin,

    You beat me to it! And very glad you did – Evernote badly needed some extensibility. Also, I love the Growl detection – I may have to add that to my DEVONthink script.

    All the best,
    Dan

  2. Justin October 26, 2008 at 2:14 pm #

    Thanks Dan!

    Play around with it for a bit and let me know what you think — your input would be especially appreciated.

    And be sure to try the FastScripts shortcuts…makes things a little more fast and, dare I say it, fun!

  3. wow March 25, 2009 at 1:14 pm #

    Thanks .Really awesome .God bless ya

  4. Rob Roach October 6, 2009 at 1:26 pm #

    I keep getting an error Evernote got an error: Operation would exceed monthly upload allowance. But I signed up for premium, and evernote web and evernote mac desktop know now that I am premium and am nowhere near upload limit. Your script is a lifesaver, if I can only get it to work around this problem. Any ideas where to look?

    Thanks,

    Rob

    • Justin October 6, 2009 at 5:31 pm #

      Hi Rob,

      Glad you’re enjoying the script!

      Let’s try to eliminate a few things so we can find out what’s going on:

      1.) Be sure to empty the trash in Evernote; I believe that it also counts against the monthly quota, so let’s eliminate it as a cause of the message.

      2.) Try moving the NNW items into a Local (not Synchronized) notebook with the script and then moving the items into a synchronized folder. Does doing it this way also give you an error message?

      Let me know what happens after you’ve tried these two things and we’ll take it from there.

      • David October 11, 2009 at 2:26 am #

        Oops, I’m in the same boat: premium acct and same error msg. Emptying Evernote’s trash and moving NNW items into a Local notebook didn’t make any difference.

        I’m using Evernote 1.4.9 and NNW 3.2

        Any help would be appreciated.

        And thanks for all your Evernote applescript work! You’re helping a ton of people, I’m sure.

        • Justin October 11, 2009 at 3:08 am #

          Hi David,

          I am just finishing up some revisions to the DEVONthink/Evernote scripts, but I will tackle this next!

          Check back later this week for updates and, in the interim, let me know if you discover any other “common denominator” associated with this error.

          • David October 11, 2009 at 4:17 am #

            Hi Justin,

            I just tried the script two more times and had partial success. Both attempts got about half way through my 125 flagged items before getting “Error number: -1712 Message: Evernote got an error: AppleEvent timed out.”

            I don’t have good ideas on why I didn’t get the “exceed allowance” error I had earlier. Hard to tell since I changed a number of things including quitting many open applications. I also disconnected from the internet for one attempt (and ended up with no images in the newly created notes).

            Your “single” script has worked fine throughout, so I may end up using that instead of the “batch” one. Nice to have a fallback.

            BTW, I may contact you later to ask about a script to change the creation year on a notebook’s worth of notes (long story).

            Thanks again for all your efforts!

  5. Justin October 11, 2009 at 11:06 am #

    I am definitely going to spend some time on this script this week — it’s been a year since I posted it and I have learned a few things about AppleScript since then. 😀

    Also, thanks for your detective work… I think I *may* have identified the cause, but will test over the next few days to be sure.

    If and when you (or anyone else) would like to talk about commissioning a custom script, just use the Contact page!

    • Rob Roach October 11, 2009 at 3:09 pm #

      thanks Justin,

      I tried, still stuck. An email when/if you have a new script would be mighty kind–thanks for looking at this.
      Rob [email protected]

  6. Joe Chiarelli October 15, 2009 at 9:03 am #

    Great script! Make my life so much easier.
    The only thing i need to NNW to sync with Starred items or get a script to export my InstaPaper stuff to Evernote!

    Ether way would make memory clippings easier on the iphone!

    • Justin October 15, 2009 at 10:59 am #

      Joe,

      From the NNW Website:
      “Flagged items should now be in sync with starred items (both directions). One limitation, however — in NetNewsWire it only shows flagged items if the item appears in a feed you’re subscribed to.”

      Tried it out with my iPhone — works fine!

  7. Tommy November 1, 2009 at 5:17 pm #

    Great script! I’ve been trying to use it with my starred items in Google Reader to get them into evernote. It worked well for a little bit, but it seems that it’s taking google a while to update my starred items public feed so I guess I’ll have to do a little bit each day.

  8. Jim Shook November 29, 2010 at 12:36 pm #

    Hi, awesome script! I would like to tell the script to only pull in text (no images). Any thoughts on how to achieve this in the script? Would be super helpful. Thanks!

  9. Matthew Bennett June 3, 2013 at 4:56 am #

    This is great, thanks, especially when you change ‘unread’ to ‘read’. Then you can just scroll down NNW, click on the headlines and send all the ones you’ve read to Evernote. A very powerful script. Thanks.

  10. Justin F April 11, 2014 at 1:16 am #

    Thank you so much for this. For ages it seems, I’ve been looking for a way to move from WordPress to Evernote. Now, if only I could transfer categories, tags and dates from WordPress to Evernote.