The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniGraffle General (http://forums.omnigroup.com/forumdisplay.php?f=10)
-   -   Creating chart via AppleScript is slow? (http://forums.omnigroup.com/showthread.php?t=30416)

cscooper 2013-07-22 09:01 AM

Creating chart via AppleScript is slow?
 
Am I doing something wrong?

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

[FONT="Courier New"]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
[/FONT]
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

RobTrew 2013-07-22 09:23 AM

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 [I]connect[/I] 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[/CODE]

RobTrew 2013-07-22 09:34 AM

And, of course, [I]layout [/I] is a very slow and expensive method ...

cscooper 2013-07-22 09:37 AM

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 ...

cscooper 2013-07-22 09:39 AM

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

RobTrew 2013-07-22 09:49 AM

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 [I]get[/I] operation across the Applescript interface each time.


All times are GMT -8. The time now is 10:06 PM.

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