Improved del.icio.us import

Hey, I stayed up a bit too late tonight, so I decided to extend the del.icio.us import script. I really hated the way it put the timestamp of import instead of the correct date from del.icio.us, which was hampering my ability to use it how I needed it. Here is an updated version that puts the actual del.icio.us timestamp in as the time of the record:


-- Import del.icio.us bookmarks
-- Created by Christian Grunenberg on Mon Jan 23 2006.
-- Copyright (c) 2006. All rights reserved.
-- Modified by sunetos of adamia.com

-- If the properties pUser and pPassword are undefined and the keychain doesn't contain a key for del.icio.us,
-- then dialogs pop up asking for these properties

property pUser : ""
property pPassword : ""

tell application "DEVONthink Pro"
	try
		set theURLs to {}
		set theNames to {}
		set theTags to {}
		set theDates to {}
		
		if (pUser is "") or (pPassword is "") then
			try
				tell application "Keychain Scripting"
					set theKey to first Internet key of current keychain whose name is "del.icio.us"
					set pUser to account of theKey
					set pPassword to password of theKey
				end tell
			end try
		end if
		
		if pUser is "" then
			repeat
				display dialog "Enter username:" default answer "" buttons {"Cancel", "OK"} default button 2
				set pUser to the text returned of the result
				if pUser is not "" then exit repeat
			end repeat
		end if
		
		if pPassword is "" then
			repeat
				display dialog "Enter password:" default answer "" buttons {"Cancel", "OK"} default button 2
				set pPassword to the text returned of the result
				if pPassword is not "" then exit repeat
			end repeat
		end if
		
		set theXML to download markup from "https://api.del.icio.us/v1/posts/all" user pUser password pPassword encoding "UTF-8"
		
		if theXML is not "" then
			set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "href=\""}
			set theHrefs to text items 2 thru -1 of theXML
			set AppleScript's text item delimiters to "\""
			repeat with theHref in theHrefs
				set theURLs to theURLs & (text item 1 of theHref)
			end repeat
			
			set AppleScript's text item delimiters to "description=\""
			set theDescriptions to text items 2 thru -1 of theXML
			set AppleScript's text item delimiters to "\""
			repeat with theDescription in theDescriptions
				set theNames to theNames & (text item 1 of theDescription)
				if theDescription contains "tag=\"" then
					set AppleScript's text item delimiters to "tag=\""
					set theTag to text item 2 of theDescription
					set AppleScript's text item delimiters to "\""
					set theTags to theTags & (text item 1 of theTag)
				else
					set theTags to theTags & ""
				end if
				
				if theDescription contains "time=\"" then
					set AppleScript's text item delimiters to "time=\""
					set theDate to text item 2 of theDescription
					set AppleScript's text item delimiters to "\""
					set wholeDateString to text item 1 of theDate
					set theDates to theDates & (wholeDateString)
				else
					set theDates to theDates & ("")
				end if
				
			end repeat
			set AppleScript's text item delimiters to od
			
			if (count of theURLs) is equal to (count of theNames) then
				set theGroup to create location "del.icio.us"
				set theCnt to (count of theURLs)
				repeat with i from 1 to theCnt
					set theURL to (item i of theURLs)
					set theName to (item i of theNames)
					set theTag to (item i of theTags)
					set theDate to (item i of theDates)
					if not (exists record with URL theURL in theGroup) then
						set newDate to my dateFromDeliciousString(theDate)
						create record with {name:theName, type:link, URL:theURL, comment:theTag, date:newDate} in theGroup
					end if
				end repeat
			else
				error "Unknown error."
			end if
		else
			error "Download failed."
		end if
	on error error_message number error_number
		if the error_number is not -128 then
			try
				display alert "DEVONthink Pro" message error_message as warning
			on error number error_number
				if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
			end try
		end if
	end try
end tell

on dateFromDeliciousString(dateStringUnicode)
	set AppleScript's text item delimiters to ""
	set dateString to (dateStringUnicode as string)
	set theYear to characters 1 thru 4 of dateString as string
	set theMonth to characters 6 thru 7 of dateString as string
	set theDay to characters 9 thru 10 of dateString as string
	
	set justDate to theMonth & "/" & theDay & "/" & theYear as string
	set justTime to characters 12 thru 19 of dateString as string
	
	set newDate to justDate & " " & justTime
	
	set dateObj to date newDate
	return dateObj
end dateFromDeliciousString