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

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

问题描述

我希望能够运行在一个Cocoa应用程序的简单的AppleScript。我读了关于这个问题的苹果文档,但它太混乱,我(初学者)了解。

 告诉应用程序iTunes的玩


解决方案

按照<一个href=\"http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_classes/SAppsScriptClasses.html#//apple_ref/doc/uid/20000034-BBCBGCEI\"相对=nofollow>文档,您使用 NSAppleScript 类。

在很短的API参考都有一段名为初始化脚本,一个方法,就是 -initWithSource:,这需要一个NSString。您将创建您的对象这样的。

一旦你有你的脚本对象,然后,您可以 -compileAndReturnError:然后 -executeAndReturnError:作为单独的步骤,或只是 -executeAndReturnError:,其中 - 按照该方法的文档 - 首先尝试编译源,如果不是被它已经,然后执行

所以,从理论上讲,你也许可以做到这一切在一行中。 (ALLOC时,init ...,自动释放,executeAndReturnError :)如果你忽略像一个顽皮的开发人员的错误。

注意NSAppleScript只能从主线程(即不从的NSOperation /队列或其他线程)。

执行警告

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

解决方案

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).


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

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