View Single Post
I'm trying to get data from a database and build objects in Omnigraffle 5.4.4 pro based on that. For this, I use scriptingBridge and macRuby. I'm able to build some shapes in Omnigraffle but I need to group them altogether and I'm stuck at this point since 2 weeks.

Did somebody already figured out how to group shapes ? I used sdef to get the scripting definitions and I saw the methode "- (OmniGraffleProfessional5Group *) assembleSubgraph:(BOOL)subgraphtableShape:(NSArray *)tableShape; // Group graphics". Seems like I need to use this one but I really have an issue with this method. Looks like it returns nothing. Maybe I use it wrongly. I also tried to create a group and initialise it with an array of shapes but I cannot make it work too.

Anybody has an idea ?

Here is my code:
Code:
#!/usr/bin/env macruby
#framework 'Cocoa'
framework 'Foundation' 
framework 'ScriptingBridge'
load_bridge_support_file 'OmniGraffleProfessional5.bridgesupport'


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

# Omnigraffle scriptingBridge header
# sdef  /Applications/OmniGraffle\ Professional\ 5.app/ | sdp -fh -o ./ -A --basename OmniGraffleProfessional
# .bridgesupport file
# gen_bridge_metadata -c '-I.' OmniGraffleProfessional.h -o ./OmniGraffleProfessional.bridgesupport
# And include that bridgsupport file in your script with

pixelRatio = 28.3465
# origin in cm
originX = 1 * pixelRatio 
originY = 1 * pixelRatio 

win = graffle.windows.first 
canvas = win.canvas
# data to fill into object
header="APS"
template="template name"
# CMS version
cms_version = "1.0"
# or use query: select name from info_version where version_type='config' and is_current_version=1; (but need to check with R&D)
notes = ""
# object config
data = []

# headers
objectHeader = OmniGraffleProfessional5Shape.alloc.initWithProperties({:text=>{:alignment=>"1330929969", :color=>[1,1,1], :font=>"Helvetica-Bold", :size=>11, :text=>header}, :textPlacement=>"Center", :name=>"Rectangle", :origin=>[originX, originY], :size=>[9*pixelRatio, 0.5*pixelRatio], :drawsShadow=>0, :drawsStroke=>1, :fillColor=>[0.0972915, 0.188266, 0.434028], :strokeColor=>[0.0972915, 0.188266, 0.434028], :sidePadding=>2})
objectNameHeader = OmniGraffleProfessional5Shape.alloc.initWithProperties({:text=>{:alignment=>"left", :color=>[0,0,0], :font=>"Helvetica", :size=>9, :text=>"Name"}, :textPlacement=>"Center", :name=>"Rectangle", :origin=>[originX, originY+0.5*pixelRatio], :size=>[2*pixelRatio, 0.5*pixelRatio], :drawsShadow=>0, :drawsStroke=>1, :fillColor=>[0.329412,0.490181,0.737255], :strokeColor=>[0.0972915, 0.188266, 0.434028], :sidePadding=>2})
objectTemplateHeader = OmniGraffleProfessional5Shape.alloc.initWithProperties({:text=>{:alignment=>"left", :color=>[0,0,0], :font=>"Helvetica", :size=>9, :text=>"Template"}, :textPlacement=>"Center", :name=>"Rectangle", :origin=>[originX, originY+1*pixelRatio], :size=>[2*pixelRatio, 1*pixelRatio], :drawsShadow=>0, :drawsStroke=>1, :fillColor=>[0.329412,0.490181,0.737255], :strokeColor=>[0.0972915, 0.188266, 0.434028], :sidePadding=>2})
objectVersionHeader = OmniGraffleProfessional5Shape.alloc.initWithProperties({:text=>{:alignment=>"left", :color=>[0,0,0], :font=>"Helvetica", :size=>9, :text=>"CMS version"}, :textPlacement=>"Center", :name=>"Rectangle", :origin=>[originX, originY+2*pixelRatio], :size=>[2*pixelRatio, 0.5*pixelRatio], :drawsShadow=>0, :drawsStroke=>1, :fillColor=>[0.329412,0.490181,0.737255], :strokeColor=>[0.0972915, 0.188266, 0.434028], :sidePadding=>2})
# data
objectName = OmniGraffleProfessional5Shape.alloc.initWithProperties({:text=>{:alignment=>"left", :color=>[0,0,0], :font=>"Helvetica", :size=>9, :text=>"<to configure>"}, :textPlacement=>"Center", :name=>"Rectangle", :origin=>[originX+2*pixelRatio, originY+0.5*pixelRatio], :size=>[7*pixelRatio, 0.5*pixelRatio], :drawsShadow=>0, :drawsStroke=>1, :fillColor=>[1,1,1], :strokeColor=>[0.0972915, 0.188266, 0.434028], :sidePadding=>2})
objectTemplate = OmniGraffleProfessional5Shape.alloc.initWithProperties({:text=>{:alignment=>"left", :color=>[0,0,0], :font=>"Helvetica", :size=>9, :text=>template}, :textPlacement=>"Center", :name=>"Rectangle", :origin=>[originX+2*pixelRatio, originY+1*pixelRatio], :size=>[7*pixelRatio, 1*pixelRatio], :drawsShadow=>0, :drawsStroke=>1, :fillColor=>[1,1,1], :strokeColor=>[0.0972915, 0.188266, 0.434028], :sidePadding=>2})
objectVersion = OmniGraffleProfessional5Shape.alloc.initWithProperties({:text=>{:alignment=>"left", :color=>[0,0,0], :font=>"Helvetica", :size=>9, :text=>cms_version}, :textPlacement=>"Center", :name=>"Rectangle", :origin=>[originX+2*pixelRatio, originY+2*pixelRatio], :size=>[7*pixelRatio, 0.5*pixelRatio], :drawsShadow=>0, :drawsStroke=>1, :fillColor=>[1,1,1], :strokeColor=>[0.0972915, 0.188266, 0.434028], :sidePadding=>2})
# make group

# adding shapes to canvas because otherwise I get an error: "object has not been added to a container yet; selector not recognised"
canvas.shapes.addObject(objectHeader)
canvas.shapes.addObject(objectNameHeader)
canvas.shapes.addObject(objectTemplateHeader)
canvas.shapes.addObject(objectVersionHeader)
canvas.shapes.addObject(objectName)
canvas.shapes.addObject(objectTemplate)
canvas.shapes.addObject(objectVersion)

# making an array of all shapes
shapesToGroup = NSArray.arrayWithObjects(objectHeader, objectNameHeader, objectTemplateHeader, objectVersionHeader, objectName, objectTemplate, objectVersion, nil) 

puts shapesToGroup.className
# => __NSArrayI

# grouping shapes. The output should be a group
objectGroup = canvas.assembleSubgraph(false,:tableShape=>shapesToGroup)

puts objectGroup.className
# => NSNull
canvas.shapes.addObject(objectGroup)
#=> NSInvalidArgumentException: *** -[SBElementArray addObject:]: can't add an object that already exists. (RuntimeError)
Thanks

Tiago