为什么无法使我的Swift应用程序可编写脚本?

时间:2019-11-21 10:13:37

标签: swift macos scriptable

我正在尝试使Swift应用程序“ Earwig.app”(MacOS)可编写脚本。首先,我无耻地从Making Cocoa Application Scriptable Swift窃取了一个示例,并添加了两个文件,分别是.sdef和.swift,如下所示。我已经在info.plist中设置了脚本和脚本定义文件名

我的AppleScript是:

tell application "EarwigServer" to save in "path/to/file"

运行脚本给我一个错误,“ EarwigServer出现错误:无法继续保存。”

这时我被卡住了。任何建议表示赞赏

sdef文件:

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

<dictionary title="ScriptableSwift Terminology">

    <suite name="ScriptableSwift Scripting Suite" code="SSss" description="Standard suite for application communication.">

        <command name="save" code="SSssSave" description="Save something.">
            <cocoa class="ScriptableSwift.SaveScriptCommand"/>
            <parameter name="in" code="Fpat" type="text" description="The file path in which to save the document.">
                <cocoa key="FilePath"/>
            </parameter>
            <result type="text" description="Echoes back the filepath supplied."/>
        </command>

    </suite>
</dictionary>

快速文件:

import Foundation
import Cocoa

class SaveScriptCommand: NSScriptCommand {
    override func performDefaultImplementation() -> Any? {
        print( "in" )
        let filePath = self.evaluatedArguments!["FilePath"] as! String

        print( "here" )
        return filePath
    }
}

info.plist:

info.plist

1 个答案:

答案 0 :(得分:0)

当我将ScriptableSwift.SaveScriptCommand更改为EarwigServer.SaveScriptCommand时,它开始工作。我知道这似乎很明显,但是我之前没有做过。直到我清理了构建文件夹,更改似乎才生效。

此xml有效,但swift文件保持不变:

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

<dictionary title="EarwigServer Terminology">

    <suite name="EarwigServer Scripting Suite" code="ESss" description="Standard suite for application communication.">

        <command name="run" code="ESssErun" description="Save something.">
            <cocoa class="EarwigServer.SaveScriptCommand"/>

            <parameter name="in" code="Fpat" type="text" description="The file path in which to save the document.">
                <cocoa key="FilePath"/>
            </parameter>
            <result type="text" description="Echoes back the filepath supplied."/>
        </command>

    </suite>
</dictionary>

感谢您的输入