If you are on macOS prior to macOS Monterey 12.3, then you have the Python/Objective-C bridge already inherent with Python 2.7.16. After Monterey 12.3, Apple removed the [all] Python 2.7.16 distribution from the operating system and withit went the PyObjc bridge.
If you installed Python3.11.3 from Python.org, it too must have pyobjc installed in order to have functional pyobjc.
/usr/local/bin/pip3 install -U pip
/usr/local/bin/pip3 install -U pyobjc
As of yesterday, that would install the pyobjc frameworks v9.0.1 into:
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages
In your Python3 program:
#!/usr/bin/env python3
from Cocoa import (NSObject, NSURL, NSURLRequest, NSString, NSWindow, NSApplication,
NSTerminateNow, NSMakeRect, NSTitledWindowMask,
NSClosableWindowMask, NSResizableWindowMask,
NSMiniaturizableWindowMask, NSBackingStoreBuffered)
import WebKit
import sys
class AppDelegate(NSObject):
def init(self):
self = super(AppDelegate, self).init()
if self is None:
return None
return self
def applicationDidFinishLaunching_(self, aNotification):
#
self.pageurl = NSURL.URLWithString_("https://discussions.apple.com")
#self.apath = (NSString.stringWithString_("~/Documents/CSS_Styles/ASC.css")
# .stringByExpandingTildeInPath())
# self.mycssURL = NSURL.fileURLWithPath_(self.apath)
self.rect = NSMakeRect(100.0, 350.0, 950.0, 825.0)
self.mask = (NSTitledWindowMask | NSClosableWindowMask |
NSResizableWindowMask | NSMiniaturizableWindowMask)
self.win = (NSWindow.alloc()
.initWithContentRect_styleMask_backing_defer_(self.rect,
self.mask,
NSBackingStoreBuffered,
False))
self.win.display()
self.win.orderFrontRegardless()
self.webview = WebKit.WebView.alloc()
self.webview.initWithFrame_(self.rect)
self.webview.preferences().setUserStyleSheetEnabled_(True)
# self.cssurl = NSURL.URLWithString_("file:///Users/ckhowe/Documents/ASC.css")
# self.webview.preferences().setUserStyleSheetLocation_(self.cssurl)
# if CSS file cannot be found, opens window anyway without error
# self.webview.preferences().setUserStyleSheetLocation_(self.mycssURL)
self.req = NSURLRequest.requestWithURL_(self.pageurl)
self.webview.mainFrame().loadRequest_(self.req)
self.win.setContentView_(self.webview)
def windowWillClose_(self, aNotification):
#
NSApplication.sharedApplication().terminate_(self)
def applicationShouldTerminateAfterLastWindowClosed_(self, sender):
return True
def applicationShouldTerminate_(self, sender):
return NSTerminateNow
def main():
#
app = NSApplication.sharedApplication()
myDelegate = AppDelegate.alloc().init()
app.effectiveAppearance()
app.setDelegate_(myDelegate)
app.activateIgnoringOtherApps_(True)
app.run()
if __name__ == '__main__':
sys.exit(main())
Use the red traffic light in the title bar to cleanly quit this app.
Launch it from the Terminal as:
./webk2.py