Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a cron script that I want to run regularly if I am logged into my Mac. I don't want it to run if the Lock Screen is up. Is there a way to check from the command line / Terminal if the Lock Screen is activated?

Lion-Lock-Screen-small.png

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

This is the exact implementation I also wanted. After a BUNCH of research, I put this applescript together with contributions from abarnert at http://stackoverflow.com/questions/11505255/osx-check-if-the-screen-is-locked

set queryUserLoginState to "python -c 'import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); print d'"

tell the application "SleepDisplay" to launch
set displayOff to true
delay 2
repeat while displayOff
    set loginTest to do shell script queryUserLoginState
    if loginTest does not contain "CGSSessionScreenIsLocked = 1" then
        set displayOff to false
    end if
    delay 3
end repeat

-- do stuff!

Now, I know you asked for a terminal check to see if the screen is locked; it's implemented in the code.

I'll point in out here:

If the result of the terminal command:

python -c 'import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); print d'

contains the entry

CGSSessionScreenIsLocked = 1

Then the screen is currently locked. When it does not contain that line, the user is logged in (that is to say, the password has been entered at the lock screen).

share|improve this answer
    
excellent! +1 thank you! –  cwd Nov 1 '12 at 2:50
    
You're welcome! :P –  programmeric Nov 1 '12 at 3:46
    
Oh, I also needed to check for the variable CGSSessionOnConsoleKey as was posted in your link :) –  cwd Nov 1 '12 at 16:35
    
The only problem seems to be if this is run from a cron script then the python bit returns None instead of outputting the data. Any tips? –  cwd Nov 5 '12 at 20:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.