A better method to connect Bookends and Devonthink

Users asking other users for AppleScripts that work with Bookends.
Post Reply
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

A better method to connect Bookends and Devonthink

Post by Dellu »

I have been using Zach's script posted here viewtopic.php?t=4823 to jump back and forth between BE and DT.
Zach's script very useful one; have been using it for a long time. But, it requires a lot of work to write the links on user20 and the url field in Dt.

ryanjamurphy in DT forum https://discourse.devontechnologies.com ... an/72230/5 came up with rather a simple magic.

I just want to bring your attention to this script. I find this new method much more simple and effective.

Code: Select all

-- get frontmost app and trigger the right function depending on if Bookends or DEVONthink are active.
tell application "System Events" to set activeApp to name of application processes whose frontmost is true
if (activeApp as text) contains "DEVONthink" then
	my openBookendsReferenceFromDTItem()
else if activeApp contains "Bookends" then
	my openDTItemFromBookends()
end if

on openDTItemFromBookends()
	tell application "Bookends"
		set theWindow to front library window
		set theSelection to theWindow's selected publication items
		repeat with eachItem in theSelection
			set theAttachments to get eachItem's attachment item
			set theAttachment to item 1 of theAttachments
			log theAttachment's path as string
			tell application id "DNtp"
				set mainframeDatabase to get database with uuid "D0CA3444-A862-4C99-9A20-3B93E6F24CA8"
				set libraryDatabase to get database with uuid "956EB0B2-F8DE-4955-873A-A065F0D096B7"
				set archiveDatabase to get database with uuid "ADB9D9D0-0304-43F4-A187-945F15A4777C"
				set attachmentDTRecord to lookup records with path (theAttachment's path as string) in mainframeDatabase
				if (count of attachmentDTRecord) is 0 then
					set attachmentDTRecord to lookup records with path (theAttachment's path as string) in libraryDatabase
				end if
				if (count of attachmentDTRecord) is 0 then
					set attachmentDTRecord to lookup records with path (theAttachment's path as string) in archiveDatabase
				end if
				if (count of attachmentDTRecord) is 0 then
					display notification "No corresponding DEVONthink record found for this reference's first attachment."
				else
					set attachmentDTRecord to the first item in attachmentDTRecord
					set openRecordWindow to open window for record the first item in attachmentDTRecord
					activate
				end if
			end tell
		end repeat
	end tell
end openDTItemFromBookends

on openBookendsReferenceFromDTItem()
	tell application id "DNtp"
		set theSelection to get the selection
		if (count of theSelection) is greater than 1 then
			display notification "More than one record selected in DEVONthink. Opening the first in Bookends."
		end if
		repeat with eachRecord in theSelection
			set thePath to eachRecord's path
			set theName to eachRecord's name
			tell application "Bookends"
				set thePublications to sql search "attachments REGEX '" & theName & "'"
				try
					set thePublication to the first item in thePublications
					my openBookendsItem(thePublication's id as string)
					activate
				on error
					display notification "This record was not found as an attachment in Bookends."
				end try
				--if (count of theSelection) is 1 then
				--else
				--set targetGroupName to the text returned of (display dialog "Name the group for these items:" default answer "")
				--tell front library window
				--	set targetGroup to make new group with properties {name:targetGroupName}
				--						
				--	repeat with eachPublication in thePublications
				--		add eachItem to targetGroup
				--	end repeat
				--end tell
				-- end if
			end tell
		end repeat
	end tell
end openBookendsReferenceFromDTItem


on openBookendsItem(someBookendsID)
	open location "bookends://sonnysoftware.com/" & someBookendsID
end openBookendsItem
Post Reply