AppleScript Help: Select all children
Gavin Kistner
gavin at refinery.com
Sat Apr 26 20:30:01 PDT 2003
[If you're impatient, skip to the very end for a list of summary
questions.]
I'm great with Javascript, but new to AppleScript.
I wanted to write a script to select all descendants of the currently
selected object(s)...but I am stymied again and again.
I read through the OmniGraffle dictionary, and even spent time making a
nice reference chart for it. (Unfortunately, using v3.0 pro I kept
getting various reallly ugly errors. [bug report coming, omnigroup.]
The final result, after various restarts, copy/paste to a new document,
copy/paste text out of corrupted objects and into copies, looks correct
in OG but the exported PDF crashes Preview and shows ugly boxes in
Acrobat 5.0. [I even tried remaking the objects which look wrong in
Acrobat, but that only added another to the list of broken-looking
ones.] If you want to see it, you can download the 77k OG3Pro file
http://phrogz.net/tmp/OmniGraffle%20AS%20Classes.graffle or the 418k
PNG http://phrogz.net/tmp/OmniGraffle%20AS%20Classes.png )
ANYhow...
I'm failing to get major pieces of this script to work using either
AppleScript or JavaScript. Some failed test code snippets that I don't
understand why they don't work:
//JavaScript
var og = MacOS.appBySignature("OGfl");
og.documents[0].pages[0].solids;
og.documents[0].pages[0].graphics;
og.documents[0].pages[0].shapes;
og.documents[0].pages[0].layers[0].graphics;
og.documents[0].pages[0].layers[0].solids;
og.documents[0].pages[0].layers[0].shapes;
//every one of the above returns 'null', even though
//there are 7 boxes (with text) connected by 6 lines
--AppleScript
tell application "OmniGraffle Professional"
the count of graphics of the first layer of the first document
--"OmniGraffle Professional got an error: NSCannotCreateScriptCommand"
end tell
tell the front window of application "OmniGraffle Professional"
count of selection -- this works
the first item of selection -- this fails...doesn't like 'item' here
end tell
The following might be close to working, but:
a) I don't know how to actually select the object (adding it to the
collection doesn't work)
b) it's not recursing as it ought (I end up with 3 items in my array
rather than 7...maybe this JS engine is passing arrays by value and not
reference...and attempts to use a global array end up having it not
recognized inside the child functions)
var og = MacOS.appBySignature("OGfl");
SelectChildrenOfSelected();
function SelectChildrenOfSelected(){
og._strict=false;
var objsToSelect=[];
var selectedObjs = og.windows.first.selection;
for (var i=0,len= selectedObjs.length;i<len;i++){
SelectChildrenOf(selectedObjs[i],objsToSelect);
}
for (var i=0,len=obsToSelect.length;i<len;i++){
var objToSelect = objsToSelect[i];
//WHAT DO I DO HERE? THE FOLLOWING GIVES AN ERROR
selectedObjs[selectedObjs.length]=objToSelect;
}
}
function SelectChildrenOf(obj,arrayTracker){
if (obj==null) return;
arrayTracker[arrayTracker.length]=obj;
for (var j=0,len2=obj.outgoing_lines.length;j<len2;j++){
SelectChildrenOf(obj.outgoing_lines[j].destination,arrayTracker);
}
}
SO...
1) Using JS or AS -- how do I select an object?
2) (If you only know AS): why are the sample AS scripts above giving an
error?
3) Using AS, how do I get at the individual items in the selection?
4) How would I get the text contents of a single selected non-grouped
object? (My JS tests on properties of a single object object inside the
'selection' array seems to indicate that it is not of class
'solid'...but that's the only class I see with a .text property other
than label.)
5) [For OmniGroup] Having a unique class called 'selection', with a
plural of 'selection', which has the exact same properties as the
'graphic' class feels really bizarre. Couldn't there be some better way
in AS of expressing a collection of selected items?
More information about the OmniGraffle-Users
mailing list