cocoabits / MASShortcut

Modern framework for managing global keyboard shortcuts compatible with Mac App Store. More details:
http://blog.shpakovski.com/2012/07/global-keyboard-shortcuts-in-cocoa.html
BSD 2-Clause "Simplified" License
1.52k stars 220 forks source link

add BridgeSupport generation to build #81

Open glyph opened 8 years ago

glyph commented 8 years ago

I was able to use MASShortcut almost immediately from Python, but first I had to do this:

gen_bridge_metadata -f ./MASShortcut.framework | sed -e "s/function_pointer='true'/block='true'/g" > MASShortcut.framework/Resources/BridgeSupport/MASShortcut.bridgesupport

It would be nice if this were built in to the framework. (I'm also not sure if a nicer way to do that sed exists...)

zoul commented 8 years ago

Wow, I had no idea you could use frameworks from Python. (Have some pointers to further documentation?) What would we have to change in the build process? Just run a post-build script? How do we know it works?

glyph commented 8 years ago

The steps are pretty straightforward. A post-build script that runs that exact command should do it. (You lucked out; some frameworks require extensive, weird post-processing to speak Python's dialect of BridgeSupport, but MASShortcut seems like it's in good shape out of the box.)

The next step is you have to create a Python package that describes the framework. I put the following into build/Release/MASShortcut.py right next to MASShortcut.framework:

import objc as _objc
__bundle__ = _objc.initFrameworkWrapper(
    __name__,
    frameworkIdentifier="com.github.shpakovski.MASShortcut",
    frameworkPath=_objc.pathForFramework("MASShortcut.framework"),
    globals=globals()
)

This should work out-of-the-box with system python, but if it needs a new pyobjc for some reason, python -m ensurepip --user && python -m pip install --user pyobjc should build and install all the relevant dependencies.

You can then package this using http://pythonhosted.org/py2app/ by specifying MASShortcut as a required framework; it'll bundle it in to the app bundle automatically.

glyph commented 8 years ago

As far as knowing it works; I'm not sure what your automated test situation is like, but here's a script that imports the framework and does a simple thing:


from AppKit import NSApplication
app = NSApplication.sharedApplication()

import MASShortcut

# snarfed from /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
kVK_ANSI_Q                    = 0x0C

from AppKit import NSCommandKeyMask, NSControlKeyMask
shortcut = MASShortcut.MASShortcut.shortcutWithKeyCode_modifierFlags_(
    kVK_ANSI_Q, NSCommandKeyMask | NSControlKeyMask
)
monitor = MASShortcut.MASShortcutMonitor.sharedMonitor()
def playShortcutFeedback():
    print("shortcut activated; goodbye!")
    app.terminate_(None)
monitor.registerShortcut_withAction_(
    shortcut, playShortcutFeedback
)

from PyObjCTools.AppHelper import runEventLoop
runEventLoop()
glyph commented 8 years ago

BTW if you are curious about doing various Mac tricks with Python, you may find https://glyph.twistedmatrix.com/2015/07/just-a-button.html interesting.

glyph commented 8 years ago

Oh, sorry. I jumped right to the back of the book. If you want to get started with calling some cocoa APIs with Python, https://pythonhosted.org/pyobjc/ is the relevant tool :).