使 Cocoa 应用程序可编写脚本 Swift

Day*_*der 5 macos scripting cocoa applescript swift

目标

我正在尝试使我用 Swift 编写的 Cocoa 应用程序可以从 Applescript 编写脚本。

我做过什么

我创建了一个 SDEF 文件,配置了我的 info.plist 并创建了一个我认为合适的类。

定义.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>
Run Code Online (Sandbox Code Playgroud)

信息.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
    }
}
Run Code Online (Sandbox Code Playgroud)

我在哪里

我有一个启动的应用程序。应用程序的 SDEF 文件似乎反映在 Applescript 编辑器中。Applescript 编辑器还返回字典定义。但是,当我运行命令时,我总是得到 5 (int) 的输出,并且我的调试行似乎都没有在 Xcode 中输出。

在我看来,也许我在 SDEF 中引用了我的类不正确。但我不是 100% 确定。我试过多次重命名它。任何帮助将不胜感激。

Applescript 字典 在此处输入图片说明

测试脚本

tell application "MyApplication"
    set testString to "Hello"
    set returnValue to savedoc testString
    display alert returnValue
end tell
Run Code Online (Sandbox Code Playgroud)

Jon*_*Jon 5

编辑:

主要问题是您实际上并没有with dname在脚本中使用该参数。它应该是:

set returnValue to savedoc with dname testString
Run Code Online (Sandbox Code Playgroud)

也就是说,下面的信息对于创建正确的信息仍然有效sdef,其他建议/示例可能会有所帮助。


这是一个基本示例,在 中传递一个字符串evaluatedArgumentsNSScriptCommand然后返回该字符串作为 Swift 中脚本命令的结果(您可以在命令成功/失败或任何其他类型的结果时返回布尔值;并且,实际上,在你sdef说你要返回 aboolean但你的命令返回 a stringtextsdef定义中))。创建你的sdef可能很棘手。您的命令代码应以套件代码开头,并且您可以删除idoptional参数(如果省略该optional参数,则默认该参数是必需的)。如果您只需要一个参数,您也可以使用direct-parameter来代替。

您可以下载一个演示项目:

脚本化Swift.zip

以下是相关部分(除了plist您在测试中正确的条目之外)。

脚本化Swift.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>
Run Code Online (Sandbox Code Playgroud)

保存脚本命令.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
    }
}
Run Code Online (Sandbox Code Playgroud)

测试AppleScript

tell application "ScriptableSwift" to save in "path/to/file"
Run Code Online (Sandbox Code Playgroud)

结果:

"path/to/file"
Run Code Online (Sandbox Code Playgroud)