Integration with Hook

Here’s a revision of Luc’s script (see https://hookproductivity.com/help/integration/using-hook-with-devonthink/devonthink-hook-applescript/) which supports all databases, is faster and adds error handling:

property pTag : "Hook"

tell application id "DNtp"
	tell application "Hook" to set hookedLinks to address of (every bookmark whose address begins with "x-devonthink-item://" and hooked bookmarks is not equal to {})
	try
		repeat with theDatabase in databases
			repeat with taggedRec in (lookup records with tags {pTag} in theDatabase)
				set oldTags to tags of taggedRec
				set newTags to {}
				repeat with t in oldTags
					if (t as string) is not equal to pTag then set newTags to newTags & t
				end repeat
				set tags of taggedRec to newTags
			end repeat
		end repeat
		
		set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "x-devonthink-item://"}
		repeat with l in hookedLinks
			set theRecord to get record with uuid (text item 2 of l)
			if theRecord exists then set tags of theRecord to tags of theRecord & pTag
		end repeat
		set AppleScript's text item delimiters to od
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

But better (meaning automatic and more efficient) is probably to use a scheduled smart rule like this one…

…and the following script:

-- The (scheduled) smart rule has to find items already tagged with "Hook" and to untag them as this scripts adds only the tag

property pTag : "Hook"

on performSmartRule(theRecords)
	tell application "Hook" to set hookedLinks to address of (every bookmark whose address begins with "x-devonthink-item://" and hooked bookmarks is not equal to {})
	tell application id "DNtp"
		try
			set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "x-devonthink-item://"}
			repeat with l in hookedLinks
				set theRecord to get record with uuid (text item 2 of l)
				if theRecord exists then set tags of theRecord to tags of theRecord & pTag
			end repeat
			set AppleScript's text item delimiters to od
		end try
	end tell
end performSmartRule
3 Likes