As you may have noticed, DEVONthink links are not properly recognized by Microsoft Word.
First of all, when you have one of them (e.g. the “x-devonthink-item://14AE6E1A-DB3E-429B-8A46-4E1C3174A6F2?page=23″) in the clipboard by using the “Copy Page Link” DEVONthink command and paste it to a Word document, you do not get a proper hyperlink but you just see the raw URL. Moreover, when you try to create a Word hyperlink using the normal procedure, (Insert -> Hyperlink… or Command + K) the DEVONthink URL is changed to a degree that cannot be recognized and used by Word. Specifically, after inserting it in the “Link to:” field of the “Insert Hyperlink” Word window

it is converted to “file://localhost/x-devonthink-item/::14AE6E1A-DB3E-429B-8A46-4E1C3174A6F2%3Fpage=23″.
Fortunately the changes are not so destructive and the link can become functional using… what else, an AppleScript!
The only requirement for the proper execution of the script is to have either the cursor inside the hyperlink or the hyperlink should be selected with or without any of its adjacent text. If more than one hyperlinks are in the selected text then the first link will be activated by the AppleScript.
The AppleScript code for activating a link in Word 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
Click here to open the source code in AppleScript Editor
Now that we can use DEVONthink links in Word it is time to automate the creation of Word hyperlinks! The new hyperlink will have as destination the selected item in DEVONthink and it is created at the cursor position in the front Word document. The display text of the link follows the template: “(Name of DEVONthink record)” or “(Name of PDF file: Page Number)”.
The AppleScript code which creates Word links to a DEVONthink record 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
Click here to open the source code in AppleScript Editor
hello john
it’s always a great journey every time i visit your blog, your tutorial was very specific and the roadmap also help me to understand “the big picture” clearly. Ok recently i just try the apple script for integrating word and DEVONthink, but honestly i didn’t have any experience or knowledge how to use these script. I already try to run the apple script editor, then paste your script, hit the run button, but it wasn’t succeed yet. Could you explain more “step by step for dummies” how to use these script ? Thank you for your time.
Thanks for the comment Dani!
The use case for the first AppleScript 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
Source:http://forum.devontechnologies.com/viewtopic.php?f=20&t=15320 – Thanks Korm!
Before executing the second AppleScript you should have selected only one item in DEVONthink.