user25093
user25093

Reputation: 287

Can I use apple script commands in a MacOSX application?

A certain API I want to use only works with AppleScript to request information from an App. I want to build a MacOSX application, can I use apple script in this application? Are there work arounds to implement apple script?

Thanks!

Upvotes: 0

Views: 70

Answers (2)

foo
foo

Reputation: 249

Don't bother with NSAppleScript: it's a right PITA for passing data in and out as you have to pack and unpack every NSAppleEventDescriptor yourself. The simplest way to call AS inside a Cocoa app is via the AppleScript-ObjC bridge - that lets your ObjC code call AS handlers like native methods (and vice-versa), and performs the AS-ObjC value conversions for you.

Other options for sending Apple events directly from ObjC/Swift:

  • macOS's ScriptingBridge framework, which is crippled, crappy and horribly obfuscated, but may suffice trivial tasks (AS via ASOC is still the safest and most robust choice though)

  • the SwiftAutomation framework that I'm developing with the goal of pitching it to Apple in the new year. It's a new codebase and I'm still knocking out bugs, but the design descends from appscript which has proven itself a true alternative to AppleScript over a decade of real-world use.

BTW, if SwiftAutomation is of interest to you, please a file feature request on bugreport.apple.com asking Apple to adopt it and include in 10.13. The more duplicate tickets they get, the more likely they are to pay attention.

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89559

If you want to talk to another app only via AppleScript, this might be as easy as using a couple API's in the NSAppleScript class.

E.G.

initWithSource: (I'm assuming Objective-C but of course there are Swift equivalents) and executeAndReturnError:

Upvotes: 2

Related Questions