Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.