3 Replies Latest reply on Jul 31, 2010 1:31 AM by Paul Riggott

    Help with script to translate randomly-placed layer into specific coordinates.

    navand

      I have a set of images in a folder. They all have the same size (200px x 200px).

       

      I create a large blank document, and I'm making a script to put all the images in my large document as a series of layers.

       

      The idea is to make image1 align with the left edge of the document, and then have the rest of images follow to the right (imageX is X*250 pixels away from the left edge of the document). That way I would get a series of images that start at the left edge and go right, separated by 50 pixels from each other.

       

      My code is something like this:

      var folderPath=Folder("C:/bleh/images/");
      var images=10;
      var imagesWidth=200;
      var separation=10
      
      var newFileName;
      
      for(var i=0;i<images;i++){
           newFileName="image"+counter+".jpg"
           app.load(File(folderPath+"/"+newFileName));     //open the new image
           backFile=app.activeDocument;
           backFile.selection.selectAll();                 //select it's contents
           backFile.selection.copy();                      //copy them
           backFile.close(SaveOptions.DONOTSAVECHANGES);   //close the image
           app.activeDocument.paste();                     //paste the image's contents into the large document
           app.activeDocument.layers[0].translate(20,0)    // <---- THIS IS THE PROBLEM
      
      }
      

       

      The problem is that when an image is pasted onto the large document, it seems that it aligns itself depending on the current view's zoom and seeing area. That means that each time the script is ran, the images will be pasted at different "global coordinates". This is a problem because the only way to move a layer seems to be with the layer.translate() function, which takes as parameters an amount in pixels to move the layer. I can't find anything that remotely looks like "align layer to the leftmost edge" or anything.

       

      I want to be able to translate them all the way to the left (and then translate them again in this manner: deltaX = i*(imagesWidth+separation)).

       

      So how can I find the gap in pixels between the left edge of the document and the edge of the layer/center of the layer?