Get Selected Item Link, Whether Document or Group

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.

Perhaps this is possible?

Welcome @Astor

I can get the “x-DEVONthink-item” link for a document:

But your code is getting the name of a document: set recordName to get the name of (item 1 of (selection as list))

And I can get the “x-DEVONthink-item” link for a group (at least this way):

While variable names are you choice, your use of recordName is unclear in this context: set recordName to get the reference URL of the current group


But to your question, you just use the reference URL of the selected item.

Also, use selected records to handle the selection in matters like this.

tell application id "DNtp"
	set recordURL to (reference URL of selected record 1)
end tell

This works on multiple selected items as well…

PS: It’s best to not use a try…end try block when you’re initially developing your code. It hides errors.

1 Like

Thank you for quick help.

If I use your first code on selected document:

tell application id "DNtp"
set recordURL to (reference URL of selected record 1)
end tell

I receive the x-devonthink-item link. It works!

But if I use on selected group, I receive Script Error:

error "DEVONthink 3 got an error: Can’t get selected record 1. Invalid index." number -1719 from selected record 1

I am making some mistake.

Also if I use the code in the screen shot box:

tell application id "DNtp"
	set recordURL to (reference URL of selected records)
end tell

This works for multiple selected documents all right. But if I use on a selected group, I receive error again, this time just {}

Did you select the group in the item list?

I select a group in the side-bar.

That’s the root of the window.

set recordURL to (reference URL of (root of think window 1))

and it doesn’t support multiple selected groups in the Navigate sidebar.

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?

Select groups in the item list, not the Navigate sidebar and you can use the code I originally provided.

I can select groups in the side-bar only. The documents show in the next list (what is named the item list?)

Not true – see the screenshot below.

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.


JavaScript code:

(() => {
  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.

1 Like

Do you have View > Show Only Documents enabled?

PS: DEVONthink is heavily documented (I should know as it’s my responsibility) via the built-in Help and manual.

Also Help > Tutorials > Understand the UI

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
2 Likes

Perhaps it is this way. Yes I have View > Show Only Documents enabled. And the ‘View’ type is “as List” and ‘Preview’ type is “Widescreen”.

So to copy item links by AppleScript does not work with my chosen configuration of these view setups?

Then the groups are logically hidden.

And in your case, you can only use the root of think window 1 to get the item link for a single group selected in the Navigate sidebar.

What are you specifically trying to accomplish?

2 Likes

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.

It’s OK for me. I can use one script for groups and another for documents.

Thank you for the quick help.

You’re welcome.