2010-03-19 39 views
31

我是新来的可可编程世界,我想添加Applescript支持到我的应用程序。苹果网站上的例子看起来已经过时了。如何将Applescript支持添加到我的Cocoa应用程序中?

如何将Applescript支持添加到我的Cocoa应用程序中?

+0

目前尚不清楚这个问题是有关从一个应用程序使应用程序编写脚本或调用脚本检查错误检查。 – 2016-03-28 01:44:10

回答

4

Cocoa的现代版本可以直接解释脚本定义(.sdef)属性列表,因此您需要为基本的AppleScript支持所做的所有事情是为每个文档创建sdef,并将其添加到“复制束资源”阶段并在您的Info.plist中声明AppleScript支持。要访问除NSApp以外的对象,可以定义对象说明符,以便每个对象知道它在脚本世界层次结构中的位置。这可以让你对对象属性进行kvc操作,并且可以使用对象方法作为简单的脚本命令。

+3

Sdef编辑器可以提供帮助。 http://www.shadowlab.org/Software/sdefeditor.php – 2010-03-19 23:48:03

+0

“要访问除NSApp以外的对象,请定义对象说明符” - 这是在哪里记录的?你能提供一个直接的链接吗? – hendrik 2015-02-13 09:45:56

+0

Sdef编辑器的工作链接:https://www.shadowlab.org/softwares/sdefeditor.php – doekman 2016-05-11 07:34:45

41
  1. 如果你想从你的应用程序发送的AppleScript,需要一个沙盒应用程序,你需要创建一个临时的权利

  2. 您需要在您的info.plist中添加这两个键

    <key>NSAppleScriptEnabled</key> 
    <true/> 
    <key>OSAScriptingDefinition</key> 
    <string>MyAppName.sdef</string> 
    

...当然你要 “MyAppName” 更改为您的应用程序的名称

  • 创建一个.sdef文件并将其添加到您的项目中。 进一步的过程中,现在在很大程度上取决于应用程序的需求,主要有:

    1. 顶级元素(创建的AppleScript对象)
    2. 命令要素(覆盖NSScriptCommand和执行“的动词,如”命令)
    3. 枚举元素
    4. 记录型元件
    5. 值型元件(KVC)
    6. 可可元素

    -

    去这里找到详细的描述,很多细节上的执行:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html

  • 我发现类工作,KVC要素非常复杂,因为我只是想执行一个命令,没有什么花哨。所以为了帮助其他人,下面是一个例子,说明如何用一个参数创建一个新的简单的命令。在这个例子中,它会“查找”一个字符串是这样的:

    tell application "MyAppName" 
        lookup "some string" 
    end tell 
    
  • 此命令的.sdef文件看起来是这样的:

    <?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> 
    
  • 创建NSScriptCommand并将其命名的子类MyLookupCommand

    MyLookupCommand。^ h

    #import <Foundation/Foundation.h> 
    
    @interface MyLookupCommand : NSScriptCommand 
    
    @end 
    

    的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 
    

    这基本上它。对此的秘密是对NSScriptCommand进行分类并覆盖performDefaultImplementation。我希望这可以帮助别人更快地得到它...

  • +2

    感谢您的支持。只需添加。当你第一次设置并在之后进行任何更改时。您将需要重新运行并构建应用程序。不只是建立。同时退出并重新启动Applescript Editor以清除其缓存。我通过反复试验发现了这一点。但后来当我阅读文档(无论如何,它的一部分),它被证实需要完成:-) – markhunte 2013-06-23 15:08:44

    +1

    有史以来最好的脚本答案。谢谢。 – mohacs 2014-03-16 21:50:04

    +0

    直接参数不应该像那样获得。你可以简单地在你的'NSScriptCommand'子类中使用'self.directParameter'。 – 2014-11-13 00:19:43

    1

    一个简单的例子,让你开始,

    地方的脚本(命名对话框)到文件夹,然后你可以从Xcode的

    运行

    保持脚本外部的好处是能够在Xcode之外进行编辑。 我会建议增加如果你没有开始编辑作为AppleScript的可能无法编译

    可能与

    if(scriptObject.isCompiled){ 
    
    相关问题