Automating link post with Evernote and AppleScripts

Recently I switch back to Postach.io as my blogging platform. Because…. because it’s cool, and it just works.

The only things I’ve added to my workflow is an easy way to blogging links. Of course, the Evernote Web Clipper is cool and, finally, with iOS8 we have it also on mobile, but when I post about a link I found, I like to keep a standard structure in the link:

  • Every link post is a Markdown note (this is true for all my blog post), so is tagged with the “Markdown” tag
  • Every link post have the “Link” tag
  • The post title is set as the Page title plus the arrow symbol →
  • The first line in the post is the string “Source” which is a link to the original source
  • Sometimes I quote a phrase from the source

This kind of operations makes a little tedious making new link notes so, why not automate this? (I love the automations)

Here we are, make a new Automator workflow which is available only in Safari

New Automator Workflow

Then put in it the “Run AppleScript” action and fill with this code:

on run {input, parameters}
    
    tell application "Safari"
        set theURL to URL of front document
        set theTitle to name of front document
        set theSelection to (do JavaScript "(''+getSelection())" in document 1)
        if contents of theSelection is not "" then
            set theQuote to "> " & theSelection
        else
            set theQuote to "
"
        end if
    end tell
    
    set noteUrlString to "[Source](" & theURL & ")"
    set noteTitle to theTitle & " →"
    set noteText to "[Source](" & theURL & ")

" & theQuote
    
    tell application "Evernote"
        set blogNotebook to notebook "Postach.io"
        
        set newNote to create note title noteTitle with text noteText notebook blogNotebook
        
        set markdownTag to tag "Markdown"
        set linkTag to tag "Link"
        assign {markdownTag, linkTag} to newNote
        
        open note window with newNote
        activate
    end tell
    
    return input
end run

Hit Save and move the .workflow directory in the ~/Library/Services directory; it will appear in the Safari->Services menu.

I’ve also made a bind to this menu entry with a keyboard shortcut so I can, during the browsing, press the combination Ctrl+Alt+Cmd+A and make a new note, already formatted and tagged, with the source link and all stuff I need usually.

Of course, if before launch this Service I select text in the current Safari webpage, I can find this test as a quote (already Markdown formatted).

Enjoy

Written on September 22, 2014 - #Automation #Evernote #Mac