Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created December 16, 2011 18:34
Show Gist options
  • Save iloveitaly/1487305 to your computer and use it in GitHub Desktop.
Save iloveitaly/1487305 to your computer and use it in GitHub Desktop.
Convert Hierarchical OmniGraffle Document to JSON
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
# rubycocoa
require 'osx/cocoa'
include OSX
OSX.require_framework 'ScriptingBridge'
graffle = SBApplication.applicationWithBundleIdentifier_("com.omnigroup.OmniGraffle")
root_nodes = []
json_rep = []
# ==========
# {
# 'name':'node name',
# 'children': [
# {...}, {...}
# ]
# }
# ==========
def process_node(shape)
children = []
# puts shape.inspect
# puts shape.name
# puts shape.text.get
shape.outgoingLines.each do |line|
# destination is a graphic, not a shape, was having some issues working with shapes
nextShape = $shape_list.detect { |d| d.valueForKey_("id") == line.destination.valueForKey_("id") }
children << process_node(nextShape)
end
{ :children => children, :name => shape.text.get.to_s }
end
$shape_list = []
graffle.windows[0].document.canvases[0].layers[0].shapes.select do |s|
$shape_list << s
s.incomingLines.length == 0 and s.outgoingLines.length > 1
end.each {|root| json_rep << process_node(root) }
puts JSON.pretty_generate json_rep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment