我正在尝试使用Applescript编写的Swift脚本编写的Cocoa应用程序.
我做了什么
我创建了一个SDEF文件,配置了我的info.plist并创建了一个我认为合适的类.
definition.sdef
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd"> <dictionary title="SamX"> <!-- specific suite(s) for the application follow... --> <suite name="SamX Scripting Suite" code="Samx" description="Suite for communication with the application"> <command name="savedoc" code="corecnte" description="description"> <cocoa class="ProjectName.ScriptingSaveNotification" id="BLah"/> <parameter name="with dname" code="WTdc" type="text" optional="no" description="description"> <cocoa key="DocumentName"/> </parameter> <result type="boolean" description="The result of the invocation. True if it succeeds, False if it does not"/> </command> </suite> </dictionary>
info.plist中
ScriptingSaveNotification.swift
import Foundation import Cocoa class ScriptingSaveNotification: NSScriptCommand, NSUserNotificationCenterDelegate { override func performDefaultImplementation() -> AnyObject? { let parms = self.evaluatedArguments var name = "" if let args = parms { if let DocumentName = args["DocumentName"] as? String { name = DocumentName } } debugPrint("We were prompted to save"); return "hello world" } func userNotificationCenter(center: NSUserNotificationCenter, shouldPresentNotification notification: NSUserNotification) -> Bool { debugPrint("We were prompted to save"); return true } }
我在哪里
我有一个启动的应用程序.应用程序的SDEF文件似乎在Applescript Editor中反映出来. Applescript编辑器还返回字典定义.但是当我运行命令时,我总是得到5(int)的输出,并且我的调试行似乎都没有在Xcode中输出.
在我看来,也许我在SDEF中不恰当地引用了我的课程.但我不是百分百肯定.我已经尝试过多次重命名了.任何帮助将不胜感激.
Applescript字典
测试脚本
tell application "MyApplication" set testString to "Hello" set returnValue to savedoc testString display alert returnValue end tell编辑:
主要问题是您实际上并未在脚本中使用with dname参数.它应该是:
set returnValue to savedoc with dname testString
也就是说,下面的信息对于创建正确的sdef仍然有效,其他建议/示例可能会有所帮助.
这是在NSScriptCommand的evaluateArguments中传递字符串然后在Swift中作为脚本命令的结果返回该字符串的基本示例(您可以在命令成功/失败或任何其他类型的结果时返回布尔值;以及实际上,在你的sdef中,你说你要返回一个布尔值但你的命令返回一个字符串(sdef定义中的文本)).创建你的sdef可能很棘手.您的命令代码应该以套件的代码开头,您可以删除id和optional参数(如果省略可选参数,则默认为需要参数).如果你只需要一个参数,你也可以使用direct-parameter代替.
您可以下载演示项目:
ScriptableSwift.zip
以下是相关位(除了您在测试中正确的plist条目).
ScriptableSwift.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>
SaveScriptCommand.swift
import Foundation import Cocoa class SaveScriptCommand: NSScriptCommand { override func performDefaultImplementation() -> AnyObject? { let filePath = self.evaluatedArguments!["FilePath"] as! String debugPrint("We were prompted to save something at: \(filePath)"); return filePath } }
测试AppleScript
tell application "ScriptableSwift" to save in "path/to/file"
结果:
"path/to/file"