Automatic upload of Safari bookmarks

I use Safari as my primary browser on my laptop, but often browse the web from other computers, and I like having my bookmarks up to date. This describes how I use launchd to update my website whenever I change my bookmarks in Safari.

Generating Links page

So the first step is the script which grabs the bookmarks from Safari and converts them into a webpage. For this I have a script bookmarks.py which reads the ~/Librfary/Safari/Bookmarks.plist and formats into HTML. This script relies on the Python<->Objective-C bridge to parse the bookmarks file.

Upload script

I have a simple one-line shell script which runs the runs bookmarks.py and uploads the result to my website:

/Users/benno/local/bin/bookmarks.py | ssh benno.id.au "cat > public_html/links.pml"

Setting up launchd

launchd is the new uber-daemon in Tiger, which allows users to register their own tasks to be run automatically. There are various triggers that can be used to fire off a task, but the one we are interested in here is WatchPaths, which fires when a particular path is modified.

User tasks are stored in ~/Library/LaunchAgents. I created a file au.id.benno.bookmarker:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">
<dict>
        <key>Label</key><string>au.id.benno.bookmarker</string>
        <key>OnDemand</key><true/>

        <key>Program</key>
        <string>/Users/benno/local/bin/update_bookmarks</string>

        <key>WatchPaths</key>
        <array>
        <string>/Users/benno/Library/Safari/Bookmarks.plist</string></array>
</dict>

</plist>

The Label field identifies the task. We use the reverse dotted notation to avoid any possible namespace clashes. The WatchPath key specifies a list of paths to watch, in this case we care about Safari's Bookmarks.plist. Finally the Program specifies what to run when the event is triggered, in this case the shell script shown above.

Conclusion

Launchd makes it simple to automate tasks on OS X, and when combined with PyObC allows for some powerful scripts to be written.