我正在尝试使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:
最佳答案
当我将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/