Del.icio.us, Pukka and (more) AppleScript

Recently I’ve become much enamoured with AppleScript. All those little things you wish you could simplify or do automatically can be made a reality with this wonderful tool.

I’ve also started using del.icio.us after much procrastinating; I’ve been meaning to store and tag my bookmarks somewhere other than on my browser toolbar as they are starting to get a little unmanageable.

In my quest to further remove my need for the mouse, I looked into a handy app to add to my del.icio.us bookmarks straight from Safari. I couldn’t quite find what I was looking for. I did find a very simple but very neat app for creating bookmarks which can be scripted using AppleScript: Pukka.

I wanted to substitute Safari’s Add Bookmark cmd-d shortcut to create a del.icio.us entry and ask what tags to assign. Again, the power of Quicksilver and AppleScript come to my rescue.

The following AppleScript takes the current page in safari and uses Pukka to send the link to del.icio.us. If there is any text selected it gets added as the description, as does the referrer. The slight twist is that it uses some javascript to ask what tags to assign to the page.

The script below is based on a script by Pukka’s creator.

set account to "Splee"

to split(someText)
    set AppleScript's text item delimiters to space
    set someText to someText's text items
    set AppleScript's text item delimiters to {""} --> restore delimiters
    return someText
end split

tell application "Safari"
    set myURL to URL of document 1
    set myTitle to do JavaScript "document.title" in document 1
    set myReferrer to do JavaScript "document.referrer" in document 1
    set mySelection to do JavaScript "window.getSelection() + ''" in document 1
    set rawTags to do JavaScript "prompt('Enter tags:')" in document 1
end tell

set myTags to split(rawTags)

tell application "Pukka"
    set post url to myURL
    set post title to myTitle
    set post tags to myTags
    if (length of mySelection is not 0) and (length of myReferrer is not 0) then
        set post description to mySelection & " (linked via " & myReferrer & ")"
    else if (length of myReferrer is not 0) then
        set post description to "linked via " & myReferrer
    else if (length of mySelection is not 0) then
        set post description to mySelection
    end if
    set selected account to account
    add link
end tell

The tags you enter in the javascript prompt should be separated by spaces.

You’ll need to change the account variable to your del.icio.us account as set up in Pukka, but other than that it’s ready to go.

I then set up another trigger in Quicksilver to launch the script using cmd-d with a scope of Safari so as not to overlap with other app’s shortcuts. Voila. That is as near as it gets to Safari integration with del.icio.us from what I can find.