从 Cocoa 应用程序运行 Applescript
我想在可可应用程序中运行一个简单的苹果脚本。我阅读了有关此事的苹果文档,但这对我(初学者)来说太混乱了,无法理解。
tell application "iTunes" to play
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 文档,您使用 NSAppleScript 类。
非常短的 API 参考中有一个名为“初始化脚本”的部分,其中一个方法是
-initWithSource:
,它采用 NSString。您将通过这种方式创建您的对象。获得脚本对象后,您可以将
-compileAndReturnError:
然后-executeAndReturnError:
作为单独的步骤,或者只是-executeAndReturnError:
,这- 根据该方法的文档 - 如果尚未编译源代码,则尝试首先编译源代码,然后执行。因此,从理论上讲,您可以在一行中完成所有这些操作。 (alloc、init...、autorelease、executeAndReturnError:) 如果你像顽皮的开发人员一样忽略错误。
请注意警告,NSAppleScript 只能从主线程执行(即不能从 NSOperation/Queue 或其他线程执行)。
Per the documentation, you use the NSAppleScript class.
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.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.So, in theory, you could probably do all this in one line. (alloc, init..., autorelease, executeAndReturnError:) if you ignore errors like a naughty developer.
Note the warning that NSAppleScript can only be executed from the main thread (ie, not from an NSOperation/Queue or other threads).