In HTML records I have lots of links (originally created in Zettelkasten.app), but they broke when I moved to DEVONthink. The broken links look like “#z_60” and the right record to link to in DEVONthink would be “60.html”.
With the script below I can open the window that lets me edit the link, copy the broken link, format it (to match the beginning of the corresponding record), search for that record, get its reference URL and paste it in the link edit window.
But why does it work one time and the next time the clipboard pastes the wrong content? (= the reference URL from the previous run)
Is there a known issue with the clipboard being to slow? I tried a delay of 10 and even then it does not work reliable. Or am I missing something?
PS is there an easier way to do the trick?
tell application "System Events"
tell process "DEVONthink Pro"
set frontmost to true
# open window to copy the broken link (assigned a shortcut for that)
keystroke "x" using {control down, option down, command down, shift down}
set copyLink to (keystroke "c" using {command down})
set oldLink to the clipboard
delay 2
# format broken link to match with corresponding record name
set theSelection to (characters ((offset of "_" in oldLink) + 1) thru -1 in oldLink as string)
display notification "Suche nach Zettel: " & theSelection
tell application "DEVONthink Pro"
set theSearch to search theSelection
set result1 to item 1 in theSearch
set resultName1 to name of item 1 in theSearch
display notification "Reference URL für: " & resultName1
if resultName1 starts with theSelection then
set theRefURL to (the reference URL of result1)
set the clipboard to theRefURL
tell application "System Events"
tell process "DEVONthink Pro"
set frontmost to true
keystroke "v" using command down
end tell
end tell
end if
end tell
end tell
end tell