Skip to content

Instantly share code, notes, and snippets.

@cleverdevil
Last active Aug 29, 2015
Embed
What would you like to do?
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