You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为自研应用添加AppleScript支持执行自定义命令时报错-1708的排查

自研应用接入AppleScript支持触发错误-1708排查

问题背景

为自行开发的程序添加AppleScript支持功能,已将实现逻辑裁剪到最基础状态,仍然触发Apple错误码-1708(事件未被应用处理)。初步判断这是开发初期的低级疏漏,但反复排查始终无法定位问题点。

已确认的基础配置

  • 应用已标记为可脚本化,Info.plist中sdef定义文件路径配置正确
  • 将应用拖拽到脚本编辑器中,可正常加载显示应用的所有脚本命令字典
  • 运行测试脚本时,自定义命令的核心实现逻辑完全没有执行——在自定义命令类的核心方法上打的断点从未命中

现有实现代码

sdef脚本字典定义

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="AppleScript Test">
    <suite name="AppleScript Test Suite" code="ASTS" description="AppleScript Test Scripts">
        
        <command name="open" code="aevtodoc" description="Open a document.">
            <direct-parameter description="The file(s) to be opened.">
                <type type="file"/>
                <type type="file" list="yes"/>
            </direct-parameter>
            <result description="The opened document(s).">
                <type type="document"/>
                <type type="document" list="yes"/>
            </result>
        </command>

        <command name="quit" code="aevtquit" description="Quit the application.">
            <cocoa class="NSQuitCommand"/>
            <parameter name="saving" code="savo" type="save options" optional="yes" description="Should changes be saved before quitting?">
                <cocoa key="SaveOptions"/>
            </parameter>
        </command>

        <command name="run test" code="astsrunt" description="Run test code with a script">
            <cocoa class="RunTestCommand"/>
            <result type="text" description="A return string to show that it works okay"/>
        </command>
                
    </suite>
</dictionary>

自定义命令类实现

RunTestCommand.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface RunTestCommand : NSObject

@end

NS_ASSUME_NONNULL_END

RunTestCommand.m

#import "RunTestCommand.h"

@implementation RunTestCommand

-(id)performDefaultImplementation {
    NSString* returnString = @"Hello World"; // 此处设置的断点从未被触发
    return returnString; 
}

@end

触发错误的测试脚本

tell application "AppleScript Test Program"
    activate
    run test
end tell

问题根源与修复方案

触发-1708错误的核心原因是自定义命令类的继承关系不符合框架要求:

  • Cocoa脚本调度框架对sdef中通过<cocoa class>绑定的命令类有强制校验规则:所有合法的命令类必须直接或间接继承NSScriptCommand基类,框架才能完成类校验、Apple事件路由逻辑
  • 当前实现的RunTestCommand直接继承NSObject,不属于框架认可的合法命令类,sdef中声明的类绑定配置会被框架直接忽略
  • 框架找不到可处理run test命令的合法类实例,就会直接抛出错误-1708,自然永远不会进入类内的performDefaultImplementation方法,打断点也不会命中
  • 最小修复方式仅需要修改RunTestCommand的继承关系:将父类从NSObject改为NSScriptCommand,在头文件中导入脚本框架对应头文件即可,无需改动其他配置。

内容的提问来源于stack exchange,提问作者headbanger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.23 12:54:03