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

标签 swift macos scriptable

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

我的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

最佳答案

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

此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>

感谢您的输入

关于swift - 为什么无法使我的Swift应用程序可编写脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58972385/

相关文章:

ios - 如何在表格 View 上设置拖动 handle 的自定义图像和位置

json - Firebase 如何在 Swift 中进行此类查询

swift - 在 Xcode11 beta 4 中工作但在 beta 5 中停止工作的代码有问题

c++ - 在 C++ 项目中使用 (OSX) Core MIDI

swift - 写字符串给我错误,写图像不

c - GDB 不会在没有调试信息的情况下跳过函数

cocoa - OSX(Leopard 和 Snow Leopard)的上下文菜单项开发

java - Rhino:如何从 ScriptableObject 获取所有属性?

unity3d - 为什么从assetBundle添加MeshCollider时mesh已经被标记为不可访问,如何解决?