JavaScript: Create smart group

Ok, I’m now on the JS train. But trying to translate your script here to JS I can make the smart group but not making it do the query. I’ve guessed all kind of things (as the documentation got some holes) along the lines of this:

app.createRecordWith({ name: 'test group', type: 'smart group', searchGroups: group, searchPredicates: 'tags: test tag' })

Do you know how to write it? And if there are there some documentation somewhere please point me towards it.

Some context would have been great. Also, if you get an error message (which one) and more generally, what happens.

Your first point of reference should be the scripting dictionary as shown in Script Editor. If you look up createRecord there, you’ll see this:

createRecordWith Record : The properties of the record
(‘name’, ‘type’, ‘comment’, ‘aliases’, ‘path’, ‘URL’, ‘creation’, ‘modification’, ‘date’, ‘plain text’, ‘rich text’, ‘source’, ‘data’, ‘content’, ‘columns’, ‘cells’, ‘thumbnail’ and ‘tags’ are possible values).
[in: Record] : The destination group for the new record. Uses incoming group or group selector if not specified.
→ Record

From that, you can see that searchGroup and searchPredicates are not properties of the object passed to this method. Which leads to the conclusion, that you have to set them after you’ve created the new smart group. The following codes works for me:

(() => {
  const app = Application("DEVONThink 3")
  const db = app.databases["TestDatabase"];
  const group = app.createLocation("SmartGroup-Test", {in: db});
  const smartGroup = app.createRecordWith({name: "New Smartgroup", 
    type: "smart group",
  }, {in: group});
  smartGroup.searchGroup = group;
  smartGroup.searchPredicates = "tags: test tag"
})()

Blatant marketing: My site deals with JXA in general and has some examples for DT (and other apps)

I suggest you create a new thread. It’s probably a good idea to keep

  • AppleScript (which this thread started with) vs. JavaScript

and

  • working AppleScript vs. questions regarding JavaScript

separated.

No error message, the only things that happens is that a smart group i created but with no query to it.

This is actually my first point of reference but sometimes I feel the dictionary is somewhat sparse.

I’m probably too new to JS/JXA to come to that conclusion just like that. Maybe that’s obious for everyone else, if not I hope my lack of knowledge resulting in threads like this can help someone else.

Thanks! I really appreciate you taking time for this

I’ve spent some time there and will probably come back many more times.

Yes, definitely. If an admin would like to split this thread I’m all for that.

1 Like

Very diplomatic wording. In some cases, it is also misleading. And for historic reasons, the wording is targeted towards AppleScript programmers.

And by “context” I meant the script. Posting a single line that does not do what you want does not really help in most cases. In the current one, it’s for example impossible to see what group really is.