Perform Smart Rule slow in applescript but not when run manually

I have a smart rule that takes < 1 sec to run manually, but when I use the command ‘Perform Smart Rule’ to run the same rule within an applescript, it is taking > 8 sec to run. Do you have any idea why this might be? Is this common?

Posting the code might get you more useful answers.

Do the script and the rule process exactly the same items?

The script and the rule should do exactly the same thing. the code is simple:

try
				perform smart rule "Set_MetaData" record theNote
			on error
				display dialog "Err Set_MetaData smart rule"
			end try

Here theNote is an rtf file and when I run the smart rule manually I run it on the same kinds of rtf files. I tried it on this same exact rtf file manually and using the code and got the same issue.

And the smart rule does exactly what?

How many matched files is the smart rule running on?

It is working on only one file. The smart rule activates an applescript that sets some custom metadata based on what kind of file it is. However, the same smart rule activates the same applescript when I run it manually and it is quick. However, when I run the smart rule at the end of another applescript, it is slow. I can see that it is this line that is slow in both the activity log of script debugger and if I comment out this line, the script runs fast.

Note that the only reason I am trying to run the smart rule within the applescript is that I can’t figure out how to get a smart rule to run on a file that is created within an applescript. I have the smart rule set to run On Creation, On Import, On Renaming, On Tagging, and On Demand, but when I create a new file within an applescript, it doesn’t run. If there is a setting in the smart rule that would make it apply to files created within an applescript, that would work also.

That is correct. Creating or modifiying items via scripting does not trigger smart rules. The potential for such behaviors to cascade out of control is high. Unintended consequences, the inability to easily stop such a chain of actions, and a high degree of difficulty in debugging such processes are some of the reasons why it’s not supported.

That is correct. Creating or modifiying items via scripting does not trigger smart rules.

That makes sense. In that case, I still want to run the smart rule within the applescript. I know there isn’t much you can tell me without more context (which would be hard to give without going into a lot of detail), but is there anything you can suggest that I try? Or have you seen this behavior before?

It’s hard to say what may be happening, especially with no clue as to the complexity of what you’re doing.

Here’s a simple example of creating an item via AppleScript and running a smart rule on it via the perform smart rule command…

tell application id "DNtp"
	set cLoc to root of think window 1
	repeat with a from 1 to 4
		create record with {name:"test" & a, type:text, content:"hi"} in cLoc
		perform smart rule name "Beep" trigger creation event -- Beeps and adds a color label
	end repeat
end tell

image

There’s no appreciable slowdown in performing the smart rule, but it’s a simple script doing simple things, so…


Note: The smart rule isn’t performed with a specified trigger unless that’s the only trigger of the smart rule being called. @cgrunenberg: Is that expected?
→ Actually, it’s running inconsistently as it’s now working when the smart rule has multiple even triggers.

And failed again…

No beeps and no labeling, though the script reports the smart rule had run…

image

1 Like

Only viewer windows might have a root, a script which uses current group instead works fine over here. In addition, the script should specify the record to which the smart rule should be applied.

I’m now having trouble just getting perform smart rule to function and realize that I am confused about its use of triggers while operating within a script on a specific record. For example, I am creating a new tag within a script and I want to perform a smart rule on the tag. When I apply the smart rule on demand to the new tag outside of the script, it performs. However, when I run it within the script, nothing happens.
The smart rule is set up as:

The code I am running that doesn’t work is:

tell application id "DNtp"
set newTagName to "newTagName"
set matchedTag to create location "/Tags/" & newTagName
log message "Created new tag: " & newTagName
-- Run smart rule fileByTags
try
  perform smart rule name "FileByTags" record matchedTag
on error
  display dialog "Err FileByTags smart rule"
end try
end tell

The rule is not performed on the new tag.

Is there something I am supposed to be doing with the trigger function? Once the new tag is created, when I apply the smart rule on demand, it works (the script attached to the smart rule is executed).

The smart rule “FileByTags” performs a script which tries to perform the same smart rule again, this is not possible.

In addition, what exactly do you want to do? Maybe a script isn’t necessary at all.

I’m not sure I understand. The smart rule FileByTags performs a script - but that script does not perform the smart rule. The Perform Smart Rule script fragment I posted was a separate script (i.e., it is not the script attached to the smart rule). The FileByTags smart rule performs a script that parses the name of the new tag and files it into specific groups based on certain criteria. I want to run this each time I create a new tag, but since smart rules won’t act on creation events that occur within a script, if I create a new tag within a new script, I need to perform the smart rule within the new script or it won’t act on the newly created tag.

If there is a way to get the smart rule to run even on tags created within a script, that would be great, but I’ve been told this isn’t possible.

The code of the script “FileByTags_v71_SR” would be useful too in this case.

That code is quite long and involved and relies on multiple script libraries. Is there anything you can tell me without it? It functions fine when I run it on demand on a tag or when the smart rule functions normally (i.e., when a new tag is created within DT3 and triggers the smart rule). Shouldn’t the smart rule act in the same way when it is called using Perform Smart Rule on a record within a script? Is there anything wrong with my syntax, given the formulation of the smart rule?

Here – The script attached to FileByTags is here (temporarily) and the library it depends on is here.

1 Like

I forgot - it also requires a plist file that is generated using this code.

1 Like

Probably the longest script I’ve ever seen :slight_smile: Instead of using perform smart rule in a script to perform a smart rule which just executes another script it’s actually recommended to execute & load the script directly.