Automate creation of daily (date ranged) Smart Groups

Hey gang!

I’m hoping someone has a script, workflow, template or advice to speed up what is currently a manual (and long) process.

What I’m trying to do

Automate the creation of Smart Groups for the entire year with months (as Groups) and days (as Smart groups) similar to Template » Registers » 1-31 but with the search criteria for each day already inbuilt.

For example:

2021 (Group)
09 September (Nested Group)

  • 2021-09-01 (Smart Group)
  • 2021-09-02 (Smart Group)
  • 2021-09-30 (Smart Group)

If there is no way to automate this process, if anyone has a link to a Custom template that has already been setup by another user and shared, I’d be very grateful.

Thank you all for your help in advance!

It’s not yet possible to script global smart groups (the ones in the sidebar), only local smart groups inside databases would be possible that way. E.g. here’s a basic example:

property pMonths : {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
property pDays : {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

tell application id "DNtp"
	set theGroup to current group
	repeat with m from 1 to 12
		set theMonthName to (m as string) -- item i of pMonths
		set theMonthGroup to create record with {name:theMonthName, type:group} in theGroup
		set theMonthQuery to "2021-" & (m as string) & "-"
		set numDays to item m of pDays
		repeat with d from 1 to numDays
			set theDayQuery to theMonthQuery & (d as string)
			set theDayBeginQuery to "modificationDate>=" & theDayQuery & " 00:00:00"
			set theDayEndQuery to "modificationDate<=" & theDayQuery & " 23:59:59"
			set theQuery to theDayBeginQuery & " " & theDayEndQuery
			create record with {name:(d as string), type:smart group, search predicates:theQuery} in theMonthGroup
		end repeat
	end repeat
end tell

However, I wonder whether so many smart groups are actually necessary/useful. Did you have a look at Tools > Filter > Info and the possibility to limit the current main view to items with a certain date or in a date range? Filters can be also applied to global smart groups or search results.

Thank you for this script and for the further advice. I’ll have a play around with both options.

The reason behind using Smart Groups is primarily to shortcut the act of searching. I have data stored in contextual groups within a single database, but when I want a quick overview of everything I might have been reading, writing or researching on that day, week or month, I can quickly navigate to the parent group (say March 2021) and expand all the contained Smart Groups to quickly see an aggregation of those documents. This helps me to visually track and place my thought process within context to that period of time (how an image saved and stored in one group might have inspired a note stored in another group or aided further research stored as a pdf in another group).

I’m not very proficient as yet with the advanced search options, so for me, Smart Groups keeps things accessible and easy to find.

Thank you again for your help! Really appreciate it! :grinning: :+1:

The script works great! Thank you :grinning:

I have been able to figure out how to change the year set theMonthQuery to "2021-" & (m as string) & "-" by replacing 2021 with my required year, and changing modificationDate to creationDate. A huge deal for me and I’m still learning! :raised_hands:

But I can’t figure out how to add three more search criteria’s – exclude groups, tags and smart groups.

I’d be grateful for any advice! Thank you again for your help!!

1 Like

The easiest way to figure out the right search syntax is to set up an advanced search via the user interface. The search term in the toolbar gets automatically updated. In this case just append kind:any

Thanks for the help! Following your instructions I have the following:

kind:tag
kind:group
kind:smart group

But how do I actually write this in the script to translate as kind is not tag, kind is not group and kind is not smart group?

I’m guessing it has something to do with `set’

set theKindQuery to (is not) tag ?

I’m kinda lost with what I need to do. Could you provide me with an example to follow please?

Actually you don’t need these conditions, kind:any is sufficient:

set theQuery to theQuery & " kind:any"

This limits results to documents.

That works great! Thank you so much for your help! :raised_hands:

Here’s a variant that creates groups by year and month:

property pMonths : {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
property pDays : {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
property pYears : {2013, 2022}
-- adjust pYears to your start, end

tell application id "DNtp"
	set theGroup to current group
	repeat with y from item 1 of pYears to item 2 of pYears
		set theYearGroup to create record with {name:(y as string), type:group} in theGroup
		repeat with m from 1 to 12
			set theMonthName to (m as string) -- item i of pMonths
			-- 		set theMonthGroup to create record with {name:theMonthName, type:group} in theGroup
			set theMonthQuery to (y as string) & "-" & (m as string) & "-"
			set numDays to item m of pDays
			set theDayBeginQuery to "modificationDate>=" & theMonthQuery & "1" & " 00:00:00"
			set theDayEndQuery to "modificationDate<=" & theMonthQuery & (numDays as string) & " 23:59:59"
			set theQuery to theDayBeginQuery & " " & theDayEndQuery
			create record with {name:(item m of pMonths as string), type:smart group, search predicates:theQuery} in theYearGroup
		end repeat
	end repeat
end tell

Hey @cgrunenberg , hope you’re keeping well. I’d be grateful for some help, please. I’m trying to automate smart groups for weeks this time. My school uses the format 2240, 2241, 2242, etc to denote year-week for admin tasks and I’d like to create a smart group index that aggregates all documents in the database with this year-week format in the filename.

This is how I’m setting it up:


Is there a script I can batch create smart groups for the full year (weeks 01 to 52) in format of YY-WW?

Apprectiate the help in advance :blush::pray:

I’m curious as to why you’d set up 52 individual smart groups?

Hey @BLUEFROG , what would you suggest? :blush:

tell application id "DNtp"
	set theGroup to current group
	repeat with i from 1 to 52
		if i < 10 then
			set theName to "220" & (i as string)
		else
			set theName to "22" & (i as string)
		end if
		set thePredicate to "name:<" & theName
		create record with {name:theName, type:smart group, search predicates:thePredicate} in theGroup
	end repeat
end tell

Just to clarify: :< does not mean “smaller than” but “begins with” (I was wondering about it, so I looked it up in the Lovely Manual).

1 Like

That’s right. And no need to use the manual in this case, after using the advanced search the toolbar search string is automatically updated. IMHO the easiest way to figure out the right syntax.

1 Like

I was just curious if that was your actual intention and trying to imagine why.

  • Are you tagging the files with the week?
    • If not, why not just create groups?

Thank you @cgrunenberg , the script creates smart folders perfectly. Is there a way to change the ‘begins with’ to ‘contains’ in the criteria?

Hey @BLUEFROG , in response to your question I need something as a quick and easy catch-all where tags might not have been added to the document and where I can see everything aggregated in one place. Sometimes things slip through the cracks, and a smart folder is quicker than repetitive searches.

Hey @chrillek , quick question… which manual are you referencing? I’m searching the Help section in app, and the Take Control book… but I can’t find what you’re talking about :see_no_evil:

“contains” is :~ as described in the Appendix > Search Prefixes > Prefix Operators section of the help and manual