The Omni Group
These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

Go Back   The Omni Group Forums > OmniGraffle > OmniGraffle General
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Creating chart via AppleScript is slow? Thread Tools Search this Thread Display Modes
Am I doing something wrong?

I have a simple script, it creates 50-some boxes and lines between them. Looks basically like this:

tell application "OmniGraffle 5"
make new document with properties {template:"Blank"}
tell canvas of front window
set Node0 to make new shape at end of graphics with properties {text:{alignment:left, text:"nargle"}, origin:{100.0, 20.0}, autosizing:full}
set Node1 to make new shape at end of graphics with properties {text:{alignment:left, text:"fubar"}, origin:{100.0, 20.0}, autosizing:full}
set lineNode0Node1 to make new line at end of graphics with properties {head type:"FilledArrow", stroke pattern:1}
set source of lineNode0Node1 to Node0
set destination of lineNode0Node1 to Node1
// etc. etc. etc.
layout
end tell
save document 1 in "/tmp/userSelectedStudy.graffle"
close
end tell

What I find strange is that running this script takes like 8 seconds on my rather new MacBook Pro. That's not terribly long, but much longer than I expected for such a simple chart.

Is there a better way to create a chart programatically (my main program is generating and then executing the AppleScript)? (1) Are there different AppleScript commands I should use? (2) Am I better off using some technology besides AppleScript?

Thanks,
Chris
 
Unless you want to generate XML files directly, the applescript library is probably the right interface, but it's not fast. You may just have to minimize traffic across it, by cacheing references wherever possible, and using built-in methods.

(Using the connect method for example, should prove a faster way to link two shapes - see below).


Code:
tell application id "OGfl"
	make new document with properties {template:"Blank"}
	tell canvas of front window
		set refGraphics to a reference to graphics
		
		set Node0 to make new shape at end of refGraphics with properties {text:{alignment:left, text:"nargle"}, origin:{100.0, 20.0}, autosizing:full}
		set Node1 to make new shape at end of refGraphics with properties {text:{alignment:left, text:"fubar"}, origin:{100.0, 20.0}, autosizing:full}
		
		--set lineNode0Node1 to make new line at end of graphics with properties {head type:"FilledArrow", stroke pattern:1}
		--set source of lineNode0Node1 to Node0
		--set destination of lineNode0Node1 to Node1
		
		-- REPLACE THE 3 LINES ABOVE WITH SOMETHING LIKE:
		connect Node0 to Node1 with properties {head type:"FilledArrow", stroke pattern:1}
		
		layout
	end tell
end tell
 
And, of course, layout is a very slow and expensive method ...
 
I can see the layout happening, it does take a second or two, that's one command I'm _not_ surprised at its slowness - laying out a bunch of nodes is a complicated process ...
 
I'll try your idea on using the "connect" command, that looks good. I don't know what you mean by "caching references wherever possible", can you give me an idea what that would look like?

And what do you think about the XML files ... I've seen that idea mentioned, but it seems like documentation doesn't exist so I would have to pretty much reverse-engineer an XML file to figure out what I need to do?

Thanks!
Chris
 
Most of the XML is straightforward, but the string formatting is stored as RTF, which I would personally tend to stay away from.

(You can edit the suffix of .graffle file to .zip, unzip it, and view the result in a text editor).

Cacheing references – I just mean that whenever you will need to reference the same object several times, store it in a variable and refer to that, rather than repeating the implicit get operation across the Applescript interface each time.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



All times are GMT -8. The time now is 03:54 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.