Rule to determine if screen is currently locked?

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

Moderator: Mr_Noodle

I have crafted a Hazel embedded Applescript to import new files into a fitness app when they get added to a Dropbox folder. This app hasn't any Applescript support, so the script uses key sequences and such to get the job done. Tests and works fine when the Mac screen is unlocked, but it appears OSX blocks some of these UI actions when the screen is locked, and then the script fails. Since Hazel has matched the file since it was added, no further processing occurs upon screen unlock.

These files get added to Dropbox as soon as the fitness device (a Wahoo Elemnt Bolt) is on the home network. That's pretty much always going to be before I get to my keyboard.

I can add code to the Applescript to test for screen lock (not terribly difficult) and unlock it before running the rest of the code (ugly, as it would require putting my password into the script and keeping it in sync). I'd prefer not to do this.

Is there some existing mechanism in Hazel (I've not found it in the manual) to do the following:

When new file appears in folder
If screen is unlocked
run internal Applescript against theFile
else
leave file marked as unmatched

I believe I can "roll my own rule" to check screen lock using the "passes shell" rule style. My question then becomes: if the "passes shell" rule fails, then the new-file-match rule doesn't happen, and the file is considered not to have been matched yet, right? If so, does this create a pretty busy loop that keeps testing the "passes shell" rule until it works?

Advice on how to tackle this much appreciated.

--Richard
rlmalisz
 
Posts: 3
Joined: Thu Aug 02, 2018 6:10 pm

If your script can detect whether the screen is locked, then you can throw an error in your script. Hazel will catch it and re-try the rule later until it succeeds.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:If your script can detect whether the screen is locked, then you can throw an error in your script. Hazel will catch it and re-try the rule later until it succeeds.


So I can put the "script passes" condition ahead of the "new file" condition, and if the first condition fails, the second condition never happens, and the file won't have been matched? Excellent.

Curious: "later" is how much/often?

Modified from code on the web (and tested and known to work):
===== code ====
set queryUserLoginState to "/usr/bin/python -c 'import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); print d'"
set loginTest to do shell script queryUserLoginState
if loginTest does not contain "CGSSessionScreenIsLocked = 1" then
-- unlocked
return true
else
return false
end if
==== end code ====
rlmalisz
 
Posts: 3
Joined: Thu Aug 02, 2018 6:10 pm

Actually, I was referring to scripts in actions. For conditions, it's even more effective. If your script in a condition returns false, then the rule simply doesn't match and will act just like any other condition in that case.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Actually, I was referring to scripts in actions. For conditions, it's even more effective. If your script in a condition returns false, then the rule simply doesn't match and will act just like any other condition in that case.


And that's what I did, and it worked as hoped, using the code above. Got back to the house from a bike ride yesterday, Wahoo dropped the FIT file onto Dropbox over wireless, Hazel saw it, but screen-unlocked condition failed, so no processing. Got to my desk, unlocked screen, and about a minute later, Hazel rechecked and executed the import Applescript.

--Richard
rlmalisz
 
Posts: 3
Joined: Thu Aug 02, 2018 6:10 pm


Return to Support

cron