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

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

问题描述

我正在寻找一种方式来从Mac应用程序内执行一个AppleScript文件。我做了使用previous Mac应用程序 NSAppleScript *脚本= [[NSAppleScript页​​头] initWithSource:@MY_ code]]; ,但我的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).

感谢您!

推荐答案

要运行沙箱之外的用户提供的AppleScript(10.8+),使用<一个href=\"http://developer.apple.com/library/mac/#documentation/Foundation/Reference/NSUserAppleScriptTask_Class/Reference/Reference.html#//apple_ref/occ/cl/NSUserAppleScriptTask\">NSUserAppleScriptTask

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

要运行.scpt嵌入到应用程序包文件,或用户提供的脚本10.7或更早的版本,使用<一个href=\"https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAppleScript_Class/Reference/Reference.html\">NSAppleScript.

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

NSUserAppleScriptTask和NSAppleScript都有点跛,你介意。无论是提供可可和AS之间的类型自动数据转换,这意味着大量的具有手动NSAppleEventDescriptors摆弄来传递参数和结果。 NSAppleScript实例提供持续性的部分支持:脚本的属性在多个处理程序调用保留状态,但是没有办法保存改变脚本回磁盘所以一旦它dealloced的任何更改将丢失。和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 code之间更深层次的集成,使用AppleScriptObjC(10.6 +):

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

  • 10.6 Release Notes
  • AppleScriptObjC Explored
  • MacScripter forum
  • applescript-objc

ASOC是一个双向的桥梁类似PyObjC或RubyCocoa,让你写脚本中您的AppleScript code对象出现在屏幕上你的ObjC code作为常规Cocoa类,允许ObjC code到调用作为处理程序和AS code调用ObjC方法。常见ObjC和AS类型(整型,双打,NSS​​tring的,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是不完美的(苹果自己的文档吸,桥是不是免费电话,和毛刺都没有不明),但对于从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天全站免登陆