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.
Tim Stoop | 24-Aug-06 at 10:02 pm | Permalink
This might be a stupid question, but how do I “install” the script? Do I save it to a file or what?
Splee | 24-Aug-06 at 11:03 pm | Permalink
Yes. If you open Script Editor and paste the above script in, click “Compile” to make sure that no errors have crept in during the copy/paste process.
I launch this particular script using a Quicksilver trigger, but you can install it in your Scripts directory and use the Script menubar item if you prefer.
Alex | 01-Sep-06 at 8:29 am | Permalink
This is terrific. I’ve been keeping my eyes open for a script that will post to del.icio.us via pukka from safari, camino, firefox, omniweb–basically any browser. I just tried frankensteining this script with this one, but can’t make that work. (I don’t quite get applescript yet..) Anyway, thanks for this one.
Splee | 01-Sep-06 at 12:19 pm | Permalink
As far as I’m aware, Safari is the only browser that allows you to execute JavaScript via AppleScript.
Almost all the functionality of this script relies on JS to pull the data from the browser, including the dialogue box that prompts you for your tags. I don’t think that you’ll be able to get the same result from, say, Firefox or Camino.
I’d love to be proven wrong on that front though.
Adam | 22-Nov-06 at 8:00 pm | Permalink
Thanks for the great example! I have modified it to better fit my workflow and posted at
http://www.adamia.com/tips/post-to-delicious-from-safari
Al | 12-May-07 at 10:32 pm | Permalink
Apparently Javascript is supported by Camino, as referenced in the link below.
http://www.caminobrowser.org/support/docs/annoyances/
Substituting “Camino” for “Safari”
in the script results in a failed
compile. Anyone know how to convert
this script to work for Camino?
Ulrich Maasmeier | 15-Jul-07 at 10:17 pm | Permalink
Hello. Nice thing.
But as im not a programmer, and just want things to get done easily, my shortcut is here
Install Pukka
Install the Pukka bookmarklet (javascript code) through it´s menu “Pukka” > “Install Bookmarklet…”
Assign a shortcut to this bookmarklet
a - Opera: via organizing bookmarks, Settings: Assign a letter or two (e.g. “h” or “hh”, you´ll see later, why!)
b - Safari: Show up your new Bookmarklet-Bookmark in your Bookmarks Bar (first 9 ones get shortcuts according to their listing)
go ahead! To your Browser:
a - all browsers: select somer descriptive text
b - Opera: hit the “Address bar shortcut”: h (now you´re in there!)
b - Safari: hit your shortcut (e.g. Command-1) - You´re done!
Was that shorter and better? Please tell me…
Uli, Germany, near the Lake of Constance