Query database via AppleScript, get results as list

Is there a way to query the database via AppleScript (for instance groups) and get the results as pairs of [name, uid] ?

I am using this here to query the database (groups) via Alfred App:

x-devonthink://search?query=name:{query}* kind:group

But I’d love to get the result as a list, query the list via another tool (fzf) and narrow the result via fzf.

Currently navigating to groups is via “Go to Group”, and that menu is good, but not good enough to narrow down results. For instance the order of search words is not irrelevant, for instance “research covid” != “covid research”.

So is there a way in AppleScript to get the search result as a list and process it further in AppleScript?

The search AppleScript returns a list of results. Afterwards you could e.g. retrieve the name & UUID of each item to build the desired list.

1 Like

I have tanken this

And come up with this:

on findMatchingItems(_string)
	tell application id "DNtp"
		set resultList to search _string
		-- optional properties:
		-- title, subtitle, label/badge,icon,quickLookURL,action,actionBundleIdentifier 
		set results to {}
		repeat with result in resultList
			set rName to get name of result
			if exists uuid of result then
				set rUUID to uuid of result
			end if
			set rPath to "internal"
			if exists path of result then
				set rUUID to uuid of result
			end if
			-- set results to results & [{title:rName, subtitle:rSubtitle, path:rPath}]
			set results to results & [{title:rName, path:rPath}]
		end repeat
		return results
	end tell
end findMatchingItems

set myItemsHere to findMatchingItems("films")

But it can’t get the UUID. All I get is a list of entries like this:

{title:"DID-UGT-7-00238.doc", path:"internal"}

“path:” is always “internal” in my case.

Also, this function is pretty slow. It takes several seconds to return results.

Are there any examples in the forum I am not aware of?

If I understand you correctly, you want to query the database for names of groups and then get the result in the form

name <separator> uuid

Then here’s my take on it (not in AppleScript, as could be expected). The script asks for a string to search for, which should be ok for a script outside of a smart rule.

(() => {
  let app = Application("DEVONthink 3");
  app.includeStandardAdditions = true;
  var response = app.displayDialog("Group name?", {
    defaultAnswer: "",
    withIcon: "note",
    buttons: ["Cancel", "Continue"],
    defaultButton: "Continue"
  })
  if (response.buttonReturned === "Cancel") {
    return;
  }
  let searchString = "";
  if (response.textReturned !== "") {
	searchString = `name:${response.textReturned}*`;
  }
  searchString += " kind:group";
  let searchResult = app.search(searchString);
  saveResult(searchResult, app);
  searchResult.forEach(g => {
    console.log(`${g.name()}: ${g.uuid()}`);
  })
})()

function saveResult(searchResult, app) {
  const name = "searchResult";
  let rec = app.createRecordWith({"name": name, "type": "text"});
  let txt = "";
  searchResult.forEach(g => {
    txt += `${g.name()}: ${g.uuid()}\n`;
  })
  rec.plainText = txt;
}

The function saveResult can be modified as you see fit (as can the rest of the script, of course). I choose to output the results to a simple text file here. You could of course write them to a sheet or choose a different separator between group name and uuid (it’s a space here).
Caveat: If you have the same group name in different databases, the output is not very helpful. You could use

r.database().name()

to get at the name of the database, which you can then output as well

1 Like

I’m not an AppleScript conoisseur, but I guess that you’d have to get the uuid, same as the name. Or maybe not, this syntax is so weird … Oh, and you’re not outputting the uuid anyway (nor rUUID). So it’s not really shocking that it doesn’t show up :wink:

This is awesome! Thank you very much.