Skip to content

Instantly share code, notes, and snippets.

@scturtle
Created January 3, 2015 06:57
Show Gist options
  • Save scturtle/41fe6ee265f7b96ef1bf to your computer and use it in GitHub Desktop.
Save scturtle/41fe6ee265f7b96ef1bf to your computer and use it in GitHub Desktop.
show flight price on OS X system status bar
# coding: utf8
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
import os
import re
start_time = NSDate.date()
class MyApplicationAppDelegate(NSObject):
state = 'idle'
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(
NSVariableStatusItemLength)
self.statusItem.setTitle_(u"Hello World")
self.statusItem.setHighlightMode_(TRUE)
self.statusItem.setEnabled_(TRUE)
# menu
self.menu = NSMenu.alloc().init()
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Sync...', 'sync:', '')
self.menu.addItem_(menuitem)
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Quit', 'terminate:', '')
self.menu.addItem_(menuitem)
self.statusItem.setMenu_(self.menu)
# Get the timer going
self.timer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_(
start_time, 60.0, self, 'tick:', None, True)
NSRunLoop.currentRunLoop().addTimer_forMode_(
self.timer, NSDefaultRunLoopMode)
self.timer.fire()
def sync_(self, notification):
print "sync"
def tick_(self, notification):
# print self.state
cmd = open('cmd.txt').read().strip()
os.system(cmd)
output = open('output.html').read()
result = output[output.index('<table cellspacing="1" id="FFCR"'):
output.index('所顯示票價為一位成人的票價')]
prices = re.findall(r"value='([^']*)'", result)
assert len(prices) == 49
self.statusItem.setTitle_(' '.join('HKD ' + p for p in prices[24:25]))
newmenu = NSMenu.alloc().init()
for i in xrange(15, 15 + 7):
for j in xrange(21, 21 + 7):
menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'2.{}-2.{} HKD {}'.format(i, j, prices[7 * (i - 15) + (j - 21)]), 'sync:', '')
newmenu.addItem_(menuitem)
self.statusItem.setMenu_(newmenu)
del self.menu
self.menu = newmenu
print 'updated'
if __name__ == "__main__":
app = NSApplication.sharedApplication()
delegate = MyApplicationAppDelegate.alloc().init()
app.setDelegate_(delegate)
AppHelper.runEventLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment