David Kendal’s Weblog

A Damn Fine Web Publication

Post to Chyrp From NetNewsWire with AppleScript

Most things I read on the Web, I read in the excellent NetNewsWire by Brent Simmons. I have found, though, that its ‘post to weblog’ feature is rather anemic, only supporting external applications like MarsEdit. No external weblog editor yet has good enough support for Chyrp’s feathers system, in particular the ability to post links, something I like to do from time to time here.

So I’ve made an AppleScript that pops up a browser window with the same bookmarklet interface you get in your web browser, setting the Title and URL elements as appropriate.

tell application "NetNewsWire"
  if selectedHeadline exists then
    set postURL to the URL of the selectedHeadline
    set postTitle to the title of the selectedHeadline

    set postResolvedURL to do shell script "curl -LIs " & quoted form of postURL & " | grep ^Location: | tail -n1 | sed -E 's/^Location: (.*)/\\1/'"

    if (postResolvedURL is not "") then
      set postURL to postResolvedURL
    end if
  else
    display alert "Couldn't post to Chyrp" message "No headline selected to post to weblog." as warning
    return
  end if
end tell
set theURL to "http://dpk.org.uk/admin/?action=bookmarklet&url=" & postURL & "&title=" & postTitle
do shell script "open " & quoted form of theURL

Obviously you’ll want to change http://dpk.org.uk/admin/ to the URL of your own Chyrp admin panel. (usually the address of your blog followed by /admin/ — don’t change anythin after and including the question-mark)

I’ve got a bunch of ideas for improving this script. For now, I’d like to publicly urge Brent Simmons to alter his ‘Post to Weblog’ feature to allow blog posts to be handled by AppleScript as well as application bundles. (not that it’s a serious oversight—I think this may be the first AppleScript enabling this functionality)

Update 10/05/10: Script altered to show an error message if no article is selected.

Update 16/06/10: Script altered to always use default browser using the open shell command provided with Mac OS X. Instructions for configuring this to work with different browsers have been removed.

Update 29/08/10: Script altered to resolve all redirects in the URL of the selected post, thus ensuring the removal of insipid redirects via various feed-proxy sites, which links are more likely to break than a real permalink. It does this using some command-line trickery.