Hypertext Blogging With Textmate And Using Applescript And Javascript To Ease The Pain

Hypertext Blogging With Textmate And Using Applescript And Javascript To Ease The Pain

Justin Blanton:

Doing all of my blogging with TextMate these past few weeks has been great, except for one terribly annoying thing, namely the inability to quickly conjure up a “linked-list”-type post. I was having to manually — gasp — create these (usually) quick, simple posts … MarsEdit has spoiled me over the years with its simple bookmarklet that grabs the data I need (i.e., page title, page URI and any currently-selected text), drops it into a new post and populates the Title field with the title of the page. Since I’ve made the switch to a homegrown site, I’ve started experiencing exactly what Jason describes. I need a way to quickly create a link post and have missed the ease that MarsEdit provides. Jason’s method involves an AppleScript which executes some Javascript, opens a new document in TextMate, and populates it with the URL, title, and selected text from a web page. It’s quite brilliant. His script works fine except for one thing: it doesn’t pull the selected text. Rather than putting the selected text in the document, the AppleScript would spit out the following:

missing value After some digging around, I found this thread in Apple’s Support Forum which identifies the problem and gives several solutions. The best solution is given by Annapolitan and involves replacing this line from Justin’s code set selectedText to (do JavaScript “(getSelection())” in document 1) with this: set selectedText to (do JavaScript “var x = ‘‘+document.getSelection();x” in document 1) It works great. Following Justin’s lead, I configured FastScripts to execute the script when I hit ⌘-b. Here’s my completed code. You can modify the set post lines to build out your template as needed. tell application “Safari” set selectedText to (do JavaScript “var x = ‘‘+document.getSelection();x” in document 1) as string set pageURI to (get URL of document 1) set pageTitle to (do JavaScript “document.title” in document 1) end tell property LF : ASCII character 10 tell application “TextMate” activate open “/path/to/local/file” set post to “Quick Link” & LF & LF set post to post & “[” & pageTitle & “]” set post to post & “(“ & pageURI & “)” & LF & LF set post to post & “> “ & selectedText & LF insert post end tell