How to swap two objects?

Sometimes I want to swap two objects, so one of selected will be on second’s place, and vice versa.
There is any simple way to do it?


In addition to Naty’s answer, the “swap objects” script by Wundes also swaps the dimensions of the objects. I made a similar script which just swaps the position regardless of the dimensions.

The script swaps 2 objects based on the objects center x & y.

Here you go:

try { 
    var selObjs = "Please select two objects on the page.";
    var docRef = activeDocument;
    if (documents.length>0) {
        if (docRef.selection.length == 2) {
            mySelection = docRef.selection;

            //if object is a (collection of) object(s) not a text field.
            if (mySelection instanceof Array) {
                //initialize vars
                var object_A = mySelection[0];
                var object_B = mySelection[1];

                // Calculate new center object A
                new_A = [
                // Calculate new center object B
                new_B = [
                // Make the swap
                object_A.position = new_A;
                object_B.position = new_B;

            } else {
                alert(mySelection+" is not an array!\n"+selObjs);
        } else {
            alert("Selection is not 2 objects!\n"+selObjs);
} catch(e){

Source : Link , Question Author : Даниил Пронин , Answer Author : Cai

Leave a Comment