In DEVONthink it is possible to right-click and “Copy Item Link” to get the x-devonthink-item link for any document. It is also possible to do the same to get this sort of link for a group.
I wonder how to use this in AppleScript.
I can get the “x-DEVONthink-item” link for a document:
tell application id "DNtp"
try
set recordName to get the name of (item 1 of (selection as list))
end try
end tell
And I can get the “x-DEVONthink-item” link for a group (at least this way):
tell application id "DNtp"
try
set recordName to get the reference URL of the current group
end try
end tell
But how can I get the x-devonthink-item link regardless if it is a document or a group that is currently selected?
I only select one document or one group at a time.
Yes that works for groups I select in the side-bar. But it does not work for individual documents. It gives the link of the group of the document, not just document.
I hope for a way to receive the link for a group or a document, whichever I select, but perhaps I can only use one or the other?
Whatever you select in the item list (i.e. the central part of the DT window) will become part of the selected records property. Be that groups, records, tags… no matter.
(() => {
const app = Application("DEVONthink 3");
app.selectedRecords().forEach(r => console.log(`${r.referenceURL()} is a ${r.type()}`));
})()
result:
x-devonthink-item://5BBDF436-8A6D-41EE-BE50-99A3F438E21E is a group
x-devonthink-item://AB6B3F85-8AF3-4484-AFD4-9B5BC72C12F0 is a group
x-devonthink-item://6C6E44EC-067B-49F1-9732-E39A405E42ED is a group
x-devonthink-item://223BD62B-C608-40C6-9A68-CEEE7A67D399 is a smart group
x-devonthink-item://2BDF4D1A-A69A-4162-A6F3-633658AD3272 is a smart group
x-devonthink-item://498E3540-FC34-4B09-A563-5A9293F1F781 is a smart group
x-devonthink-item://EA31D1FE-1ADA-488E-A59A-3CEC3A7390DF is a rtf
x-devonthink-item://2DF7C74B-6198-4E5E-8A74-1EF166D98780 is a html
The left side of the window, aka the “navigation sidebar”, is not reflected in the selected records property. And there, you can only select records if they are part of the favorites.
And an AppleScript variation of @chrillek’s JavaScript code…
tell application id "DNtp"
set theList to {}
repeat with theRecord in (selected records)
set the end of theList to (theRecord's reference URL & " is a " & theRecord's type & return as string)
end repeat
return theList as string
end tell
yields…
x-devonthink-item://CEA7F193-27E1-444D-9670-C99215BD431A is a smart group
x-devonthink-item://66461675-6C78-43AD-8A38-B5C2EA94E0BE is a group
x-devonthink-item://03EEF0E4-C562-45A0-BA08-1EFE21949849 is a txt
x-devonthink-item://D8B8E2C7-F097-464F-965D-3E2840A81E5B is a sheet
Why do you cast a string (reference URL) that is concatenated to other strings (" is a ", type, and return) to a string?
Is that necessary or just a safety measure because sometimes concatenating strings in AppleScript results in something else? And why pushing everything to the end of a list that is then converted to a string again in the end – wouldn’t it be more straightforward to just concatenate strings? Just asking.
tell application id "DNtp"
set theList to ""
repeat with theRecord in (selected records)
set theList to theList & (theRecord's reference URL & " is a " & theRecord's type & linefeed)
end repeat
return theList
end tell
Fun fact: one has to use linefeed instead of return in this case. Weird.
Habit and yes, it’s safer to explictly coerce it. While it can seem like overkill in some instances, there’s no overhead hit and it avoids problems in other instances.
And why pushing everything to the end of a list that is then converted to a string again in the end – wouldn’t it be more straightforward to just concatenate strings? Just asking.
Regarding the list, just another habitual example that illustrates the mechanism of coercing lists to string.
I also naturally have a preference for creating lists, especially as it enhances the dynamic use of text item delimiters.