This creates a text file on your desktop so we can see what DEVONthink gets as input. Run it on the same selection that threw the error
-- Open selected text's link(s) in default app
property theDelay : 0.5
try
delay theDelay
tell application "System Events" to tell (first process whose frontmost is true) to keystroke "c" using {command down}
set theGrepResults to do shell script "osascript -e 'the clipboard as «class RTF »' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))' | egrep -o 'x-devonthink-item[^\"]*'" -- https://superuser.com/a/1409995/
on error
display alert "No text selected or no link(s) found" buttons {"Ok"} default button 1 message "" as critical
return
end try
tell application id "DNtp"
try
set theRefURLs to paragraphs of theGrepResults
repeat with thisRefURL in theRefURLs
my write_to_file(thisRefURL & linefeed, (POSIX path of (path to desktop) & "Script Debugging.txt") as string, true)
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access POSIX file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data as «class utf8» to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access POSIX file target_file
end try
return false
end try
end write_to_file
I think I understand the problem. In my links, the label and the target are both things like:
x-devonthink-item://D410B352-231E-47E8-82BB-73DECA3E545F , and that doesn’t seem to work.
I know that that’s the problem because if I change the label in the above link to be just some random text then it seems to work. So I either need to rewrite the code that generates the links so it gives them some safer label… or perhaps there’s a way to tell the script to look at the target, not the label.
Here is the script. Added an option to have the first link as front window instead of the last.
-- Open selected text's link(s) in default app
property theDelay : 0.5
property reverseOrder : true
try
delay theDelay
tell application "System Events" to tell (first process whose frontmost is true) to keystroke "c" using {command down}
set theGrepResults to do shell script "osascript -e 'the clipboard as «class RTF »' | perl -ne 'print chr foreach unpack(\"C*\",pack(\"H*\",substr($_,11,-3)))' | egrep -o 'x-devonthink-item[^\"]*'" -- https://superuser.com/a/1409995/
on error
display alert "No text selected or no link(s) found" buttons {"Ok"} default button 1 message "" as critical
return
end try
tell application id "DNtp"
try
set theRefURLs to paragraphs of theGrepResults
if reverseOrder = true then
set theRefURLs_reverse to {}
repeat with thisRefURL in theRefURLs
set beginning of theRefURLs_reverse to thisRefURL
end repeat
set theRefURLs to theRefURLs_reverse
end if
repeat with thisRefURL in theRefURLs
if length of (thisRefURL as string) = 56 then
try
set thisUUID to (characters 21 thru -1 in thisRefURL) as string
set thisRecord to get record with uuid thisUUID
set thisPath to path of thisRecord
tell application "Finder" to open file (POSIX file thisPath as alias)
end try
end if
end repeat
on error error_message number error_number
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell
Perfect! Thank you thank you for your willingness to spend time on this!! And having the script search a whole selection for links is the perfect example of a user not realizing what they need until it’s shown to them. Great design.
Carl
btw: one of my students is doing a project to predict the places most at risk for future covid outbreaks. So your efforts will help him move forward a little more efficiently. Every bit helps.