Copy link to clipboard
Copied
I created some paths and grouped them with a script. Then I selected an item below them and tried getting zOrderPosition of it with a script. And I got an error.
Is there a way to get rid of this?
Here is a code.
Please select a path and run test1 or test2 twice. You'll get an error at the second time.
(Tested with Ai CS3 and CC 2015)
Thanks.
function test1(){ // error (at the second time)
var lay = activeDocument.activeLayer;
var group = lay.groupItems.add();
var p1 = lay.pathItems.ellipse(100,100,10,10);
var p2 = p1.duplicate();
var p3 = p1.duplicate();
p2.move(group, ElementPlacement.PLACEATEND);
p3.move(group, ElementPlacement.PLACEATEND);
alert(activeDocument.selection[0].zOrderPosition); // error
}
function test2(){ // error (at the second time)
var lay = activeDocument.activeLayer;
var group = lay.groupItems.add();
var p1 = group.pathItems.ellipse(100,100,10,10);
var p2 = p1.duplicate();
alert(activeDocument.selection[0].zOrderPosition); // error
}
function test3(){ // NO error
var lay = activeDocument.activeLayer;
var group = lay.groupItems.add();
var p1 = lay.pathItems.ellipse(100,100,10,10);
var p2 = p1.duplicate();
p2.move(group, ElementPlacement.PLACEATEND);
alert(activeDocument.selection[0].zOrderPosition); // NO error
}
test1();
Error present. If all ungroup, the error will not happen. Alternatively, you can try:
...function zIndex ( obj ) {
var arr = obj.parent.pageItems, i = arr.length;
while ( i-- ) { if ( arr === obj ) return i; }
return -1;
}
function test1() {
var lay = activeDocument.activeLayer;
var group = lay.groupItems.add();
var p1 = lay.pathItems.ellipse(100,100,10,10);
var p2 = p1.duplicate();
var p3 = p1.duplicate();
p2.move(group, ElementPlacement.PLACEATEND);
p3.move(group, Eleme
Copy link to clipboard
Copied
Yes, I see this error. It even persists if you save, close and re-open the document. I think I saw something a long time ago where someone said zOrder is a broken feature.
Copy link to clipboard
Copied
Thank you for testing.
I didn't think it persists after saving. It's troublesome.
It means zOrder is not reliable in an existing document.
I can't use it anymore.
Copy link to clipboard
Copied
it doesn't fail the second time all the time, it depends on the position, if your selection is the bottom most, your test1() fails the second time, if your selection is the second from the bottom it will fail the 3rd time, etc.
I agree it might be broken, as an alternative you could use absoluteZOrderPosition, it worked well with your test1() function.
Copy link to clipboard
Copied
I don't have thought about position. That can be a clue to figure out how zOrder broken.
absoluteZOrderPosition is new to me. I'll try using it.
Thank you for your suggestion.
Copy link to clipboard
Copied
Error present. If all ungroup, the error will not happen. Alternatively, you can try:
function zIndex ( obj ) {
var arr = obj.parent.pageItems, i = arr.length;
while ( i-- ) { if ( arr === obj ) return i; }
return -1;
}
function test1() {
var lay = activeDocument.activeLayer;
var group = lay.groupItems.add();
var p1 = lay.pathItems.ellipse(100,100,10,10);
var p2 = p1.duplicate();
var p3 = p1.duplicate();
p2.move(group, ElementPlacement.PLACEATEND);
p3.move(group, ElementPlacement.PLACEATEND);
alert( zIndex(activeDocument.selection[0]) ); // no error
}
function test2() {
var lay = activeDocument.activeLayer;
var group = lay.groupItems.add();
var p1 = group.pathItems.ellipse(100,100,10,10);
var p2 = p1.duplicate();
alert( zIndex(activeDocument.selection[0]) ); // no error
}
function test3() {
var lay = activeDocument.activeLayer;
var group = lay.groupItems.add();
var p1 = lay.pathItems.ellipse(100,100,10,10);
var p2 = p1.duplicate();
p2.move(group, ElementPlacement.PLACEATEND);
alert( zIndex(activeDocument.selection[0]) ); // no error
}
test1(); test2(); test3();
Copy link to clipboard
Copied
Thank you for your code.
This must be the most sure approach, though it takes long in some condition.
zOrderPosition is broken even in this case.
But now I guess it's unavoidable.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now