Skip to content

Instantly share code, notes, and snippets.

@BobNisco
Forked from samliu/osx_sleeptimer.py
Last active January 16, 2020 02:40
Show Gist options
  • Save BobNisco/8703662 to your computer and use it in GitHub Desktop.
Save BobNisco/8703662 to your computer and use it in GitHub Desktop.
A Python script to turn off your music and optionally put your computer to sleep after a period of time!
#!/usr/bin/env python
import os
import sys
import time
from datetime import datetime, timedelta
def main():
if len(sys.argv) >= 3:
music_service = str(sys.argv[1])
minutes_to_wait = float(sys.argv[2])
handle_countdown(minutes_to_wait, music_service)
else:
print ("usage: [itunes or spotify] [minutes to wait (fractions are allowed)] "
"[true (if you want to sleep your computer after)]")
if len(sys.argv) == 4 and str_2_bool(sys.argv[3]):
sleep_computer()
def handle_countdown(minutes_to_wait, music_service):
start = datetime.now()
# Calculate when we should be done, for display purposes
finish = start + timedelta(minutes=minutes_to_wait)
# Wait the proper amount of time, while printing a timer
print "Turning off music in:"
for i in xrange(1, int(minutes_to_wait * 60)):
time.sleep(1)
newtime = datetime.now()
time_left = str(finish - newtime)
sys.stdout.write("\r" + time_left)
sys.stdout.flush()
stop_music(music_service)
def stop_music(music_service):
# Pause the music service to pause using osx's applescript
os.system("osascript -e 'tell app \"" + music_service + "\" to playpause'")
def sleep_computer():
os.system("osascript -e 'tell application \"Finder\" to sleep'")
def str_2_bool(value):
return value.lower() in ("yes", "true", "t", "1")
if __name__ == '__main__':
main()
@mc0ps
Copy link

mc0ps commented Jan 16, 2020

thanks for sleep_computer()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment