Link List Post with Jekyll and Applescript

March 13, 2010

In the previous post, I mentioned that I hacked together a somewhat automated process to create a linked list style post. The following AppleScript is what I came up with:

on displayErrorMessage(s)
    display dialog (s) buttons {"OK"} default button "OK" with icon caution
end displayErrorMessage

tell application "Safari"
    try
        set pageURL to URL of document 1
        set pageTitle to name of document 1
        set selectedText to do JavaScript "getSelection();" in document 1
    on error errorMessage
        displayErrorMessage("Can’t create a new post because of an error getting information from Safari.") of me
        return
    end try
end tell

property LF : ASCII character 10

set modTitle to text returned of (display dialog "Modify the title:" with title "Modify Title" default answer pageTitle buttons {"OK"} default button 1)

tell application "System Events"
    set the clipboard to "[" & pageTitle & "]" & "(" & pageURL & ")" & LF & LF & ">" & selectedText & LF & LF
end tell

do shell script "ruby ~/bin/newlink.rb " & quoted form of modTitle

Here is a breakdown of how the script works.

on displayErrorMessage(s)
    display dialog (s) buttons {"OK"} default button "OK" with icon caution
end displayErrorMessage

tell application "Safari"
    try
        set pageURL to URL of document 1
        set pageTitle to name of document 1
        set selectedText to do JavaScript "getSelection();" in document 1
    on error errorMessage
        displayErrorMessage("Can’t create a new post because of an error getting information from Safari.") of me
        return
    end try
end tell

This first section, which I copied from the New Post from Safari script included with MarsEdit, grabs the URL, Title, and selected text from Safari and assigns them to the indicated variables. This is the essential part of the process, and if this was all the script did, it would be a big timesaver. But, I wanted to see if I could get more out of it.

set modTitle to text returned of (display dialog "Modify the title:" with title "Modify Title" default answer pageTitle buttons {"OK"} default button 1)

This bit of code will open a dialog box asking to modify the page title. Some page titles are lengthy, and this bit allow me to shorten it up to what I want.

tell application "System Events"
    set the clipboard to "[" & pageTitle & "]" & "(" & pageURL & ")" & LF & LF & ">" & selectedText & LF & LF
end tell

This part copies a Markdown formatted link to the clipboard. It includes an href to the article as well as a blockquote of the selected text.

Now that I've got what I want to include in the link list in the clipboard, the next step is to create a new post in the Jekyll system I have setup. Thanks to this bit of Ruby code, compliments of Alex Payne, a shell script call to the Ruby progarm will create the file with the necessary markup.

do shell script "ruby ~/bin/newlink.rb " & quoted form of modTitle

The next step is to paste in the clipboard data and type any other comments I have.

It may seem like a lot of effort, but it only takes a few seconds to generate the link list item. I'm sure there is a more efficient way to do this, and I'm looking forward to figuring one out.