【问题标题】:Making Cocoa Application Scriptable Swift使 Cocoa 应用程序可编写脚本 Swift
【发布时间】:2016-09-08 18:07:46
【问题描述】:

目标

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

我做了什么

我创建了一个 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 编辑器中。 Applescript 编辑器还返回字典定义。但是,当我运行命令时,我总是得到 5 (int) 的输出,并且我的调试行似乎都没有在 Xcode 中输出。

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

Applescript 字典

测试脚本

tell application "MyApplication"
    set testString to "Hello"
    set returnValue to savedoc testString
    display alert returnValue
end tell

【问题讨论】:

  • 刚刚注意到您的脚本也有问题。您实际上并未在脚本中使用“with dname”参数。应该是:使用 dname testString 将 returnValue 设置为 savedoc
  • 就是这样。 : 脸。十分感谢你的帮助。随意编辑您的答案以包含该答案,我会接受它:)

标签: swift macos cocoa scripting applescript


【解决方案1】:

编辑:

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

set returnValue to savedoc with dname testString

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


这是一个基本示例,在 evaluatedArgumentsNSScriptCommand 中传递一个字符串,然后将该字符串作为 Swift 中脚本命令的结果返回(您可以在命令成功/失败时返回布尔值或任何其他类型的结果;实际上,在您的sdef 中,您说您将返回boolean,但您的命令将返回stringtextsdef 定义中))。创建您的sdef 可能会很棘手。您的命令代码应以套件代码开头,您可以删除idoptional 参数(如果省略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"

【讨论】:

  • 您先生是英雄和学者
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 2010-12-03
  • 2019-06-13
  • 1970-01-01
相关资源
最近更新 更多