从 Cocoa 应用程序运行 Applescript [英] Running Applescript from a Cocoa application
问题描述
我想在可可应用程序中运行一个简单的 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屋!