Smart Rule only acts on one item at a time

(Running 3.5.3build5, so this might be a beta issue, but I imagine I’m just missing something.)

I have a Smart Rule that used to work on all applicable items whenever it ran. Now, it only acts on one item at a time. The workflow moves files into “semester” folders for the year/semester it was added to the database. It used to move all applicable files at once, but now it needs to run once for every file it triggers against. (e.g., so if there were 12 files, and I was using the Hourly trigger, it would need twelve hours.)

I have looked at the conditions and tried alternative perform triggers, and I’ve re-read the scripts, but can’t fathom what would cause this behaviour. I don’t think I’d changed anything about it before the behaviour changed… any guesses as to what might be the cause?

Screen Shot 2020-10-04 at 12.44.21 PM

The first script:

property debug : false

if debug then
	tell application id "DNtp"
		set theRecords to children of (get record with uuid "BCAEEAF5-8D02-48FC-B7D1-98E612CC8855")
		repeat with eachRecord in theRecords
			my filterFolder(eachRecord)
		end repeat
	end tell
end if

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			if tags of theRecord exists then
				if tags of theRecord is not {".agnosthesis"} then
					my filterFolder(theRecord)
				end if
			end if
		end repeat
	end tell
end performSmartRule

on filterFolder(aRecord)
	tell application id "DNtp"
		set theLibrary to get record with uuid "2C954768-F5BD-44E2-A76D-D7B49A4B280A"
		set theRecord to aRecord
		set parentGroup to parent of theRecord
		if debug then
			display alert "record: " & name of theRecord
		end if
		
		set theDate to modification date of aRecord
		set theMonth to month of theDate
		set theYear to year of theDate
		if theMonth is in "January February March April" then
			set theSemester to "Winter" as string
		else if theMonth is in "May June July August" then
			set theSemester to "Summer" as string
		else if theMonth is in "September October November December" then
			set theSemester to "Fall" as string
		else
			set theSemester to ("darn. " & month of theDate & " couldn't be matched.") as string
		end if
		set newGroupLocation to ((theYear as string) & "/" & theSemester)
		if debug then
			display alert newGroupLocation
		end if
		set newGroup to create location "Library/" & newGroupLocation in theLibrary's database
		set newGroup to get record at "Library/" & newGroupLocation in theLibrary's database
		set newGroup's exclude from classification to true
		try
			display alert uuid of newGroup
		on error
			try
				display alert uuid of newGroup
			end try
		end try
		move record theRecord to newGroup from parentGroup
	end tell
end filterFolder

The second (embedded) script:


on performSmartRule(theRecords)
	tell application id "DNtp"
		set untaggedItems to {}
		repeat with eachRecord in theRecords
			if eachRecord's tags is {} then
				copy eachRecord to the end of untaggedItems
			else if eachRecord's tags is {".smudging"} then
				copy eachRecord to the end of untaggedItems
			end if
		end repeat
		set recordCount to count of untaggedItems
		set organizer to get record with uuid "BCAEEAF5-8D02-48FC-B7D1-98E612CC8855"
		
		set singleItemNotification to "One item waiting to be tagged and organized."
		set multipleItemsNotification to (recordCount as string) & " items waiting to be tagged and organized."
		if recordCount = 0 then
			tell organizer to make new reminder with properties {schedule:never}
		else if (recordCount) = 1 then
			tell organizer to make new reminder with properties {schedule:once, alarm:notification, alarm string:singleItemNotification, due date:(current date) + 10}
		else if (recordCount) is greater than 1 then
			tell organizer to make new reminder with properties {schedule:once, alarm:notification, alarm string:multipleItemsNotification, due date:(current date) + 10}
		end if
		
		
		
	end tell
end performSmartRule

I can’t see anything obvious on the first two readings; might you try adding the following?

set c to 0
repeat with theRecord in theRecords
set c to c +1
end repeat
display dialog c

Like you I am wondering whether there is a bug in the beta you are using - this little addendum would at least prove that more than one record is being passed to the script.

This has nothing to do with your question, but two things: in filterFolder, why are you using and mixing aRecord and theRecord (edit: on second thoughts, I’m not completely sure redefining “theRecord” is harmless - is there any possibility this is interfering your repeat with theRecord in theRecords block?; next edit: a quick&dirty script test suggests this is not a problem.) And what is the idea behind this code block

		try
			display alert uuid of newGroup
		on error
			try
				display alert uuid of newGroup
			end try
		end try

I really am asking - your coding is much nicer than mine :slight_smile:

Thanks for giving some thought/a read to this!

A disclaimer: My scripting is a little like a single monkey with a single typewriter and infinite time. I often have an idea and think, “oh, that should be easy to do! I’ll just… no, maybe… :anger: okay, well, let me try…”—ten hours later, a script version of Frankenstein’s monster is functional, but inexplicable.

The aRecord ← → theRecord flipping is absolutely probably needless. I think it’s a holdover from some other language I learned where clearly declaring variables was a good idea. But yes, in my experience, it hasn’t caused problems (that I know of).

As for the double-try block… I think that was a lazy way of making sure that the destination group existed before moving the target file to it. It might even be a leftover attempt at debugging something about the script, ha.


…Well, sometimes all it takes is a few good questions. I did some debugging and cleanup and now it seems to work again!

It is slow, however. It takes a minute or so before it decides to run on all of them. But that’s fine, it’s supposed to work in the background anyway.

The cleaned up version of the first script is below. (I removed the try blocks. :wink:)

property debug : false

if debug then
	tell application id "DNtp"
		set theOrganizer to (get record with uuid "BCAEEAF5-8D02-48FC-B7D1-98E612CC8855")
		set theRecords to children of theOrganizer
		repeat with eachRecord in theRecords
			my filterFolder(eachRecord)
		end repeat
	end tell
end if

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			if tags of theRecord exists then
				if tags of theRecord is not {".agnosthesis"} then
					my filterFolder(theRecord)
				end if
			end if
		end repeat
	end tell
end performSmartRule

on filterFolder(aRecord)
	tell application id "DNtp"
		set theLibrary to get record with uuid "2C954768-F5BD-44E2-A76D-D7B49A4B280A"
		set recordToMove to aRecord
		set theOrganizer to get record with uuid "BCAEEAF5-8D02-48FC-B7D1-98E612CC8855"
		if debug then
			display alert "record: " & name of recordToMove
		end if
		
		set theDate to modification date of recordToMove
		set theMonth to month of theDate
		set theYear to year of theDate
		if theMonth is in "January February March April" then
			set theSemester to "Winter" as string
		else if theMonth is in "May June July August" then
			set theSemester to "Summer" as string
		else if theMonth is in "September October November December" then
			set theSemester to "Fall" as string
		else
			set theSemester to ("darn. " & month of theDate & " couldn't be matched.") as string
		end if
		set newGroupLocation to ((theYear as string) & "/" & theSemester)
		if debug then
			display alert newGroupLocation
		end if
		set newGroup to create location "Library/" & newGroupLocation in theLibrary's database
		set newGroup to get record at "Library/" & newGroupLocation in theLibrary's database
		set newGroup's exclude from classification to true
		set recordAtNewLocation to move record recordToMove to newGroup from theOrganizer
	end tell
end filterFolder
1 Like

How many items are being matched by the rule?

Generally less than 10-15 at a time.

If it’s a low-priority rule and runs in the background, why not run it daily instead of hourly?

Actually, before synchronization was my favourite trigger setting; I think I switched it to hourly when I was fiddling with sync settings a while back and didn’t switch it.

But yes, daily would probably work fine too!

Yeah - there’s no need to add CPU cycles to a process that won’t get matches or need to run that often.

1 Like