演示程序

通过在Script Editor中输入以下脚本,来实现点击testButton。
tell application "testApp"
launch
clickTestButton
end tell
一、修改Info.plist
在Info.plist中新增两个字段Scriptable
和Scripting definition file name
。
Scriptable
字段的值设置为YES
。
Scripting definition file name
设置为testApp.sdef
。

二、生成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
网友评论