Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .