从AppleScript调用Swift方法

从AppleScript调用Swift方法,swift,applescript,sdef,Swift,Applescript,Sdef,我正在尝试编写一个AppleScript,它调用我的Swift应用程序来获取一个值。该方法接受一个字符串并需要返回另一个字符串 这是我的.SDF文件: <suite name="My Suite" code="MySU" description="My AppleScript suite."> <class name="application" code="capp" description="An application's top level scripting o

我正在尝试编写一个AppleScript,它调用我的Swift应用程序来获取一个值。该方法接受一个字符串并需要返回另一个字符串

这是我的.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>
最后,我的AppleScript在这里:

tell application "MyApp"
    set r to my command with "Hello"
end tell

当我执行AppleScript时,它会识别我的命令,但它不会调用我试图与之关联的Swift代码。Xcode或AppleScript均未报告问题。我是否遗漏了某些内容或将代码放错了位置?

对于这种脚本,我建议使用命令优先(也称为动词优先)方法,而不是您尝试的对象优先方法。您的sdef如下所示(将“MyProject”替换为您的项目名称,即您的应用程序的Swift模块名称):


“ModuleName.ClassName”sdef提示来自
name=“my command”
,因为
my
是一个AppleScript关键字,我建议不要将其作为名称的一部分使用。这不会有什么好结果,这是一个完美的答案。谢谢你的帮助。安得烈
tell application "MyApp"
    set r to my command with "Hello"
end tell
<dictionary xmlns:xi="http://www.w3.org/2003/XInclude">
<suite name="My Suite" code="MySU" description="My AppleScript suite.">

    <command name="my command" code="MySUCMND" description="My Command">
        <cocoa class="MyProject.MyCommand"/>
        <parameter name="with" code="MyPR" description="my Parameter" type="text">   
            <cocoa key="myParameter"/>
        </parameter>
        <result type="text" description="the return value"/>
    </command>

</suite>
</dictionary>
class MyCommand : NSScriptCommand {

    override func performDefaultImplementation() -> Any? {
        if let _ = self.evaluatedArguments?["myParameter"] as? String
        {
            return "Hello World!"
        }
        else
        {
            return "Nothing happening here. Move on."
        }

    }
}