I would like to have a version of the command “Update Name of Item Links” (under Tools > Item Links) that work only on a selected link, not all links in the document. So I am trying to write a script to do this, but ran into an issues.
To start, I found an AppleScript by @BLUEFROG that simulates the Update Name of Item Links. I modified it to step through each link and confirm if you want to change it or not. This works fine, but is not good for long documents with many links, because you cannot where the link appears in the text to make your decision.
I’d like to make a script that only modifies the selected link, but I can’t figure out if this is possible. When I try to process selected text, it gets stripped of the RTF attributes, so that’s no good.
Anyone have any ideas?
Here’s what I have right now:
tell application id "DNtp"
-- Get the selected file
set sel to content record
-- Only process rich text files
if (type of sel = RTF) or (type of sel = RTFD) then
-- Process attribute runs (blocks of text grouped by common attributes) in the text
repeat with textBlock in (attribute runs of rich text of sel)
-- Check if the current block has a URL
if exists (URL of textBlock) then
-- Cache the URL of the current block in a variable
set currentURL to (URL of textBlock)
-- Check if the URL is a DEVONthink item link
if currentURL begins with "x-devonthink-item://" then
-- If so, cache the text of the current block in a variable
set currentText to rich text of textBlock
-- Strip the "x-devonthink-item://" from the URL
set currentURL to (characters 21 thru -1 of currentURL) as string
-- if the linked record no longer exists, remove it
if not (exists (get record with uuid currentURL)) then
beep
display dialog "Link for '" & currentText & "' is broken. Removing it." buttons {"ok"} default button 1
set (URL of textBlock) to ""
else
-- if linked record exists, continue...
-- Get the linked record
set linkedRecord to (get record with uuid currentURL)
-- Get the name of the linked record
set linkName to (name of linkedRecord)
-- Check if the name of the linked file matches the text of the current block
if linkName ≠ currentText then
-- ask if you want to update this link
set this_line to the current line of current tab of think window 1
set question to "Do you want to update the name of this link?" & return & return & "NOW: " & currentText & return & "NEW: " & linkName
set options to {"no", "yes"}
set answer to the button returned of (display dialog question buttons the options default button 1)
if answer = "yes" then
-- If the name and text don't match, update the text to the name of the linked record
set rich text of textBlock to linkName
end if
end if
end if
end if
end if
end repeat
end if
end tell