1 Reply Latest reply on Sep 19, 2015 12:09 AM by CarlosCanto

    Script inverte posições pelo centro do objeto

    beto correa Level 1

      Olá a todos!

       

      Preciso de uma Script que inverta posições de objetos selecionados pelo centro da imagem.

      Consegui fazer a inversão mas não consigo alinhar centro com centro. Alguém pode me ajudar?

      Obrigado!

      -----------------------------------------------------------------------------------

      Hello everyone!

      I need a script that reverses positions of selected objects to the center of the image .
      I managed to turn it around but I can not align center center. Can someone help me?
      Thank you!

      -----------------------------------------------------------------------------------

       

      try {

      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

        //create bounding objects

        var origBounds = mySelection[0].geometricBounds;

        var initBounds = mySelection[1].geometricBounds;

        //define paramaters of top object

        var oul_x = origBounds[0];

        var oul_y = origBounds[1];

        var olr_x = origBounds[2];

        var olr_y = origBounds[3];

        var oSelWidth = (olr_x-oul_x);

        var oSelHeight = (oul_y-olr_y);

        var oSelPos = [oul_x, oul_y];

        //define paramaters of bottom object

        var ul_x = initBounds[0];

        var ul_y = initBounds[1];

        var lr_x = initBounds[2];

        var lr_y = initBounds[3];

        var mySelWidth = (lr_x-ul_x);

        var mySelHeight = (ul_y-lr_y);

        var mySelPos = [ul_x, ul_y];

        //apply transforms to selection[0]

        mySelection[1].height = mySelHeight;

        mySelection[1].width = mySelWidth;

        mySelection[0].position = mySelPos;

        //apply transforms to selection[1]

        mySelection[0].height = oSelHeight;

        mySelection[0].width = oSelWidth;

        mySelection[1].position = oSelPos;

       

                  } else {

              }

          }

      }

      } catch(e){

      }

        • 1. Re: Script inverte posições pelo centro do objeto
          CarlosCanto Adobe Community Professional & MVP

          you're almost there,

           

          first, get each of the objects' centers

          var oSelPos = [oul_x+oSelWidth/2, oul_y-oSelHeight/2]; // center position
          var mySelPos = [ul_x + mySelWidth/2, ul_y-mySelHeight/2]; // center position
          

           

          then, move object 0 to center 1, and adjust half of object's width/height...same for object 1

          mySelection[0].position = [mySelPos[0]-oSelWidth/2, mySelPos[1]+oSelHeight/2];
          

           

           

          try {
              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
                          //create bounding objects
                          var origBounds = mySelection[0].geometricBounds;
                          var initBounds = mySelection[1].geometricBounds;
                          //define paramaters of top object
                          var oul_x = origBounds[0];
                          var oul_y = origBounds[1];
                          var olr_x = origBounds[2];
                          var olr_y = origBounds[3];
                          var oSelWidth = (olr_x - oul_x);
                          var oSelHeight = (oul_y - olr_y);
                          var oSelPos = [oul_x+oSelWidth/2, oul_y-oSelHeight/2]; // center position
          
          
                          //define paramaters of bottom object
                          var ul_x = initBounds[0];
                          var ul_y = initBounds[1];
                          var lr_x = initBounds[2];
                          var lr_y = initBounds[3];
                          var mySelWidth = (lr_x - ul_x);
                          var mySelHeight = (ul_y - lr_y);
                          var mySelPos = [ul_x + mySelWidth/2, ul_y-mySelHeight/2]; // center position
          
          
                          //apply transforms to selection[0]
                          mySelection[0].position = [mySelPos[0]-oSelWidth/2, mySelPos[1]+oSelHeight/2];
                          //mySelection[0].position = mySelPos;
                          
                          //apply transforms to selection[1]
                          mySelection[1].position = [oSelPos[0]-mySelWidth/2, oSelPos[1]+mySelHeight/2];
                          //mySelection[1].position = oSelPos;
          
          
                      } else {}
                  }
              }
          } catch (e) {}