rubymacrubyscripting-bridgerb-appscript

Rewriting MacTypes::FileURL from appscript-ruby to MacRuby ScriptingBridge


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?


Solution

  • 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!