如何透過 OS X 上的命令列判斷鎖定畫面是否處於作用中?

如何透過 OS X 上的命令列判斷鎖定畫面是否處於作用中?

我有一個 cron 腳本,如果我登入我的 Mac,我想定期運行它。如果鎖定螢幕打開,我不希望它運行。有沒有辦法從命令列/終端機檢查鎖定畫面是否已啟動?

答案1

這正是我想要的實現。經過大量研究後,我將這個 applescript 與 abarnert 的貢獻放在一起https://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!

現在,我知道您要求進行終端檢查以查看螢幕是否已鎖定;它在代碼中實現。

我在這裡指出:

如果終端命令的結果:

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

包含條目

CGSSessionScreenIsLocked = 1

然後螢幕目前被鎖定。當它不包含該行時,用戶已登入(也就是說,密碼已在鎖定畫面上輸入)。

相關內容