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

标签 objective-c cocoa applescript

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

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

最佳答案

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

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

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

...当然,您必须将“MyAppName”更改为您应用的名称

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

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

    -

    到这里可以找到详细的描述和许多关于它们实现的细节:https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_script_cmds/SAppsScriptCmds.html

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

    tell application "MyAppName"
        lookup "some string"
    end tell
    
  3. 此命令的 .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>
    
  4. 创建 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。我希望这可以帮助某人更快地获得它...

关于objective-c - 如何向我的 Cocoa 应用程序添加 Applescript 支持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2479585/

相关文章:

macos - applescript:清空垃圾箱而不发出警告

objective-c - 更改核心图中图例的大小?

objective-c - 发出一个 block 并等待它完成

ios - UICollectionView 动态调整部分

macos - 使用 Core Data 和 iCloud 添加持久存储时涉及 PFUbiquityPeer 异常

苹果脚本 "mixed credentials"错误

ios - 在 ARC 中覆盖 setter 时如何管理对象?

macos - swift : "fatal error: use of unimplemented initializer ' 初始化()' for class..."

swift - NSUserDefaults 未保存 - 逻辑错误?

applescript - Mac applescript - 要求管理员权限提示