Python bindings for Apple's WebKit and JavaScriptCore frameworks on macOS, enabling web browser functionality and JavaScript execution in Python applications
npx @tessl/cli install tessl/pypi-pyobjc-framework-webkit@10.3.0Python bindings for Apple's WebKit and JavaScriptCore frameworks on macOS, enabling developers to integrate web browser functionality and JavaScript execution capabilities into their Python applications. This framework provides comprehensive wrappers for WebKit's web view components, navigation controls, and browser engine features, along with JavaScriptCore's JavaScript interpreter and execution environment.
pip install pyobjc-framework-WebKitpyobjc-core, pyobjc-framework-Cocoaimport WebKit
import JavaScriptCoreFor accessing specific WebKit classes:
from WebKit import WKWebView, WKWebViewConfiguration, WebView
from JavaScriptCore import JSContext, JSValueimport WebKit
from Foundation import NSURL
from AppKit import NSApplication, NSWindow, NSWindowStyleMask
# Create a modern web view
config = WebKit.WKWebViewConfiguration.alloc().init()
web_view = WebKit.WKWebView.alloc().initWithFrame_configuration_(
((0, 0), (800, 600)), config
)
# Load a web page
url = NSURL.URLWithString_("https://www.example.com")
request = WebKit.WKWebViewNavigationAction.alloc().init()
web_view.loadRequest_(request)
# Handle navigation
class NavigationDelegate:
def webView_didFinishNavigation_(self, web_view, navigation):
print("Page loaded successfully")
delegate = NavigationDelegate()
web_view.setNavigationDelegate_(delegate)import JavaScriptCore
# Create JavaScript context
context = JavaScriptCore.JSContext.alloc().init()
# Execute JavaScript code
result = context.evaluateScript_("1 + 2 * 3")
print(result.toNumber()) # Output: 7
# Handle exceptions
def exception_handler(context, exception):
print(f"JavaScript error: {exception}")
context.setExceptionHandler_(exception_handler)
# Create JavaScript values
number_value = JavaScriptCore.JSValue.valueWithDouble_inContext_(42.5, context)
string_value = JavaScriptCore.JSValue.valueWithObject_inContext_("Hello World", context)
bool_value = JavaScriptCore.JSValue.valueWithBool_inContext_(True, context)The pyobjc-framework-WebKit package provides two distinct but complementary frameworks:
This dual-framework approach enables creating everything from simple web content display to sophisticated browser applications with custom JavaScript integration, making it ideal for macOS developers building applications that need to display web content, execute JavaScript code, or create browser-like interfaces.
Modern WKWebView-based web browsing with security, performance, and feature improvements over legacy WebView. Includes configuration, navigation, user content management, and delegate-based event handling.
class WKWebView:
def initWithFrame_configuration_(self, frame, configuration): ...
def loadRequest_(self, request): ...
def loadHTMLString_baseURL_(self, string, baseURL): ...
def goBack(self): ...
def goForward(self): ...
def reload(self): ...
def evaluateJavaScript_completionHandler_(self, javaScriptString, completionHandler): ...
class WKWebViewConfiguration:
def init(self): ...
@property
def userContentController(self): ...
@property
def preferences(self): ...
@property
def websiteDataStore(self): ...Original WebView component and related classes for backward compatibility. While superseded by WKWebView, still used in some applications and provides additional DOM manipulation capabilities.
class WebView:
def initWithFrame_frameName_groupName_(self, frame, frameName, groupName): ...
def canShowMIMEType_(self, MIMEType): ...
def goBack(self): ...
def goForward(self): ...
def searchFor_direction_caseSensitive_wrap_(self, string, forward, caseFlag, wrapFlag): ...
def isLoading(self): ...
def setDrawsBackground_(self, flag): ...
class WebPreferences:
def init(self): ...
def setJavaEnabled_(self, flag): ...
def setJavaScriptEnabled_(self, flag): ...Complete Document Object Model classes for programmatic web content manipulation. Provides access to HTML elements, events, CSS styles, and document structure.
class DOMDocument:
def getElementById_(self, elementId): ...
def createElement_(self, tagName): ...
def execCommand_(self, command): ...
def hasFocus(self): ...
class DOMElement:
def getAttribute_(self, name): ...
def setAttribute_value_(self, name, value): ...
def hasAttribute_(self, name): ...
def scrollIntoView_(self, alignToTop): ...
class DOMHTMLInputElement(DOMElement):
@property
def value(self): ...
@property
def checked(self): ...
@property
def disabled(self): ...JavaScript execution environment and value management through JavaScriptCore. Enables running JavaScript code, bridging between Python and JavaScript objects, and handling JavaScript exceptions.
class JSContext:
def init(self): ...
def evaluateScript_(self, script): ...
def setExceptionHandler_(self, handler): ...
def isInspectable(self): ...
def setInspectable_(self, inspectable): ...
class JSValue:
def isArray(self): ...
def isBoolean(self): ...
def isNumber(self): ...
def isString(self): ...
def isObject(self): ...
def isNull(self): ...
def isUndefined(self): ...
def toBool(self): ...
def toNumber(self): ...
def toString(self): ...
def hasProperty_(self, property): ...
def deleteProperty_(self, property): ...
def JSExportAs(PropertyName, Selector): ...URL loading, navigation management, browser history, and network request handling. Includes both modern WKNavigation system and legacy navigation approaches.
class WKNavigation:
pass # Opaque navigation identifier
class WKNavigationAction:
@property
def request(self): ...
@property
def navigationType(self): ...
@property
def sourceFrame(self): ...
@property
def targetFrame(self): ...
class WKNavigationResponse:
@property
def response(self): ...
@property
def isForMainFrame(self): ...
@property
def canShowMIMEType(self): ...
class WKBackForwardList:
@property
def currentItem(self): ...
@property
def backItem(self): ...
@property
def forwardItem(self): ...
def itemAtIndex_(self, index): ...User scripts, content rules, cookie management, and website data handling. Provides control over web content behavior, security policies, and data storage.
class WKUserContentController:
def init(self): ...
def addUserScript_(self, userScript): ...
def removeAllUserScripts(self): ...
def addScriptMessageHandler_name_(self, scriptMessageHandler, name): ...
def addContentRuleList_(self, contentRuleList): ...
class WKUserScript:
def initWithSource_injectionTime_forMainFrameOnly_(self, source, injectionTime, forMainFrameOnly): ...
class WKScriptMessage:
@property
def body(self): ...
@property
def webView(self): ...
@property
def frameInfo(self): ...
@property
def name(self): ...
class WKHTTPCookieStore:
def getAllCookies_(self, completionHandler): ...
def setCookie_completionHandler_(self, cookie, completionHandler): ...
def deleteCookie_completionHandler_(self, cookie, completionHandler): ...Navigation types, error codes, content policies, and other enumerated values used throughout the WebKit and JavaScriptCore frameworks.
# WK Navigation Types
WKNavigationTypeLinkActivated = 0
WKNavigationTypeFormSubmitted = 1
WKNavigationTypeBackForward = 2
WKNavigationTypeReload = 3
WKNavigationTypeFormResubmitted = 4
WKNavigationTypeOther = -1
# WK Navigation Policies
WKNavigationActionPolicyCancel = 0
WKNavigationActionPolicyAllow = 1
WKNavigationActionPolicyDownload = 2
# JavaScript Types
kJSTypeUndefined = 0
kJSTypeNull = 1
kJSTypeBoolean = 2
kJSTypeNumber = 3
kJSTypeString = 4
kJSTypeObject = 5
kJSTypeSymbol = 6
# Error Codes
WKErrorUnknown = 1
WKErrorWebContentProcessTerminated = 2
WKErrorWebViewInvalidated = 3
WKErrorJavaScriptExceptionOccurred = 4