Skip to main content
Participating Frequently
June 1, 2016
Answered

zOrderPosition error caused by 'duplicate' and 'move' methods

  • June 1, 2016
  • 3 replies
  • 1203 views

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();

This topic has been closed for replies.
Correct answer Alexander Ladygin

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();

3 replies

Alexander Ladygin
Alexander LadyginCorrect answer
Inspiring
June 2, 2016

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();

Participating Frequently
June 2, 2016

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.

CarlosCanto
Community Expert
Community Expert
June 2, 2016

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.

Participating Frequently
June 2, 2016

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.

Silly-V
Legend
June 2, 2016

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.

Participating Frequently
June 2, 2016

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.