There are quite a few things I love when it comes to my Mac. I love Spotlight. I love OpenMeta tagging. I love Evernote. I love being able to collect information from any source, and find anything I’ve saved, anywhere, no matter what program I used to create it. I especially love programs that allow me to accomplish that.

Unfortunately, one of my favorite apps right now, Delibar, doesn’t integrate with Spotlight or OpenMeta, despite the fact that it would be relatively easy to do. Delibar is an excellent (and sexy) menubar application for creating and searching Delicious (and Pinboard) bookmarks. A similar app, Pukka, pulls off the Spotlight part, but I still wanted OpenMeta tags that matched my Delicious tags for local searching.

I also use Tags.app for most of my tagging. OpenMeta tags allow you to group and classify files with simple tags, and provide a Spotlight-compatible way to search more intelligently. When you tag a website with Tags, it creates a webloc file in your metadata cache and applies the OpenMeta tags to it. I figured a similar approach would be feasible using AppleScript, and it seems to be working out great. Read on to see the script I’m using, and be sure to let me know if you improve on it!

What I’m doing is using curl with the Delicious API, and downloading everything that’s been bookmarked within the last hour. I’m running it with launchd every hour, so I don’t want to grab more than that every time it runs. It parses out the title, tags and url for the bookmark, saves it to a .webloc file, and tags it using Tags.app’s AppleScript commands. If you’re not running Tags, it could be modified pretty easily to work with the openmeta command line application.

I’m leaving the target folder up to the user, so when you define it in the CONFIG section, make sure it’s pointing to an existing folder. The only other config properties are your Delicious username and password (sent securely). As noted in the comments in the script, you can adjust the time/date it retrieves from in the first line after the CONFIG section.

The script is pretty well commented, so I won’t rewrite the whole thing here. Take a look, and then I’ll show you how I installed it.

-- CONFIG
property _user : "username" -- delicious username
property _pass : "password" -- delicious password
property targetFolder : "/Users/ttscoff/Library/Caches/Metadata/Delicious" -- POSIX path to existing folder
-- END CONFIG

set _date to do shell script "date -v-1d '+%Y-%m-%dT%H:%M:%SZ'"
-- returns a date 1 day ago in the format required by the twitter API.
-- you can adjust the date based on the frequency that you run the script
-- by editing the -v-1d part of the date command (-v-1H for 1 hour)
-- Note that the Delicious dates run on GMT, so you have to account for your time offset
-- For me, this means -v+4H if I want to go back one hour

set bookmarks to do shell script "curl https://" & _user & ":" & _pass & "@api.del.icio.us/v1/posts/all?fromdt=" & _date
-- gets the bookmarks starting with the date specified in _date

set _folder to POSIX file targetFolder as alias
-- turns the POSIX path in CONFIG into an AppleScript alias

tell application "System Events"
	set xmlDocument to make new XML data with data bookmarks
	-- create the XML object
	set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
	-- store the current delimiter and set the new one to a space for breaking up the tags
	repeat with _post in XML elements of XML element 1 of xmlDocument
		set _url to value of XML attribute "href" of _post as string
		set _tags to text items of (value of XML attribute "tag" of _post as string)
		set end of _tags to "delicious"
		set _title to value of XML attribute "description" of _post as string
		try -- it fails on some files, the try block lets us keep moving if we hit a problem
			tell application "Finder" to set webloc to make new internet location file to _url at _folder with properties {name:_title}
			-- makes a .webloc file in our target folder
			tell application "Tags" to apply tags _tags to files {POSIX path of (webloc as string)}
			-- adds OpenMeta tags to our newly created file
		end try
	end repeat
	set AppleScript's text item delimiters to astid
	-- restore the delimiters
end tell

Open this script in your Script Editor

Installing

The first thing I did was make a version of the script that excluded the date parameter (fromdt) in the curl call in order to download my entire history and process it so I was up-to-date. This took a while, but didn’t seem to cause any problems. The Delicious API is pretty quick to throttle you, so download the whole bunch in one API call or you’ll be in trouble.

After editing the config options, I saved the file as an Application Bundle (see addendum below) in my own apps directory (~/Applications). It doesn’t matter where you put it (or what you name it), as long as you know the path to get there. Then I set up a launchd plist to run it every 3600 seconds (hour). I highly recommend Lingon for editing all things launchd. It will make sense when you get there.

Addendum

In reference to the install procedure above, I’ve actually had more luck saving it as a text file to a scripts folder and running it with osascript through launchd. Here’s my launchd plist, which you can edit and load through the “Expert” section of Lingon’s editor.

Also, as I just added into the code comments above, I didn’t originally account for the fact that Delicious dates are in GMT, and my offset is -5 right now, so I have to use -v+4H to get 1 hour back, not -v-1H.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>org.brettterpstra.GetDelicious</string>
	<key>Nice</key>
	<integer>8</integer>
	<key>ProgramArguments</key>
	<array>
		<string>/usr/bin/osascript</string>
		<string>/Users/ttscoff/scripts/getdelicious</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>StartInterval</key>
	<integer>3600</integer>
</dict>
</plist>

Areas for improvement:

  • Currently errors out on some bookmarks for unknown reasons
  • Could have a first-run setting to automatically download and tag ALL bookmarks
  • Could set the created date of the webloc file based on the date of the bookmark from Delicious
  • Could provide more feedback or logging