【发布时间】: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% 确定。我试过多次重命名它。任何帮助将不胜感激。
测试脚本
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