Skip to content

Instantly share code, notes, and snippets.

@MagerValp
Created February 27, 2018 18:41
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save MagerValp/c659f631870c95fb91693d11466b3221 to your computer and use it in GitHub Desktop.
Macadmin parenting
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
import io
import sys
import codecs
if sys.stdout.encoding != "UTF-8":
sys.stdout = codecs.getwriter("utf-8")(sys.stdout, "strict")
if sys.stderr.encoding != "UTF-8":
sys.stderr = codecs.getwriter("utf-8")(sys.stderr, "strict")
import argparse
import time
import subprocess
def notify(title, message, sound="default"):
msg = 'display notification "{}" with title "{}" sound name "{}"'.format(message, title, sound)
subprocess.call(["/usr/bin/osascript", "-e", msg])
PERIOD = 60
def measure_and_notify(launchtime):
while True:
sleep_time = PERIOD - (time.time() - launchtime) % PERIOD
time.sleep(sleep_time)
session_time = time.time() - launchtime
if int(session_time / PERIOD) == 55:
notify("Playtime", "5 minutes left")
if int(session_time / PERIOD) == 58:
notify("Playtime", "2 minutes left")
if int(session_time / PERIOD) == 59:
notify("Playtime", "1 minute left", "Glass")
if int(session_time / PERIOD) >= 60:
notify("Playtime", "Time is up!", "Basso")
def main(argv):
p = argparse.ArgumentParser()
p.add_argument("-v", "--verbose", action="store_true",
help="Verbose output.")
args = p.parse_args([x.decode("utf-8") for x in argv[1:]])
measure_and_notify(time.time())
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>se.automac.playtime</string>
<key>LimitLoadToSessionType</key>
<array>
<string>Aqua</string>
</array>
<key>Program</key>
<string>/Users/Shared/playtime.py</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment