17-second delay before script begins running

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

17-second delay before script begins running Mon Jan 29, 2024 8:17 am • by ErikMH
Hello, everyone —

I’m at my wits’ end here trying to figure out why there is a 17-second delay after a rule match before a script runs.

Scenario:

A file matches a set of rules, and processing of actions begins. This is typically about 1 or 2 seconds after the file has appeared in a watched folder, or changes its name. This delay is perfectly acceptable and expectable, of course.

My first action sets a color label so that I can tell at a glance that the file has been matched and that actions are being processed. Despite this being Finder, it seems to occur almost instantly.

The second action calls a brief script that does nothing other than calling a Keyboard Maestro macro. KM is flexible in how it is called, and I have tried both AppleScript and KM’s new keyboardmaestroCLI (shell) command — I would guess that both are actually just invoking AppleEvents if I looked behind the curtain. In both cases, I can always count to (exactly) 17 before KM begins its first step.

The macro runs fine and does what I expect, and Hazel then runs the final action on the file: removing the color label. It does this instantaneously.

My first thought was that this was a Keyboard Maestro, or more likely an AppleEvent problem. Easily tested: I invoked the same CLI command from Terminal. The macro began instantaneously. So the problem would seem to be with Hazel.

Just to be sure, I created a new KM script that had only one step, rather than my initial complicated one. 17-second delay if invoked from Hazel.

I’m running macOS 14.3 on an M1 MacBook Pro Max with 64GB RAM; not the fastest laptop on the planet, but 24 months ago it was, so that really shouldn’t be an issue.

Current versions of both Keyboard Maestro and Hazel.

Nothing helpful in the logs (about 19 seconds of no entries, with all logging turned on). The only thing I can think might be relevant is that I always see macOS’s little blue “Locations” indicator light pop up in my menubar at about the 3-second mark.

Any ideas? I can live with this, but it doesn’t seem to be working quite as designed....
ErikMH
 
Posts: 36
Joined: Sat Jun 21, 2008 8:49 am
Location: northern Vermont

How are you invoking it from Hazel? AppleScript or shell script?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Sorry, I meant to make that clear. I tried both, with exactly the same results.
ErikMH
 
Posts: 36
Joined: Sat Jun 21, 2008 8:49 am
Location: northern Vermont

Can you post both the scripts?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: 17-second delay before script begins running Tue Jan 30, 2024 12:54 pm • by ErikMH
They’re about as basic as can be.

Here’s the CLI one-line shell “script” that would be my first choice:

Code: Select all
km AE9D575F-2E64-486C-9A53-01E727C30F49 --parameter $1


where `km` is an alias defined thus:

Code: Select all
alias km='/Applications/Keyboard\ Maestro.app/Contents/MacOS/keyboardmaestro'


And the AppleScript (the traditional way to call KM from Hazel) looks like this:

Code: Select all
tell application "Keyboard Maestro Engine"
   do script "AE9D575F-2E64-486C-9A53-01E727C30F49" with parameter $1
end tell


In this case, after I save it, Hazel seems to replace the `$1` with `theFile`. That does not seem to happen with the shell script.
ErikMH
 
Posts: 36
Joined: Sat Jun 21, 2008 8:49 am
Location: northern Vermont

Huh.

If I don’t set the color label as the first action, there is no 17-second delay. This is weird, because the label changes in the Finder instantly. So I’m guessing there’s a long delay before Hazel realizes that the label has indeed changed.

So for now, I guess I will just forgo the safety net of the color labels. Problem solved! Or, at least worked around....
ErikMH
 
Posts: 36
Joined: Sat Jun 21, 2008 8:49 am
Location: northern Vermont

You are setting the label in Hazel in the same rule? It might be that Hazel needs to query the tags on the system along with their color associations, which may take some time depending on the system. Maybe try setting a tag instead and seeing if it behaves any differently?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: 17-second delay before script begins running Wed Jan 31, 2024 10:09 am • by ErikMH
You called it: no delay whatsoever if I set (and clear) a tag instead of a color label. I’ll just do that then. It’s cleaner anyway, since I can make it more self-evident as to what’s going on with a good tag name like “HazelLock”.

Sounds like perhaps some type of Spotlight or other OS datastore needs rebuilding in order to use color labels in the Finder more efficiently? Any thoughts about what that might be? (Not that I have any immediate plans to put color labels into use, but just to keep things in proper working order.)
ErikMH
 
Posts: 36
Joined: Sat Jun 21, 2008 8:49 am
Location: northern Vermont

Basically, Apple does not provide any API for getting the list of tags on your system, nor the associations between the tags and their colors. All of this I had to reverse-engineer and it involves checking a combination of things, including using a Spotlight query.

If you are using tags directly, except for the UI, there shouldn't be a delay in assigning them.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support