Wishful Coding

Didn't you ever wish your
computer understood you?

Delicious bookmarks in Safari

I recently started using Delicious, and while they have a nice Firefox plugin and a bunch of bookmarklets, there is no easy way to get Delicious bookmarks into the Safari bookmarks bar. To get this done I wrote an Applescript with a little piece of Python to get Delicious bookmarks right in Safari. Here is how it looks: Delicious bookmarks in Safari Here is how to do it:
  1. Get the 2 scripts below and save them to a suitable location. You might want to place it in ~/Library/Scripts/Applications/Safari
  2. Edit the Applescript with the location of the Python script and the URL of your Delicious feed.
  3. Run the Applescript.
  4. Drag the resulting bookmarks folder or its content anywhere you want.
This will work for any RSS and maybe even Atom feed, you could even bookmark the feed of your Twitter account, for... *hum* easy access to individual tweets. This is the Python code:
import feedparser
print "<dl>"
for e in feedparser.parse( "http://feeds.delicious.com/v2/rss/pepijndevos" ).entries:
    print '<dt><a href="%s">%s</dt>' % (e.link, e.title.encode('ascii','ignore'))
print "</dl>"
Don't ask me why, but Safari needs broken html to import. Maybe because it is meant to import IE bookmarks? Anyway, this is the Applescript, the real thing:
do shell script "python ~/bin/feed2html.py > ~/Documents/delicious.html"
tell application "Safari"
        tell application "System Events"
                tell application process "Safari"
                        click menu item "Import Bookmarks…" of menu "File" of menu bar item "File" of menu bar 1
                        keystroke "g" using {shift down, command down}
                        keystroke "~/Documents/delicious.html"
                        keystroke return
                        keystroke return
                end tell
        end tell
end tell