Last active
December 9, 2024 16:08
-
-
Save RhetTbull/726e1780bf6cec8edef6352bc32659b0 to your computer and use it in GitHub Desktop.
Implements a minimalist macOS Cocoa application in python using pyobjc that doesn't require a NIB file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Implements a minimalist Cocoa application using pyobjc | |
Based on the example found here: | |
https://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html | |
To run this: | |
- save as minimal.py | |
- pip3 install pyobjc | |
- python3 minimal.py | |
For a more complete example, see https://github.com/RhetTbull/appkitgui | |
""" | |
import objc | |
from AppKit import ( | |
NSApp, | |
NSApplication, | |
NSApplicationActivationPolicyRegular, | |
NSBackingStoreBuffered, | |
NSMakeRect, | |
NSMenu, | |
NSMenuItem, | |
NSProcessInfo, | |
NSTitledWindowMask, | |
NSWindow, | |
) | |
from Foundation import NSMakePoint | |
def main(): | |
"""Create a minimalist window programmatically, without a NIB file.""" | |
with objc.autorelease_pool(): | |
# create the app | |
NSApplication.sharedApplication() | |
NSApp.setActivationPolicy_(NSApplicationActivationPolicyRegular) | |
# create the menu bar and attach it to the app | |
menubar = NSMenu.alloc().init().autorelease() | |
app_menu_item = NSMenuItem.alloc().init().autorelease() | |
menubar.addItem_(app_menu_item) | |
NSApp.setMainMenu_(menubar) | |
app_menu = NSMenu.alloc().init().autorelease() | |
# add a menu item to the menu to quit the app | |
app_name = NSProcessInfo.processInfo().processName() | |
quit_title = f"Quit {app_name}" | |
quit_menu_item = ( | |
NSMenuItem.alloc() | |
.initWithTitle_action_keyEquivalent_(quit_title, "terminate:", "q") | |
.autorelease() | |
) | |
app_menu.addItem_(quit_menu_item) | |
app_menu_item.setSubmenu_(app_menu) | |
# create the window | |
window = ( | |
NSWindow.alloc() | |
.initWithContentRect_styleMask_backing_defer_( | |
NSMakeRect(0, 0, 200, 200), | |
NSTitledWindowMask, | |
NSBackingStoreBuffered, | |
False, | |
) | |
.autorelease() | |
) | |
window.cascadeTopLeftFromPoint_(NSMakePoint(20, 20)) | |
window.setTitle_(app_name) | |
window.makeKeyAndOrderFront_(None) | |
# run the app | |
NSApp.activateIgnoringOtherApps_(True) | |
NSApp.run() | |
return 0 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For a more complete example, see appkitgui which expands on these ideas to build a complete macOS app programmatically with python.