Skip to content

Instantly share code, notes, and snippets.

@andyvanee
Created July 20, 2011 05:04
Show Gist options
  • Select an option

  • Save andyvanee/1094375 to your computer and use it in GitHub Desktop.

Select an option

Save andyvanee/1094375 to your computer and use it in GitHub Desktop.
Tying a native mac UI to shell script via PyObjC.
# 2011, andyvanee
from Foundation import *
from AppKit import *
from Cocoa import *
class LockboxAppDelegate(NSObject):
configPanel = objc.IBOutlet()
srcFolder = objc.IBOutlet()
destFolder = objc.IBOutlet()
password = objc.IBOutlet()
debugDisplay = objc.IBOutlet()
startAgent = objc.IBOutlet()
statusMenu = objc.IBOutlet()
statusItem = False
statusIcon = NSImage.imageNamed_("lock_icon.png")
def taskCompletion_(self, sender):
NSLog("Task Complete")
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength)
self.statusItem.setMenu_(self.statusMenu)
self.statusItem.setImage_(self.statusIcon)
self.statusItem.setHighlightMode_(True)
@objc.IBAction
def changeSrc_(self, sender):
self.srcFolder.setStringValue_(openFile())
@objc.IBAction
def changeDest_(self, sender):
self.destFolder.setStringValue_(openFile())
@objc.IBAction
def startSync_(self, sender):
src = self.srcFolder.stringValue()
dest = self.destFolder.stringValue()
script = NSBundle.mainBundle().pathForResource_ofType_('lockbox', 'sh')
task = NSTask.alloc().init()
task.setLaunchPath_(script)
scriptout = NSBundle.mainBundle().pathForResource_ofType_('scriptout', 'txt')
task.setStandardOutput_(NSFileHandle.fileHandleForUpdatingAtPath_(scriptout))
task.launch()
while task.isRunning():
pass
status = task.terminationStatus()
old_out = self.debugDisplay.stringValue()
a = None; err = None
str_data = NSString.alloc().initWithContentsOfFile_usedEncoding_error_(scriptout, a, err)
self.debugDisplay.setStringValue_( str_data[0]+"\n"+old_out )
@objc.IBAction
def openConfigPanel_(self, sender):
self.configPanel.makeKeyAndOrderFront_(self)
NSLog("Open config")
def openFile():
panel = NSOpenPanel.openPanel()
panel.setCanCreateDirectories_(True)
panel.setCanChooseDirectories_(True)
panel.setCanChooseFiles_(False)
panel.setAllowsMultipleSelection_(False)
if panel.runModal() == NSOKButton:
return panel.filename()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment