Created
December 16, 2011 18:34
-
-
Save iloveitaly/1487305 to your computer and use it in GitHub Desktop.
Convert Hierarchical OmniGraffle Document to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# rubycocoa | |
require 'osx/cocoa' | |
include OSX | |
OSX.require_framework 'ScriptingBridge' | |
class GraffleConverter | |
def initialize | |
@graffle = SBApplication.applicationWithBundleIdentifier_("com.omnigroup.OmniGraffle") | |
end | |
def to_hash | |
@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.collect { |root| process_node(root) } | |
end | |
# ========== | |
# { | |
# 'name':'node name', | |
# 'children': [ | |
# {...}, {...} | |
# ] | |
# } | |
# ========== | |
private | |
def process_node(shape) | |
children = [] | |
shape.outgoingLines.each do |line| | |
# destination is a graphic, not a shape, was having some issues working with shapes | |
next_shape = @shape_list.detect { |d| d.valueForKey_("id") == line.destination.valueForKey_("id") } | |
children << process_node(next_shape) | |
end | |
{ :children => children, :name => shape.text.get.to_s } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'graffle2hash' | |
begin | |
require 'rubygems' | |
require 'json' | |
has_json = true | |
rescue Exception => e | |
has_json = false | |
end | |
graffle_structure = GraffleConverter.new.to_hash | |
# this is for the spree commerce product importer | |
# convert to md formatted product lists | |
def find_node name, node_list | |
d = node_list.detect { |n| n[:name] == name } | |
return d if d | |
node_list.each do |n| | |
r = find_node name, n[:children] | |
return r if not r.nil? | |
end | |
nil | |
end | |
def print_node node_list, path = [], level = 1 | |
node_list.each do |n| | |
if n[:children].length > 0 | |
puts ("#" * level) + n[:name] | |
print_node n[:children], path.dup.push(n[:name]), level + 1 | |
else | |
puts path.join(" > ") + " > " + n[:name] + " " | |
end | |
end | |
end | |
if has_json | |
puts find_node("Main Menu Bar", graffle_structure).to_json | |
else | |
print_node find_node("Main Menu Bar", graffle_structure)[:children] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment