Bookends and DevonThink "side by side" with same PDF folder -- risky?

This script might be useful too for users who simply index the complete attachments folder of Bookends as it doesn’t index attachments on its own, instead it matches already indexed files inside all opened databases to all attachments of the frontmost library window of Bookends.

The URL of the indexed attachments links to the record in Bookends and optionally User20 field in Bookends links back to DEVONthink.

-- Cross-links indexed files in DEVONthink's databases and attachments of Bookends

-- Optionally sets User20 field of Bookends' reference to DEVONthink's item link
-- In case of multiple attachments or an attachment indexed multiple times the item link is not unique and therefore the field not changed
property pUser20ItemLink : false

-- Optionally set Finder comment to formatted reference using the specified format
-- Use e.g. "RIS.fmt" instead
property pCommentFormat : ""

-- Optionally set aliases to citekey
property pCitekeyAliases : false

tell application "Bookends"
	tell front library window
		repeat with theItem in publication items of group attachments
			set itemLink to ""
			set numCrossLinks to 0
			set {theID, theAliases} to {id, citekey} of theItem
			set theURL to ("bookends://sonnysoftware.com/" & theID) as string
			
			if pCommentFormat is not "" then
				set theComment to format theItem using pCommentFormat
			else
				set theComment to ""
			end if
			
			repeat with theAttachment in attachment items of theItem
				set thePath to path of theAttachment
				
				tell application id "DNtp"
					repeat with theDatabase in databases
						repeat with theRecord in lookup records with path thePath in theDatabase
							set recordURL to URL of theRecord
							if recordURL is "" or recordURL is equal to theURL then
								set URL of theRecord to theURL
								if pCitekeyAliases then set aliases of theRecord to theAliases
								if theComment is not "" then set comment of theRecord to theComment
								set itemLink to reference URL of theRecord
								set numCrossLinks to numCrossLinks + 1
							end if
						end repeat
					end repeat
				end tell
			end repeat
			
			if pUser20ItemLink then
				if numCrossLinks > 1 then
					set theTitle to title of theItem
					tell application id "DNtp" to log message theTitle info "Record has multiple indexed attachments."
				else if numCrossLinks is 1 and user20 of theItem is "" then
					set user20 of theItem to itemLink
				end if
			end if
		end repeat
	end tell
end tell
1 Like