Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yeonsh/9b084059c4eb48a730a9 to your computer and use it in GitHub Desktop.
Save yeonsh/9b084059c4eb48a730a9 to your computer and use it in GitHub Desktop.
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
start_time = NSDate.date()
class MyApplicationAppDelegate(NSObject):
state = 'idle'
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength)
print self.statusItem
self.statusItem.setTitle_(u"Hello World")
self.statusItem.setHighlightMode_(TRUE)
self.statusItem.setEnabled_(TRUE)
# set click action handler
self.statusItem.setTarget_(self)
self.statusItem.setAction_('click:')
statusBarButton = self.statusItem.button()
print statusBarButton
rectInWindow = statusBarButton.convertRect_toView_(statusBarButton.bounds(), None)
screenRect = statusBarButton.window().convertRectToScreen_(rectInWindow)
stringRect = NSStringFromRect(screenRect)
NSLog(stringRect)
# Get the timer going
self.timer = NSTimer.alloc().initWithFireDate_interval_target_selector_userInfo_repeats_(start_time, 5.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
# click action handler
def click_(self, sender):
print sender
if __name__ == "__main__":
app = NSApplication.sharedApplication()
delegate = MyApplicationAppDelegate.alloc().init()
app.setDelegate_(delegate)
#image = NSImage.alloc().initWithContentsOfFile_("/Users/bluenred/Work/netdrive-2/UI/res/NetDrive.png")
AppHelper.runEventLoop()
@yeonsh
Copy link
Author

yeonsh commented Sep 24, 2015

Sample output

2015-09-25 07:49:54.276 python[4347:36517] Application did finish launching.
<NSStatusItem: 0x7ff2bb78d410>
<NSStatusBarButton: 0x7ff2bb5bcf20>
2015-09-25 07:49:54.421 python[4347:36517] {{1998, 1418}, {88, 22}}
idle
idle

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