Skip to content

Instantly share code, notes, and snippets.

@glutanimate
Forked from dsandler/touchbar_demo.py
Created June 24, 2018 07:40
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 glutanimate/c5dfd52461e0c1cc13ab428fb3424b1f to your computer and use it in GitHub Desktop.
Save glutanimate/c5dfd52461e0c1cc13ab428fb3424b1f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# touchbar_demo.py
# dsandler@dsandler.org
# PyObjC proof of concept NSTouchBar implementation.
# Note that exceptions in the delegates will crash with "Illegal instruction: 4"
# rather than printing helpful stack traces; look in CrashReporter for the
# exception string.
import objc
from AppKit import \
NSObject, NSTouchBar, NSButton, NSCustomTouchBarItem, \
NSColor, NSApplication, NSApp
from PyObjCTools import AppHelper
NSApplicationDelegate = objc.protocolNamed('NSApplicationDelegate')
NSTouchBarProvider = objc.protocolNamed('NSTouchBarProvider')
class AppDelegate (NSObject, NSTouchBarProvider, NSApplicationDelegate):
FOO_BUTTON = "fooButton"
def applicationDidFinishLaunching_(self, aNotification):
print("Hello, World!")
def buttonClicked_(self, view):
print("Clicked: " + repr(view))
def touchBar_makeItemForIdentifier_(self, bar, ident):
print("makeItemForIdentifier: " + ident)
if ident == AppDelegate.FOO_BUTTON:
item = NSCustomTouchBarItem.alloc().initWithIdentifier_(ident)
print("made item: " + repr(item))
button = NSButton.buttonWithTitle_target_action_(
"foo", self, "buttonClicked:")
button.setBezelColor_(
NSColor.colorWithRed_green_blue_alpha_(
1.0, 0.0, 0.0, 1.0))
print("made button: " + repr(button))
item.setCustomizationLabel_(ident)
item.setView_(button)
return item
# NSTouchBarProvider protocol
def touchBar(self):
print('touchBar start')
tb = NSTouchBar.alloc().init()
print('alloc')
tb.setDelegate_(self)
print('setDelegate_')
#tb.setCustomizationIdentifier_('fooBar')
tb.setDefaultItemIdentifiers_([AppDelegate.FOO_BUTTON])
#tb.setCustomizationAllowedItemIdentifiers_(['fooButton'])
print('touchBar: ' + repr(tb))
return tb
def main():
app = NSApplication.sharedApplication()
app.setAutomaticCustomizeTouchBarMenuItemEnabled_(True)
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment