从 Cocoa 应用程序运行 Applescript [英] Running Applescript from a Cocoa application

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

问题描述

我想在可可应用程序中运行一个简单的 applescript.我阅读了关于此事的苹果文档,但对我(初学者)来说太混乱了,无法理解.

I want to run a simple applescript in a cocoa application. I read the apple documentation on the matter but it was too confusing for me (a beginner) to understand.

tell application "iTunes" to play

推荐答案

根据 文档,您使用NSAppleScript 类.

这个非常简短的 API 参考有一个名为初始化脚本"的部分,其中一种方法是 -initWithSource:,它接受一个 NSString.您将通过这种方式创建对象.

The very short API reference has a section called "Initializing a Script," one method of which is -initWithSource:, which takes an NSString. You'll create your object this way.

一旦你有了你的脚本对象,你就可以将 -compileAndReturnError: 然后 -executeAndReturnError: 作为单独的步骤,或者只是 -executeAndReturnError:>,根据该方法的文档,它首先尝试编译源代码(如果尚未编译),然后执行.

Once you have your script object, you can then either -compileAndReturnError: then -executeAndReturnError: as separate steps, or just -executeAndReturnError:, which - according to the documentation for that method - tries to compile the source first if it's not been already, then executes.

因此,理论上,您可能可以在一行中完成所有这些.(alloc, init..., autorelease, executeAndReturnError:) 如果你像一个顽皮的开发者一样忽略错误.

So, in theory, you could probably do all this in one line. (alloc, init..., autorelease, executeAndReturnError:) if you ignore errors like a naughty developer.

注意 NSAppleScript 只能从主线程执行的警告(即不能从 NSOperation/Queue 或其他线程执行).

Note the warning that NSAppleScript can only be executed from the main thread (ie, not from an NSOperation/Queue or other threads).

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

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