将appsType-ruby中的MacTypes :: FileURL重写为MacRuby ScriptingBridge

时间:2012-02-11 17:31:00

标签: ruby macruby scripting-bridge rb-appscript

我有一个脚本会自动将给定的PDF附加到BibDesk中的出版物。使用appscript-rb,以下代码段完美无缺:

BibDesk = Appscript.app('BibDesk')
selection = BibDesk.document.selection.get[0]
f = MacTypes::FileURL.path(curfile)
selection[0].linked_files.add(f,{:to =>Selection[0]})
selection[0].auto_file

尝试为MacRuby重写它,我想出了以下内容:

framework 'Cocoa'
framework 'ScriptingBridge'

file=NSURL.fileURLWithPath("file:///Users/Stian/Downloads/Telearn.pdf")
dt=SBApplication.applicationWithBundleIdentifier("edu.ucsd.cs.mmccrack.bibdesk")
d= dt.documents[0].selection[0]
d.linkedFiles.add(file,[:to=>dt.documents[0].selection[0]])

但是,这会崩溃MacRuby(我假设也是因为它错了)。我得到:

 84829 abort      macruby attach_bibdesk.rb

如何将appscript-ruby重写为正确的MacRuby ScriptingBridge格式?

1 个答案:

答案 0 :(得分:1)

这应该有效:

 framework 'Cocoa'
 framework 'ScriptingBridge'

 file_path = NSURL.fileURLWithPath("/Users/Stian/Downloads/Telearn.pdf")
 bib_desk = SBApplication.applicationWithBundleIdentifier("edu.ucsd.cs.mmccrack.bibdesk")
 selected_doc = bib_desk.documents.first.selection.first
 bib_desk.add(file_path, to:selected_doc)

文件路径声明可能是您遇到问题的原因,但我不确定!