【发布时间】:2016-12-04 10:30:08
【问题描述】:
我正在尝试编写一个调用我的 Swift 应用程序以获取值的 AppleScript。该方法接受一个字符串,需要返回另一个字符串。
这是我的 .SDF 文件:
<suite name="My Suite" code="MySU" description="My AppleScript suite.">
<class name="application" code="capp" description="An application's top level scripting object.">
<cocoa class="NSApplication"/>
<element type="my types" access="r">
<cocoa key="types"/>
</element>
</class>
<command name="my command" code="MyCOMMND" description="My Command">
<parameter name="with" code="MyPR" description="my Parameter" type="text">
<cocoa key="myParameter"/>
</parameter>
<result type="text" description="the return value"/>
<cocoa method="myCommand:"/>
</command>
</suite>
对应的 Swift 代码相当简单:
func myCommand(_ command: NSScriptCommand) -> String
{
if let myParameter = command.evaluatedArguments?["myParameter"] as? String
{
return "Hello World!"
}
else
{
return "Nothing happening here. Move on."
}
}
我的 AppleScript 终于来了:
tell application "MyApp"
set r to my command with "Hello"
end tell
当我执行 AppleScript 时,它会识别我的命令,但不会调用我尝试与之关联的 Swift 代码。 Xcode 或 AppleScript 都没有报告问题。我是否遗漏了什么或将我的代码放在了错误的位置?
【问题讨论】:
-
name="my command"由于my是 AppleScript 关键字,我建议不要将其用作名称的一部分。没有什么好处。
标签: swift applescript sdef