问题描述
我是新来的Cocoa编程的世界,我想AppleScript的支持添加到我的应用程序。在苹果公司的网站上的例子似乎过时了。
我如何加入的AppleScript支持我的Cocoa应用程序?
-
如果您想从您的应用程序发送的AppleScript,需要一个沙盒应用程序,你需要创建一个临时的权利
-
您需要在您的info.plist添加这两个键
<串GT;的NSApplication< /串>
<密钥GT; NSAppleScriptEnabled< /键>
<真/>
<密钥GT; OSAScriptingDefinition< /键>
<串GT; MyAppName.sdef< /串>...当然你要MyAppName更改为您的应用程序的名称
-
创建.sdef文件并将其添加到您的项目。
进一步当然,现在在很大程度上取决于应用程序的需求,主要有:- 顶级元素(创建的AppleScript的对象)
- 命令元素(覆盖NSScriptCommand和执行动词,如命令)
- 枚举元素
- 记录类型元素
- 数值类型元素(KVC)
- 可可元素
-
去这里找到一个详细的说明,并就其实施的许多细节:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html
-
我发现类和KVC要素非常复杂的工作,因为我只是想执行一个命令,没有什么花哨。因此,为了帮助别人,这里是如何创建一个新的简单的例子命令有一个参数。在这个例子中,它会查找一根弦是这样的:
告诉应用程序MyAppName
查找一些字符串
告诉结束 -
此命令的.sdef文件看起来是这样的:
<?XML版本=1.0编码=UTF-8&GT?;
!< DOCTYPE词典系统文件://localhost/System/Library/DTDs/sdef.dtd><字典标题=MyAppName>
<套件名称=MyAppName套房code =MApN描述=MyAppName脚本>
<命令名称=查找code =lkpstrng描述=查找的字符串,条目的搜索>
<可可类=MyLookupCommand/>
<直接参数描述=字符串查找>
<类型type =TEXT/>
< /直接参数>
< /指挥GT;
< /套房>
< /字典> -
创建NSScriptCommand的子类,并将其命名为MyLookupCommand
该MyLookupCommand.h
#进口<基金会/ Foundation.h>@interface MyLookupCommand:NSScriptCommand@结束
该MyLookupCommand.m
#进口MyLookupCommand.h@implementation MyLookupCommand - (ID)performDefaultImplementation { //获取参数
*的NSDictionary ARGS = [个体经营evaluatedArguments]
* NSString的stringToSearch = @;
如果(args.count){
stringToSearch = [参数valueForKey:@]; //获取直接的说法
}其他{
//引发错误
[个体经营setScriptErrorNumber:-50]。
[个体经营setScriptErrorString:@参数错误:参数是预期的动词'查找'(你必须指定_what_要查找!)。];
}
//实现你code逻辑(在这个例子中,我只是发布内部通知)
[NSNotificationCenter defaultCenter] postNotificationName:@AppShouldLookupStringNotification对象:stringToSearch];
回零;
}@结束这基本上它。秘密这是子类的 NSScriptCommand 的并重写的 performDefaultImplementation 的。我希望这可以帮助别人更快地得到它...
I am new to the world of Cocoa programming, and I want to add Applescript support to my application. The examples at Apple's website seem out of date.
How do I add Applescript support to my Cocoa application?
If you want to send AppleScript from your application and need a sandboxed app, you need to create a temporary entitlement
You need to add those two keys in your info.plist
<string>NSApplication</string> <key>NSAppleScriptEnabled</key> <true/> <key>OSAScriptingDefinition</key> <string>MyAppName.sdef</string>
...of course you have to change "MyAppName" to your app's name
Create a .sdef file and add it to your project.The further course now greatly depends on the needs of your application, there are:
- Class Elements (create an object from AppleScript)
- Command Elements (override NSScriptCommand and execute "verb-like" commands)
- Enumeration Elements
- Record-Type Elements
- Value-Type Elements (KVC)
- Cocoa Elements
-
Go here to find a detailed description and many details on their implementation: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html
I found working with Class and KVC Elements very complicated, as I just wanted to execute a single command, nothing fancy. So in order to help others, here's an example of how to create a new simple command with one argument. In this example it'll "lookup" one string like this:
tell application "MyAppName" lookup "some string" end tell
The .sdef file for this command looks like this:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd"> <dictionary title="MyAppName"> <suite name="MyAppName Suite" code="MApN" description="MyAppName Scripts"> <command name="lookup" code="lkpstrng" description="Look up a string, searches for an entry"> <cocoa class="MyLookupCommand"/> <direct-parameter description="The string to lookup"> <type type="text"/> </direct-parameter> </command> </suite> </dictionary>
Create a subclass of NSScriptCommand and name it MyLookupCommand
The MyLookupCommand.h
#import <Foundation/Foundation.h> @interface MyLookupCommand : NSScriptCommand @end
The MyLookupCommand.m
#import "MyLookupCommand.h" @implementation MyLookupCommand -(id)performDefaultImplementation { // get the arguments NSDictionary *args = [self evaluatedArguments]; NSString *stringToSearch = @""; if(args.count) { stringToSearch = [args valueForKey:@""]; // get the direct argument } else { // raise error [self setScriptErrorNumber:-50]; [self setScriptErrorString:@"Parameter Error: A Parameter is expected for the verb 'lookup' (You have to specify _what_ you want to lookup!)."]; } // Implement your code logic (in this example, I'm just posting an internal notification) [[NSNotificationCenter defaultCenter] postNotificationName:@"AppShouldLookupStringNotification" object:stringToSearch]; return nil; } @end
That's basically it. The secret to this is to subclass NSScriptCommand and override performDefaultImplementation. I hope this helps someone to get it faster...
这篇关于我怎么向我的Cocoa应用程序添加AppleScript的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!