[MacRuby-devel] Scripting OmniGraffle

Kam Dahlin haxie1 at me.com
Tue Nov 15 15:11:43 PST 2011


Hi Sophie,

Here is a little example, that I hope makes things more clear.

graffle = SBApplication.applicationWithBundleIdentifier("com.omnigroup.OmniGrafflePro")

win = graffle.windows.first
canvas = win.canvas

circle = OmniGraffleProfessionalShape.alloc.initWithProperties({:name=>"Circle", :origin=>[100.00, 100.00], :size=>[300, 300]})
canvas.shapes.addObject(circle)

The example above uses the class name that should match what is in the header file that you generated. 

You can also use a slightly different method for getting the correct class:

circle = graffle.classForScriptingClass("shape").alloc.initWithProperties({:name=>"Circle", :origin=>[100.00, 100.0], :size=>[300.0, 300.0]})

the -classForScriptingClass method takes the name of the class you would use in Applescript and then returns the class that the ScriptingBridge machinery expects. Just a slightly different approach and useful if you know the name of the class you would use in Applescript, but not the actual name used by ScriptingBridge.

If you look in the header that you generated for OmniGraffle, you will see a bunch of @property declarations in each of the class definitions. The names of these properties you use as the keys in the initWithProperties method. Alternately you could also do:

circle = OmniGraffleProfessionalShape.alloc.init
canvas.shapes.addObject(circle)
circle.setName("Circle")
circle.setOrigin([100.0, 100.0])
circle.setSize([300.0, 300.0])

If you are going to set properties on an object, it must be added to a container first. 

Most of this is explained pretty well in the ScriptingBridge documentation, which is probably worth a look: 
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptingBridgeConcepts/AboutScriptingBridge/AboutScriptingBridge.html#//apple_ref/doc/uid/TP40006104-CH3-SW9

hth
kam

P.S. the origin and size properties are actually defined as part of the OmniGraffleProfessionalGraphic class which OmniGraffleProfessionalShape inherits from.

On Nov 15, 2011, at 11:43 AM, Sophie wrote:

> Sorry for the sloppy subject line in my earlier question.
> 
> Thanks, Kam, Matt for your very helpful replies. 
> 
> Kam, any idea how I would initialize the alloc'd shape to be e.g. a Circle of some size at some position?
> 
> I generated the files with sdef & gen_bridge_metadata, found lots of useful things there. Is there something in these files that would lead me to:
> 
> shape = OmniGraffleShape.alloc.init 
> canvas.shapes.addObject shape
> 
> ?  I don't see either alloc or init, or addObject in the 2 files.
> 
> Also, is there some reason why there is no corresponding "make ..." method in either file? Applescript gladly seems to call "make" (so does Ruby Appscript) on doc, canvas, or layer ... but I'm no Applescript guru :-(
> 
> Thanks !! 
> 
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel



More information about the MacRuby-devel mailing list