Call Objective-C Foundation/AppKit APIs directly from a JavaScript for Automation (JXA) script via osascript's built-in ObjC bridge

domain: developer.apple.com · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Import the framework you need with ObjC.import('Foundation') (or another framework name) at the top of the JXA script; Foundation symbols are available by default but other frameworks must be explicitly imported.
  2. Access Objective-C classes and call their methods through the global ObjC object, which exposes classes/methods/properties from every imported framework to JavaScript.
  3. Convert between JS and Objective-C string/object types using the $ bridge helper where a native Cocoa object (e.g. an NSString) needs to be produced or read back as a JS value.
  4. Run the script with `osascript -l JavaScript <file.js>` (or as a saved JXA script), since the ObjC bridge is available only in the JavaScript (JXA) language context, not classic AppleScript.
  5. Wrap ObjC bridge calls that can raise Cocoa exceptions in try/catch, since a failing Objective-C call surfaces as a thrown JS exception rather than a typical AppleScript-style error.

Known gotchas

Related routes

Script a native macOS app with JavaScript for Automation (JXA) instead of AppleScript
developer.apple.com · 5 steps · unrated
Control macOS GUI applications (Finder, Mail, Safari) via JavaScript for Automation (JXA) with osascript
developer.apple.com · 5 steps · unrated
Automate macOS applications using AppleScript via osascript from the command line or scripts
developer.apple.com · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans