[Help] Use AppleScript to get all the records of Smart Groups?

Hi there,

I have a short AppleScript (copied from this forum comment) that opens a random record from all the records in the currently open group, for review purposes. Today, I created Smart Groups for some of the groups that I frequently use this AppleScript to review, and found out that (not surprisingly) the current script no longer presents me with a random record when the currently selected group is a Smart Group.

I searched in the forum and didn’t find any posts on this rather niche problem, and my poor AppleScript skills are insufficient for figuring it out on my own. Please help me update the script to meet my new demand, if it’s technically possible.

Thank you in advance for your kind help! :slight_smile:

P. S. I made some drastic changes to the folder structures of my database, so it’s not an option for me to simply revert the newly created Smart Groups to regular groups. If AppleScript is incapable of my new request, I’ll choose to make do without the script instead.

Afaik, you can retrieve the search criteria for the smart group(s) and then perhaps pass them on to the search method. That would return the list of matching records.

1 Like

Thank you for the quick reply, chillek! That approach didn’t cross my mind at all, I’ll give it a try and report back asap!

I’m not quite sure what it is that you want to have in the end. But let’s suppose that you have currently selected a smart group. The following script, which is written in JavaScript, will then open a randomly selected record from this group in a new window.

(() => {
  const app = Application("DEVONthink 3");
  const rec = app.selectedRecords()[0];
  if (!rec || rec.type() !== "smart group") {
    app.logMessage("Select a smart group");
	return;
  }
  const group = (() => {if (rec.searchGroup()) {
    return rec.searchGroup();
	}
  else
   return rec.database.root();
  })()

  const predicates = rec.searchPredicates();
  const matchingRecords = app.search(predicates, {in: group});
  const last = matchingRecords.length;
  const randomIndex = Math.floor(Math.random() * last);
  app.openWindowFor({record: matchingRecords[randomIndex]});
})()

It’s not thoroughly tested, and it will perhaps not work with global smart groups.
Edit Updated so that the script should work with top-level smart groups, too

1 Like

Is anything shown in Window > Log?

If that’s a question for me: No. At least not, if I select a smart group before running the script.

Hi chrillek, thank you for taking the time to write an example script for me!

I had no idea how to use JavaScript in DEVONthink, and still can’t get the script to work as intended. Here are a list of things I tried:

  1. I created a Keyboard Maestro macro and used its Execute JavaScript for Automation Action, and Keyboard Maestro report that Error: Can't get object. (-1728)
  2. I thought maybe I need to run the script inside DT somehow, so I opened DT’s manual and searched for “JavaScript”. There is one mention of JXA, but I’ve no idea how to use it.
  3. I used a text editor to create a Test.js file containing the sample script and put the file in ~/Library/Application Scripts/com.devon-technologies.think3/Menu, but the script didn’t show up under DT’s Scripts menu.
  4. I searched online and found your JXA guide. I used Script Editor, pasted in the code, changed the language on the top left corner to JavaScript, and saved a Test.scrpt file to the same path in 3.
    1. If I run the script while a DT window for the Smart Group is open, DT’s log says Select a smart group. If I run the script while a DT window for the smart group’s parent directory is open and the smart group is selected, I didn’t get any response from DT.
    2. I tried running the script directly in Script Editor, and its log window says Error -1728: Can't get object.. It’s the same as when I ran it in Keyboard Maestro, so I assume it’s true that I need to run JXA within DT.

The groups I tested for this forum reply (and all the groups I will be reviewing) are smart groups created within a database, so none of them are global.

Hi Jim! Thanks for the replies, I thought the question would be for me. :stuck_out_tongue:

When I tried to run the AppleScript while selecting a smart group, DT’s log window didn’t report anything at all.

Edit: I should be clearer. In this scenario, DT will open a window for a record that’s on the same level of folder hierarchy as the smart group I selected, rather than a record that’s inside the smart group, i.e. one level deeper.

- A.md
- B.pdf
- C.png
- Smart Group
    |---a.md
    |---b.pdf
    |---c.png

Take the above as an example, the script will cause DT to open A.md, B.pdf, or C.png, while what I want is for DT to open a.md, b.pdf, or c.png.

What is reported in the replies when you run the script in Script Editor?

Well, I’d made it clear that a smart group must be selected for this script to work at all. That gets the first problem out of the way.
The simple approach would be:

  • copy the script from the forum (use the copy to clipboard button appearing in the right top corner of the script code when you mouse over it
  • open script editor
  • set its language to JavaScript
  • paste the clipboard contents into the window
  • In DT, select a smart group in the list view (center pane)
  • in Script Editor, activate the protocol with Cmd-9 (or the three horizontal lines at the bottom of the Script Editor window)
  • run the script

I suspect that you’ve not selected a smart group when the script runs. Can you provide a screen shot of the DT window before you run the script?

I was running the script in the form of a .scrpt file, from DT’s Scripts menu. In order to answer your question, I tried running it in Script Editor. DT opens a window for a record on the same level of hierarchy as the smart group (see the edit of my previous reply), and Script Editor reports viewer window id 23382 of application "DEVONthink 3". The id obviously changes each time as DT picks a different random record.

It seems that the “Object not found” error happens with smart groups at the top level of the database. I’ll try to fix that.

Oh, that’s exactly the case in my scenario. Sorry I didn’t mention that, I didn’t realize that could be relevant.

I updated the script above, and it seems to work with top level groups now, too.

1 Like

Yup, I can confirm that it now works as intended for my smart groups that are at the root level of the database as well. Thank you so much for providing such an amazing script so fast!

Hi Jim,

chrillek’s JXA script works perfectly for my purpose, but since you asked twice about the log in DT or Script Editor, I can’t help but wonder: is it unexpected that the AppleScript linked in my original post doesn’t work for smart groups? If so, I’d of course be glad to provide further diagnostics or AppleScript testing results if you need them. :slight_smile:

The item you linked to was a quick proof-of-concept snippet of code, not a fully error-trapped script. There is already such a script available in Script menu > More Scripts > Display Random Note. (Note: That script is queued for an update in the near future.)

2 Likes

Oh. Wow. I’m speechless haha.

I guess I must’ve missed it and proceeded to search for “random note applescript” in the forum, and then used a proof-of-concept script instead of the one already available in the Scripts menu. The snippet worked fine for me for regular groups, but I just tested and confirmed that the “Display Random Note” AppleScript works for smart groups too. Silly me. :stuck_out_tongue:

:wink: No worries.

1 Like