从 Cocoa 应用程序运行 AppleScript [英] Run AppleScript from Cocoa Application

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

问题描述

是否可以在 Cocoa 应用程序中运行 AppleScript 代码?

Is it possible to run an AppleScript code inside an Cocoa Application?

我尝试过 NSAppleScript 类,但没有成功.

I've tried NSAppleScript class, but no success.

另外,Apple 允许这样做吗?

Also, does Apple allow this?

推荐答案

您提到 xcode 没有将脚本保存到您的应用程序的资源路径.那是正确的.你必须告诉 xcode 这样做.首先将编译好的脚本添加到您的项目中.然后打开您的目标并找到复制捆绑资源"操作.将您的脚本从文件列表拖到该操作中.通过这种方式,您的脚本会自动复制到您应用的资源中,因此您无需手动完成.

You mentioned xcode wasn't saving the script to your app's resources path. That is correct. You have to tell xcode to do this. First add the compiled script to your project. Then open your target and find the "Copy Bundle Resources" action. Drag your script from the files list into that action. This way your script is copied to your app's resources automatically so you don't have to do it by hand.

每当我在可可应用程序中使用编译的 AppleScript 时,我,1) 将脚本添加到项目中,2) 创建一个新类来控制 AppleScript,3) 使用下面的类的 init 方法,以及 4) 拖动脚本到目标的复制捆绑资源"动作.

Whenever I use a compiled AppleScript in a cocoa application I, 1) add the script to the project, 2) create a new class to control the AppleScript, 3) use the below init method for the class, and 4) drag the script to the "Copy Bundle Resources" action of the target.

- (id)init {
    NSURL *scriptURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"applescripts" ofType:@"scpt"]];
    if ([self initWithURLToCompiledScript:scriptURL] != nil) { //attempt to load the script file
    }

    return self;
}

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

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