Saving a search as a x-devonthink link

Hello!

Forgive me if it already exists and I couldn’t find it:

I’d like to be able to save a search query, not as a smart group, but as a x-DEVONthink link (copied to the clipboard) which I can paste in other systems.

What I do is what you don’t want to do, which is save the query as a smart group, then right-mouse click on that new group to “copy item link” to the clipboard, then past into elsewhere–usually Reminders or document. Can always move the new smart group to trash.

3 Likes

Item links point to objects in the database. A search query is not an object. Unless you save it as a smart group. Which in turn has a UUID and can consequently be accessed using an item link.

Which other systems? And what would these systems then do with that link if it existed?

1 Like

Thank you both!

@rmschne:

  • I am aware of that way of doing, yet it’s too many clicks for me and i fear smart-group creep.

@chrillek :

  • x-DEVONthink links allow for triggering commands (like a search), not only pointing to items.
  • once the x-DEVONthink-search link lies in the clipboard, i could quickly paste it in a task/project mgmt or calendar app, for example, to return to it later, or regularly.

You might be able to build an AppleScript script that does that for you.

This is the only reference to “search” in the context of item links that I found in the manual:

search: Directly jumps to the first occurrence of the search string in the specified document. Usage: search=<string>.

So yes, you can search with an item link. In a much more limited sense than any smart group. But, afaict, this search is nowhere close to what you can do with the search functionality in DT.

Then there are also URL commands, which indeed allow a search parameter that is more powerful. But it is also limited to the currently open databases. Perhaps you were talking about that one? I guess you could automate that somehow using KM or BTT or so: Select the search string in DT, and then trigger a keyboard command that builds the corresponding URL command from it and saves that to the clipboard.

Nothing you could do inside DT, AFAIK. You’d have to resort to other tools.
I should’ve looked more thoroughly. You can access the current search query with the search query property of DT’s main window. So, a very raw JXA script might look like this:

(() => {
  const app = Application("DEVONthink");
  app.includeStandardAdditions = true;
  const queryString = app.mainWindows[0].searchQuery();
  if (queryString.length > 0) {
    const URLcommand = `x-devonthink://search?query=${encodeURIComponent(queryString)}`;
    app.setTheClipboardTo(URLcommand);
  }
})()

This worked at least for a trivial query.

Once the query is complete, it’s only three clicks to 1. save the smart group, 2. copy the link, then 3. remove the smart group to alleviate “smart group creep”. As @chrillek suggests the third-party apps Better Text Tool and/or Keyboard Maestro may help you automate. I didn’t attempt to try Keyboard Maestro for you.

Damn, you beat me to it :smiley:
I’ll still post my version:

// Copy search as URL command
(() => {
	const app = Application("DEVONthink");
	app.includeStandardAdditions = true;
	const q = app.thinkWindows[0].searchQuery;
	q.exists() ? app.setTheClipboardTo(
		'x-devonthink://search?query=' + encodeURIComponent(q())
	) : app.displayAlert("No search query to copy.");	
})()

You can’t limit the scope as much as you can with a smart group, though. But that might be of no concern in many cases.

tell application id "DNtp"
	tell think window 1
		if not (exists search query) then return
		set searchString to (do JavaScript "encodeURIComponent('" & (search query & "');"))
		set the clipboard to ("x-devonthink://search?query=" & searchString as string)
	end tell
end tell

-- tags:support;devonthink;sync troubleshooting scope:"DEVONtech Support_r5"
-->x-devonthink://search?query=tags%3Asupport%3Bdevonthink%3Bsync%20troubleshooting%20scope%3A%22DEVONtech%20Support_r5%22

Simple.