0

I have a script that automatically attaches a given PDF to a publication in BibDesk. Using appscript-rb, the following snippet works perfectly:

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

Trying to rewrite it for MacRuby, I came up with the following:

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]])

However, this crashes MacRuby (which I am assuming is also because it is wrong). I just get:

 84829 abort      macruby attach_bibdesk.rb

How can I rewrite the appscript-ruby into proper MacRuby ScriptingBridge format?

Stian Håklev
  • 1,240
  • 2
  • 14
  • 26

1 Answers1

1

this should work:

 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)

the file path declaration could be the reason of your problem, but I’m not sure!

Sean Mateus
  • 161
  • 1
  • 9
  • Perfect, thank you _so_ much! What's the to:selected_doc, I've never seen that Ruby construct. Is it like a hash => or? – Stian Håklev Feb 11 '12 at 20:31
  • 1
    the `bib_desk.add(file_path, to:selected_doc)` is a **method with keyword arguments**, this something that comes from Objective-C; it’s not a hash! Ruby 2.0 with have keyword arguments too. _PS: to find out which methods are applicable to an Object in Macruby you can use `object.methods(true,true) - Object.new.methods` this gives you all objective-C methods you can use_ hope you enjoy Macruby! ;-) – Sean Mateus Feb 12 '12 at 08:05