Detecting lock screen on Mac through Python3

北城以北 提交于 2019-12-11 18:39:20

问题


Trying to find a way to detect if the screen is locked on a Mac system, using Python3

I've tried the following, and it gives a KeyError:

import Quartz
gui_dict = Quartz.CGSessionCopyCurrentDictionary()
print(gui_dict['CGSSessionScreenIsLocked'])

edit:

import Quartz
all_windows = Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID)
for x in all_windows:
    if x["kCGWindowOwnerName"] == "loginwindow"
        print("Locked!")

This code works on Python2, but not 3 considering /usr/bin/python needs to be used to import Quartz.


回答1:


Install quartz -
pip install pyobjc-framework-Quartz

Much simple code -

import Quartz
d = Quartz.CGSessionCopyCurrentDictionary()
print('CGSSessionScreenIsLocked' in d.keys())


来源:https://stackoverflow.com/questions/52750812/detecting-lock-screen-on-mac-through-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!