Skip to content

Instantly share code, notes, and snippets.

@eunjae-lee
Last active August 29, 2015 14:16
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 eunjae-lee/02b657ca25f9a0fcab43 to your computer and use it in GitHub Desktop.
Save eunjae-lee/02b657ca25f9a0fcab43 to your computer and use it in GitHub Desktop.
Timer
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Foundation import NSUserNotification
from Foundation import NSUserNotificationCenter
from Foundation import NSUserNotificationDefaultSoundName
from optparse import OptionParser
import time
# usage : ./timer.py -t "5s" -m "This is an alarm"
def showNotification(options):
notification = NSUserNotification.alloc().init()
notification.setTitle_(options.message)
notification.setInformativeText_('Alarm')
if options.sound:
notification.setSoundName_(NSUserNotificationDefaultSoundName)
center = NSUserNotificationCenter.defaultUserNotificationCenter()
center.deliverNotification_(notification)
def parseTime(options):
table = {
's': 1,
'sec': 1,
'secs': 1,
'second': 1,
'seconds': 1,
'm': 60,
'min': 60,
'mins': 60,
'minute': 60,
'minutes': 60,
'h': 60*60,
'hour': 60*60,
'hours': 60*60
}
time = options.time
for expr in table:
if time.endswith(expr):
firstPart = time[:-(len(expr))]
if firstPart.isdigit():
return int(firstPart)
return 3*60
def getOptions():
parser = OptionParser(usage='%prog -t TITLE -m MESSAGE')
parser.add_option('-t', '--time', action='store', default='1sec')
parser.add_option('-m', '--message', action='store', default='Alarm')
parser.add_option('--no-sound', action='store_false', default=True, dest='sound')
parser.add_option('-a', '--alfred', action='store', default=None)
options, args = parser.parse_args()
if options.alfred != None:
[options.time, options.message] = options.alfred.split(" ")
return options
def main():
options = getOptions()
seconds = parseTime(options)
print "alarm after %s seconds" % (seconds)
time.sleep(seconds)
showNotification(options)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment