本文介绍了如何向我的 Cocoa 应用程序添加 Applescript 支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Cocoa 编程领域的新手,我想为我的应用程序添加 Applescript 支持.Apple 网站上的示例似乎已经过时.

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.

如何向我的 Cocoa 应用程序添加 Applescript 支持?

How do I add Applescript support to my Cocoa application?

推荐答案

  1. 如果您想从您的应用程序发送 AppleScript 并且需要一个沙盒应用程序,您需要创建一个临时授权

  1. If you want to send AppleScript from your application and need a sandboxed app, you need to create a temporary entitlement

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

You need to add those two keys in your info.plist

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

...当然您必须将MyAppName"更改为您的应用程序名称

...of course you have to change "MyAppName" to your app's name

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

  1. Create a .sdef file and add it to your project.The further course now greatly depends on the needs of your application, there are:

  1. 类元素(从 AppleScript 创建一个对象)
  2. 命令元素(覆盖 NSScriptCommand 并执行类似动词的命令)
  3. 枚举元素
  4. 记录类型元素
  5. 值类型元素 (KVC)
  6. 可可元素

-

去这里找到一个详细的描述和很多关于他们实现的细节:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html

Go here to find a detailed description and many details on their implementation: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html

我发现使用 Class 和 KVC Elements 非常复杂,因为我只想执行一个命令,没什么特别的.因此,为了帮助他人,这里有一个示例,说明如何使用一个参数创建一个新的简单命令.在这个例子中,它将查找"一个这样的字符串:

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

  • 该命令的 .sdef 文件如下所示:

  • 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>
    

  • 创建一个 NSScriptCommand 的子类并将其命名为 MyLookupCommand

  • Create a subclass of NSScriptCommand and name it MyLookupCommand

    MyLookupCommand.h

    The MyLookupCommand.h

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

    MyLookupCommand.m

    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
    

    基本上就是这样.其秘诀是将 NSScriptCommand 子类化并覆盖 performDefaultImplementation.我希望这可以帮助某人更快地获得它...

    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 支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-20 12:12
    查看更多