美文网首页
如何让你的Cocoa程序支持AppleScript

如何让你的Cocoa程序支持AppleScript

作者: 拳战攻城师 | 来源:发表于2020-05-09 10:27 被阅读0次

演示程序

testApp

通过在Script Editor中输入以下脚本,来实现点击testButton。

tell application "testApp"
    launch
    clickTestButton
end tell

一、修改Info.plist

在Info.plist中新增两个字段ScriptableScripting definition file name
Scriptable字段的值设置为YES
Scripting definition file name设置为testApp.sdef

image.png

二、生成testApp.sdef

创建一个.sdef的文件,头两行输入以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">

然后将核心内容,放在后面:

<dictionary title="Standard Terminology">
    <suite name="testApp Suite" code="TAAS" description="testApp Application Suite">
        <cocoa name="testApp"/>
        <command name="clickTestButton" code="clickbtn" description="click the testButton">
        <cocoa class="ClickTestButton"/>
        </command>
    </suite>
</dictionary>

其中,command name设置为clickTestButton,这个对应到AppleScript中的命令。
cocoa class设置为ClickTestButton,这个对应到clickTestButton命令所执行的代码。

三、实现ClickTestButton类

首先新建ClickTestButton类继承于NSScriptCommand

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface ClickTestButton : NSScriptCommand

@end

NS_ASSUME_NONNULL_END
#import "ClickTestButton.h"
#import "AppDelegate.h"

@implementation ClickTestButton

- (id)performDefaultImplementation{
    [super performDefaultImplementation];
    AppDelegate *appDelegate = (AppDelegate*)NSApp.delegate;
    [appDelegate testButton1:nil];
    return nil;
}

@end

完成!

参考文献
https://justinyan.me/post/3075
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_intro/SAppsIntro.html

相关文章

网友评论

      本文标题:如何让你的Cocoa程序支持AppleScript

      本文链接:https://www.haomeiwen.com/subject/viuhnhtx.html