Skip to content

Instantly share code, notes, and snippets.

@cleverdevil
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleverdevil/76977d4ca6b575b298f8 to your computer and use it in GitHub Desktop.
Save cleverdevil/76977d4ca6b575b298f8 to your computer and use it in GitHub Desktop.
Script to lock your Mac via the screensaver when you unplug your iPhone
#!/usr/bin/env python
import subprocess
import threading
import time
class Scanner():
def __init__(self):
self.stop = threading.Event()
self.iphone_present = None
def start(self):
while not self.stop.is_set():
self.scan()
time.sleep(1)
def scan(self):
output = subprocess.check_output(['/usr/sbin/ioreg'])
if 'iPhone' in output:
self.iphone_present = True
else:
if self.iphone_present:
self.lock()
self.iphone_present = False
def lock(self):
subprocess.call([
'open', '-a',
'/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app'
])
def quit(self):
self.stop.set()
try:
scanner = Scanner()
scanner.start()
except KeyboardInterrupt:
scanner.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment