Skip to content

Commit a29724f

Browse files
committed
added is_screen_locked.py
1 parent e83c87b commit a29724f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

applescript/is_screen_locked.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
# vim:ts=4:sts=4:sw=4:et
4+
#
5+
# Author: Hari Sekhon
6+
# Date: 2022-12-05 22:03:56 +0000 (Mon, 05 Dec 2022)
7+
#
8+
# https://github.com/HariSekhon/DevOps-Bash-tools
9+
#
10+
# License: see accompanying Hari Sekhon LICENSE file
11+
#
12+
# If you're using my code you're welcome to connect with me on LinkedIn
13+
# and optionally send me feedback to help steer this or other code I publish
14+
#
15+
# https://www.linkedin.com/in/HariSekhon
16+
#
17+
18+
"""
19+
20+
Detects whether the macOS screen is locked
21+
22+
If locked, prints 'true' and returns exit code 0
23+
If unlocked, prints 'false' and returns exit code 1
24+
25+
Useful to avoid automated keystrokes or mouse_clicks while on locked screen which can make it hard to login back in
26+
27+
"""
28+
29+
import sys
30+
import Quartz
31+
32+
if __name__ == '__main__':
33+
# false positive
34+
# pylint: disable=no-member
35+
d = Quartz.CGSessionCopyCurrentDictionary()
36+
if 'CGSSessionScreenIsLocked' in d and d['CGSSessionScreenIsLocked'] == 1:
37+
print 'true'
38+
sys.exit(0)
39+
else:
40+
print 'false'
41+
sys.exit(1)

0 commit comments

Comments
 (0)