在 Mac 应用程序中执行 AppleScript 文件? [英] Execute AppleScript file in Mac app?

查看:25
本文介绍了在 Mac 应用程序中执行 AppleScript 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种从 Mac 应用程序中执行 Applescript 文件的方法.我已经制作了使用 NSAppleScript *script = [[NSAppleScript alloc] initWithSource: @"MY_CODE"]]; 的以前的 mac 应用程序,但是我的 applescript 很长,我希望能够将该文件作为我项目的一部分引用,并通过管道输入其中的内容(或直接从我的应用程序中执行该文件).

I'm looking for a way to execute an Applescript file from within a Mac App. I've made previous mac applications that use NSAppleScript *script = [[NSAppleScript alloc] initWithSource: @"MY_CODE"]];, but my applescript is quite lengthy and I'd like to just be able to reference that file as part of my project, and pipe in the contents of it (or just directly execute the file from within my app).

谢谢!

推荐答案

要在沙箱 (10.8+) 之外运行用户提供的 AppleScript,请使用 NSUserAppleScriptTask

To run user-supplied AppleScripts outside the sandbox (10.8+), use NSUserAppleScriptTask

要运行嵌入在应用程序包中的 .scpt 文件或 10.7 或更早版本中的用户提供的脚本,请使用 NSAppleScript.

To run .scpt files embedded in your application bundle, or user-supplied scripts in 10.7 or earlier, use NSAppleScript.

NSUserAppleScriptTask 和 NSAppleScript 都有些蹩脚,请注意.两者都没有提供 Cocoa 和 AS 类型之间的自动数据转换,这意味着需要大量手动使用 NSAppleEventDescriptors 来传递参数和结果.NSAppleScript 实例提供部分持久性支持:脚本的属性在多个处理程序调用之间保留状态,但无法将更改的脚本保存回磁盘,因此一旦解除分配,任何更改都会丢失.而且 NSUSerAppleScriptTask 是一个严格的一次性事务,因此脚本状态在调用之间根本不会持续.

NSUserAppleScriptTask and NSAppleScript are both a bit lame, mind you. Neither provides automatic data conversion between Cocoa and AS types, which means a lot of manual mucking with NSAppleEventDescriptors to pass parameters and results. NSAppleScript instances provide partial persistency support: the script's properties retain state across multiple handlers calls, but there's no way to save the altered script back to disk so any changes are lost once it's dealloced. And NSUSerAppleScriptTask is a strictly one-shot affair, so script state doesn't persist at all between calls.

要在应用程序包中更深入地集成嵌入式 AppleScript 和 ObjC 代码,请使用 AppleScriptObjC (10.6+):

For deeper integration between embedded AppleScript and ObjC code within your app bundle, use AppleScriptObjC (10.6+):

ASOC 是类似于 PyObjC 或 RubyCocoa 的双向桥接器,允许您在脚本对象中编写 AppleScript 代码,然后在您的 ObjC 代码中显示为常规 Cocoa 类,允许 ObjC 代码调用 AS 处理程序和 AS 代码调用 ObjC方法.常见的 ObjC 和 AS 类型(整数、双精度数、NSStrings、NSArrays 等)在过桥时会自动包装/转换,因此除了偶尔在 AS 端进行强制转换之外,不需要额外的工作.

ASOC is a two-way bridge similar to PyObjC or RubyCocoa, allowing you write your AppleScript code within script objects that then appear to your ObjC code as regular Cocoa classes, allowing ObjC code to call AS handlers and AS code to call ObjC methods. Common ObjC and AS types (ints, doubles, NSStrings, NSArrays, etc) are automatically wrapped/converted when crossing the bridge, so requires no additional work other an occasional cast on the AS side.

ASOC 并不完美(Apple 自己的文档很烂,桥接不是免费的,故障不是未知的),但是对于与 ObjC 的可编写脚本的应用程序进行交互,它是最好的选择,否则就相当垃圾了.

ASOC is not perfect (Apple's own docs suck, bridging isn't toll-free, and glitches aren't unknown), but for interacting with scriptable apps from ObjC it is the best of an otherwise rather rubbish set of choices.

这篇关于在 Mac 应用程序中执行 AppleScript 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆