Script for posting DEVONthink records to delicious

I’ve come up with a rough solution to the problem of posting DEVONthink records to your delicious account, posed in the following linked forum posts:

Posting tags (or folders) to delicious
Using with Delicious

I cobbled together a AppleScript program, based on stuff I’ve read here and on other sites, that extracts metadata from selected DEVONthink records and then does a shell script that accesses delicious’s API and posts a bookmarks populated with the DEVONthink record’s metadata.

The API call statement to post a bookmark is essentially a long string. So putting together the string just involved extracting the relevant data from a selected DEVONthink record and setting them as AppleScript variables to pass to shell.

I am far from being a programmer of any sort so any comments or suggestions would be much appreciated.

Here’s the code for the script.


-- Posting DEVONthink Pro records to delicious.com.
-- Last modified August 31, 2010 11:12 AM EDT.
-- Takes selected DEVONthink records and loops through each item and posts them to your delicious.com account.
-- Before using make sure to change "username" and "password" in the property list to your delicious login credentials. Keep the enclosing quotation marks.

property del_username : "username"
property del_pwd : "password"

tell application id "com.devon-technologies.thinkpro2"
	
	set thisSelection to the selection
	if thisSelection is {} then error "Please select one or more DEVONthink records."
	repeat with thisItem in thisSelection
		
		-- I. Set delicious tags.

		set defaultTag to "via:DEVONthink"
		-- The default tag if the DEVONthink record has no tags. Feel free to delete or change. If you delete it, make sure to also the delete the corresponding else statement below.
		
		if exists tags of thisItem then
			set _tags to (tags of thisItem) as string
		else
			set _tags to defaultTag
		end if
		
		-- II. Set delicious URL field.
		
		if URL of thisItem is not missing value and URL of thisItem is not "" then
			set _URL to (URL of thisItem) as string
		else
			set _URL to "x-devonthink-item://" & (uuid of thisItem) as string
		end if
		
		-- III. Set delicious Title field.
		
		set _description to (name of thisItem) as string
		
		-- IV. Set delicious Notes field.
		
		set _extended to (comment of thisItem) as string
		
		-- Instead of comment, you can set this to the plain text of the record. But if the record has more than 1000 characters, which is the limit of the delicious bookmark's notes field, the API returns an error. A possible workaround would be to have the script count how many characters there are in the record to determine whether to set the variable to the comment or the plain text of the DEVONthink record.
		
		-- V. Set delicious login credentials.
		
		set usernamePasswordString to del_username & ":" & del_pwd
		
		-- VI. Set the delicious API call statement.
		
		set u to "\"?&url=" & _URL & ¬
			"&description=" & _description & ¬
			"&tags=" & _tags & ¬
			"&extended=" & _extended & ¬
			"&shared=no" & ¬
			"&replace=yes" & "\""
		
		set curlStatement to "/usr/bin/curl -u " & usernamePasswordString & " -d " & u & " https://api.del.icio.us/v1/posts/add"
		
		do shell script curlStatement
		
		-- Some sort of error checking statement would be useful here. Something that makes the script produce an alert upon a successful upload or escape and produce an alert if there's an error. Currently, there's nothing that indicates the status of the upload when you run the script.
		
	end repeat
end tell

To use the script, copy and paste the code into a new AppleScript file. In the property list, change “username” and “password” to your delicious login credentials. (Keep the enclosing quotation marks.)

When you run the script, DEVONthink takes whatever files you’ve selected and loops through each item.

I’ve tested it for most document types and it has worked with everything I’ve thrown at it so far, including PDF files.

If the record doesn’t have a URL, the script creates a pseudo-URL based on the record’s x-devonthink-item:// file handler. Clicking on these links from delicious sometimes but not always results in opening the correct file.

The script has worked well so far for me but it’s definitely rough around the edges. I’d love to get some suggestions and tips on how to improve it.

One aspect that could use immediate improvement is the lack of error-checking statements. Currently, there’s nothing that alerts you if your upload was successful or not.

Another issue is what the script sets as the notes field for the delicious bookmark. Currently, I have the script set whatever is in the DEVONthink record’s Spotlight Comments field to the delicious notes field. You could have the script set it to the plain text of the record, which would be extremely useful for posting all the small snippets of text in your DEVONthink database to delicious, but the API returns an error if there are more than 1000 characters in the variable.

A possible workaround to this problem would be to have the script count how many characters there are in the record before deciding which DEVONthink property to set as the variable.

But this is beyond my scripting abilities. I’d be much obliged to anyone who can help with this.

I’d like to try to expand this into an attached script to a DEVONthink group, that syncs whatever is in the group to my delicious account. Or into an Automator contextual plug-in that syncs selected records to delicious.

I welcome comments and suggestions!