Smartgroups in Template

I’ve modified the projects template (Data > New from Template > Productivity > Projects) to create a group/directory structure and includes commonly created files that meets my needs for recurring projects. This works well.

I would like for this template to include Smartgroups that find certain certain records (e.g. all *.EML files or all *.PPTX files only in the project group and subgroups). This works fine if I manually set the “Search In” location for the smart group, but it would be nice to automate this during the creating of the project (as there are several such smartgroups for each project).

Is there a way to automatically limit the scope of the smart group to the group in which it resides (and subgroups thereof), either by specifying a “Search In” location that is relative to the location of the smart group or by creating a smart rule or Applescript that can quickly modify the new smart groups post creation?

I’m not sure about templates, but you can create smart groups programmatically, e.g. in AppleScript. And the documentation says

smart group n  > [item] : A smart group.
properties
search group [record] : Group of the smart group to search in.
search predicates [text] : A string representation of the conditions of the smart group.

So, yes, it is possible to set the group and search string in a smart rule with AppleScript

Thanks you for pointing me in the right direction - using this, the following is a working code excerpt that did what I needed in case it is useful to anyone else:

tell application id "DNtp"

activate

set smartGroups to {¬
	{name:"01 Emails", predicate:"any: kind:email"}, ¬
	{name:"02 Meetings", predicate:"any: tags:Meeting"}, ¬
	{name:"03 Logs", predicate:"any: tags:Logs"}, ¬
	{name:"04 Documents", predicate:"any: extension==doc extension==docx extension==rtf extension==rtfd extension==scriv extension==pages"} }

set theGroup to current group
set newGroup to create record with {name:"98 Browser", type:group} in theGroup

repeat with sg in smartGroups
	create record with {name:name of sg, type:smart group, search group:theGroup, search predicates:predicate of sg} in newGroup
end repeat

end tell

I wasn’t aware that one can write this kind of JavaScriptish code in AppleScript. Looks good.

One note: I doubt that you need the "any: " in your predicates. Especially if there’s only one condition :wink:

I don’t see what’s Javascriptish here. Looks like normal, well-written, and perfectly functional AppleScript to me :slight_smile:

One note: I doubt that you need the "any: " in your predicates. Especially if there’s only one condition

While is may be technically correct, it’s a good habit as it shows an awareness of the aspects of a smart group and adding it is a good habit to be in.

I was referring to the array of records (which would be an array of objects in JS) and the repeat loop. Before this example, I hadn’t seen this kind of abstraction in AppleScript code (probably my bad). In JS, one would write

let smartGroups = [ 
	{name:"01 Emails", predicate:"any: kind:email"}, ¬
	{name:"02 Meetings", predicate:"any: tags:Meeting"}, ¬
	{name:"03 Logs", predicate:"any: tags:Logs"}, ¬
	{name:"04 Documents", predicate:"any: extension==doc extension==docx extension==rtf extension==rtfd extension==scriv extension==pages"} 
]
…
smartGroups.forEach(sg => {
  createRecordWith({name: sg.name, type: "smart group",… "search predicates": sg.predicate});

May be both languages have more similarities than I thought.

Nested records aren’t as commonly used when people code, but they’re somewhat common in the output of properties of things in many instances.