Integrate DEVONthink with Microsoft Word

I wrote two AppleScripts which I think can be useful for the Word users!

The first makes possible the use of x-devonthink-item URLs inside Word hyperlinks.
The script is the following:


tell application "Microsoft Word"
	set linkList to hyperlink objects of selection
	try
		set linkFirstItem to item 1 of linkList
	on error
		display dialog "Please select a hyperlink in Word before executing the script."
	end try
	set linkAddress to hyperlink address of linkFirstItem
	set linkAddressText to linkAddress as text
end tell

set pagePosition to offset of "page=" in linkAddress
set uuidLink to characters 38 thru -1 of linkAddress as text

if pagePosition = 0 then
	set thinkWindowName to uuidLink
	set xdevonLink to "x-devonthink-item://" & uuidLink
else
	set thinkWindowName to replaceText(uuidLink, "%3F", "?") of me
	
	set xdevonLink to "x-devonthink-item://" & replaceText(uuidLink, "%3F", "?") of me
end if

tell application "DEVONthink Pro"
	open tab for URL xdevonLink
	close think window named thinkWindowName
	activate
end tell

on replaceText(thisText, searchString, replacementString)
	set AppleScript's text item delimiters to the searchString
	set the itemList to every text item of thisText
	set AppleScript's text item delimiters to the replacementString
	set thisText to the itemList as string
	set AppleScript's text item delimiters to {""}
	return thisText
end replaceText

The second AppleScript automates the creation of a Word hyperlink for the currently viewed item in DEVONthink.

The code is the following:


tell application "DEVONthink Pro"
	set viewedRecord to the content record of viewer window 1
	set linkRecord to the uuid of viewedRecord
	set kindRecord to kind of viewedRecord
	set titleRecord to name of viewedRecord
	
	if kindRecord is "PDF" or kindRecord is "PDF+Text" then
		set PdfPage to current page of viewer window 1
		set DEVONthinkLink to "x-devonthink-item://" & linkRecord & "?page=" & PdfPage
		set the clipboard to DEVONthinkLink
		
		tell application "Microsoft Word"
			activate
			
			set textSelected to (get text object of selection)
			set startRange to start of content of textSelected
			set endRange to end of content of textSelected
			set theRange to create range active document start startRange end endRange
			tell active document to make new hyperlink object at end with properties {text to display:"(" & titleRecord & ": " & PdfPage & ")", hyperlink address:DEVONthinkLink, text object:theRange}
		end tell
		
	else
		set DEVONthinkLink to "x-devonthink-item://" & linkRecord
		set the clipboard to DEVONthinkLink
		
		tell application "Microsoft Word"
			activate
			
			set textSelected to (get text object of selection)
			set startRange to start of content of textSelected
			set endRange to end of content of textSelected
			set theRange to create range active document start startRange end endRange
			tell active document to make new hyperlink object at end with properties {text to display:"(" & titleRecord & ")", hyperlink address:DEVONthinkLink, text object:theRange}
		end tell
	end if
end tell


For more information you can read my post http://www.organognosi.com/integrate-microsoft-word-with-devonthink/.

Thanks for posting these scripts! Just one minor suggestion: Instead of constructing x-devonthink-item URLs on your own and replacing characters with percent escapes, you could simply use the “reference URL” property instead.

No joy over here with the first script (“use of x-devonthink-item URLs inside Word hyperlinks”). Took a while to dawn on me that you have to first create the hyperlink in Word, and then run the script. Else, the script fails with no error message. So, I’d suggest updating the internal commenting in the script to explain the preconditions for use. And I’d suggest adding error handling - if the script fails, pop up a message. In the case where the hyperlink did not exist, linkFirstItem would be null and therefore the hyperlink either wasn’t created or the cursor was in the wrong place.

But, over here the first script creates links of the form:


file://localhost/x-devonthink-item/::DB9C6818-1D1B-46FA-9DCD-ACD03A528D5D

and Word throws off the unhelpful

Thanks for your constructive comments!

I updated the script for traversing Word hyperlinks and now it works both with “x-devonthink-item://A559720F-6D2F-491B-922B-B22AC1241AA8?page=3” and “x-devonthink-item://500E1E71-D260-4979-8F4B-2525BAFE3951” URLs.


tell application "Microsoft Word"
	set linkList to hyperlink objects of selection
	try
		set linkFirstItem to item 1 of linkList
	on error
		display dialog "Please select a hyperlink in Word before executing the script."
	end try
	set linkAddress to hyperlink address of linkFirstItem
	set linkAddressText to linkAddress as text
end tell

set pagePosition to offset of "page=" in linkAddress
set uuidLink to characters 38 thru -1 of linkAddress as text

if pagePosition = 0 then
	set thinkWindowName to uuidLink
	set xdevonLink to "x-devonthink-item://" & uuidLink
else
	set thinkWindowName to replaceText(uuidLink, "%3F", "?") of me
	
	set xdevonLink to "x-devonthink-item://" & replaceText(uuidLink, "%3F", "?") of me
end if

tell application "DEVONthink Pro"
	open tab for URL xdevonLink
	close think window named thinkWindowName
	activate
end tell

on replaceText(thisText, searchString, replacementString)
	set AppleScript's text item delimiters to the searchString
	set the itemList to every text item of thisText
	set AppleScript's text item delimiters to the replacementString
	set thisText to the itemList as string
	set AppleScript's text item delimiters to {""}
	return thisText
end replaceText

Got it. I see that the use case is:

  1. Copy item link from DEVONthink (Edit > Copy Item Link or shift-command-C)
  2. In Word, select text and use Insert > Hyperlink. For “Link To” paste the DEVONthink item link that’s on the clipboard
  3. Place the cursor in the link field or next to it.
  4. Run the script and the DEVONthink item will open

Clever.

I installed the script in Word’s script menu (~/Documents/Microsoft User Data/Word Script Menu Items/DEVONthink). A Word restart is needed to make the item appear in the menu. Could also use the script in an Automator item, LaunchBar, Keyboard Maestro, and so on.

Thanks for your work!