Accessing the right-clicked record vs. the selection in a context menu script

I have a JXA script installed in the Contextual Menu folder that operates on all currently selected records. This works well in the common case, but there’s an edge case I’d like to handle correctly:

If a user has several records selected and then right-clicks on a different, unselected record, the intended target of the action is clearly that right-clicked item alone — not the existing selection.

In that situation, app.selectedRecords() still returns the previously selected records, not the item that was right-clicked. Is there any way from within a context menu script to determine which record was right-clicked, independently of the selection state?

(I’m currently stuck on DT 3.9.17, in case that makes a difference.)

This is not scriptable currently. Which view did you use?

There is no “action” in that situation. A user selects records. Perhaps they have in mind to run a script on them. Perhaps they want to copy or move or delete them. Who knows.

Than they decide to right click on a record (any record!). That means that they want to run a contextual action on that record. … and wow, there is “Execute smart rule” available in that contextual menu (close to the bottom). That does exactly what you want.

So, you create a smart rule selecting all documents and executing your script as its only action. Use function performsmartrule(records) as a wrapper for your code and treat records as an Array. This smart rule works equally well from the context menu on the selected records, so you should be all set.

1 Like

Thank you for your concise answer. I’m not sure what exactly is meant by view, but the behaviour is the same in list view and icon view.

I’m talking about the “Script” entries (group?) in the context menu. But I’m not entirely sure what you mean.

I was hoping to avoid wrapping the script in a smart rule, but if it works that’s fine, of course. So I tried replacing app.selectedRecords(); with function performsmartrule(records) and got it to work; but, as far as I can tell, the script still just runs on the selected records, not the one that was used to trigger the smart rule from the context menu.

It works ok here (DT 4.2 on macOS 15.7.3). I select a bunch of records in the main window (“item list”) and then right click, “Execute smart rule” – the script in the rule handles the selected records. Then I right click on a non selected record, execute smart rule, and the script in the rule handles only the record under the mouse for which I opened the context menu.
Here’s the trivial script:

function performsmartrule(records) {
	var app = Application("DEVONthink");

	records.forEach (r => {
		app.logMessage(`Running on ${r.name()}`);
	})
}

and the equally trivial rule

Output for the first case:

Output for the second case:
Bildschirmfoto 2026-05-14 at 15.08.54

The main window

I do not see that in 4.2. Nor is a “Script” menu mentioned in the manual’s appendix on “Context menu”. Perhaps a screen shot would help to see what you see.