Applescript Inside of a Cocoa Application

心已入冬 提交于 2019-12-01 00:51:46

It seems like SBApplication should work, but I haven't used it before.

According to @cocoadevcentral:

SBApplication: use to make cross-application scripting calls with Objective-C instead of AppleScript. Ex: get current iTunes track.

Here is is the excerpt from the documentation:

The SBApplication class provides a mechanism enabling an Objective-C program to send Apple events to a scriptable application and receive Apple events in response. It thereby makes it possible for that program to control the application and exchange data with it. Scripting Bridge works by bridging data types between Apple event descriptors and Cocoa objects.

Although SBApplication includes methods that manually send and process Apple events, you should never have to call these methods directly. Instead, subclasses of SBApplication implement application-specific methods that handle the sending of Apple events automatically.

For example, if you wanted to get the current iTunes track, you can simply use the currentTrack method of the dynamically defined subclass for the iTunes application—which handles the details of sending the Apple event for you—rather than figuring out the more complicated, low-level alternative:

[iTunes propertyWithCode:'pTrk'];

If you do need to send Apple events manually, consider using the NSAppleEventDescriptor class.

Hope that helps!

As Blaenk mentioned Scripting Bridge may well be the way to go, although it can prove somewhat inefficient if you have to iterating through large arrays etc.

The simplest way to run an Applescript in Cocoa is using NSAppleScript.

Apple has some pretty good examples, which I found useful when I needed to do something similar. There are three articles you might want to take a look at. They all contain some sample code, which I always find very useful.

  • A Few Examples of using Scripting Bridge
  • Performance & Optimisation with Scripting Bridge
  • NSAppleScript Technote/Example

I created a gist with the full URLs as I can't post more than one link, what with being a newbie and all.

http://gist.github.com/130146

it seems like the Appscript framework does not support 64 bit.

Should work. Make sure you set the correct architectures and SDK (64-bit requires 10.5) in the Xcode project. File a bug report if you have a specific problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!