我正在尝试使用Applescript编写的Swift脚本编写的Cocoa应用程序.
我做了什么
我创建了一个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 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中
@L_403_1@
ScriptingSaveNotification.swift
import Foundationimport Cocoaclass 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 Editor中反映出来. Applescript编辑器还返回字典定义.但是当我运行命令时,我总是得到5(int)的输出,并且我的调试行似乎都没有在Xcode中输出.
在我看来,也许我在SDEF中不恰当地引用了我的课程.但我不是百分百肯定.我已经尝试过多次重命名了.任何帮助将不胜感激.
Applescript字典
测试脚本
tell application "MyApplication" set testString to "Hello" set returnValue to savedoc testString display alert returnValueend tell解决方法 编辑:
主要问题是您实际上并未在脚本中使用with dname参数.它应该是:
set returnValue to savedoc with dname testString
也就是说,下面的信息对于创建正确的sdef仍然有效,其他建议/示例可能会有所帮助.
这是在NSScriptCommand的evaluateArguments中传递字符串然后在Swift中作为脚本命令的结果返回该字符串的基本示例(您可以在命令成功/失败或任何其他类型的结果时返回布尔值;以及实际上,在你的sdef中,你说你要返回一个布尔值但你的命令返回一个字符串(sdef定义中的文本)).创建你的sdef可能很棘手.您的命令代码应该以套件的代码开头,您可以删除ID和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 /> <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 Foundationimport Cocoaclass 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"总结
以上是内存溢出为你收集整理的制作Cocoa Application Scriptable Swift全部内容,希望文章能够帮你解决制作Cocoa Application Scriptable Swift所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)