3 Replies Latest reply on Nov 19, 2015 7:03 PM by Chuck Uebele

    PS JS script aligning image to center, want to align to left.

    gingerj Level 1

      Hi there, i have a PS script that replaces an image layer in the PSD file with an external PNG file. and saves the newly created as a new PNG.


      Right now, the way it works, it aligns the image to the center in the bounds of the earlier image.

       

      What i would like is that it aligns it to the left in the bounds of the earlier image, instead of center aligning it.

       

      Here's the code that i'm using that I think center aligns to the bounds-


      [code]

      pasted_bounds=psdDoc.activeLayer.bounds;

      flag_width=Number(pasted_bounds[2]-pasted_bounds[0]);

      flag_height=Number(pasted_bounds[3]-pasted_bounds[1]);


      psdDoc.activeLayer.translate(original_bounds[0]-pasted_bounds[0]+((original_width-flag_wid th)/2),original_bounds[1]-pasted_bounds[1]+((original_height-flag_height)/2));

       

      [/code]


      Looking forward to the community. Thanks.

       

      Also, i miss PS-Scripts community and Mike

       

      Here's the complete code in case someone wants to look at it -

      PS it also replaces a text image using list1.txt, that's working fine and i didn't include code for it.

       

      [code]

      var working_directory="/C/work-files/";

      var image_txt="list2.txt";

      var display_name_txt="list1.txt";

       

       

      //----------------------------------COMMON HOUSEKEEPING---------------------------------------------------------

      var strtRulerUnits = app.preferences.rulerUnits;

      var strtTypeUnits = app.preferences.typeUnits;

      app.preferences.rulerUnits = Units.INCHES;

      app.preferences.typeUnits = TypeUnits.PIXELS;

      app.displayDialogs = DialogModes.NO;

      //---------------------------------------------------------------------------------------- --------

      //-------------------------OPEN GUIDE PSD FILE----

      guide_file=new File(working_directory+"guide.psd");

      if (!guide_file.exists)  Window.alert("guide.psd not found in the working directory");

      open(guide_file);

       

       

      psdDoc=app.activeDocument;

      flagLayer=psdDoc.artLayers.getByName("ChangeMe2");

      shapeLayer=psdDoc.artLayers.getByName("Shape 1");

      flagLayer.moveAfter(shapeLayer);

      //flagLayer.move(shapeLayer,ElementPlacement.INSIDE);

      //Window.alert(psdDoc.artLayers.length);

       

       

      //Window.alert(shapeLayer.kind);

      for(i=0;i<psdDoc.artLayers.length;i++){

        psdDoc.artLayers[i].visible=false;

      }

      flagLayer.visible=true;

      original_bounds=flagLayer.bounds;

       

       

      psdDoc.trim(TrimType.TRANSPARENT,true,true,true,true);

       

      original_width=Number(original_bounds[2]-original_bounds[0]);

       

      original_height=Number(original_bounds[3]-original_bounds[1]);

      original_resolution=Number(psdDoc.resolution);

      original_ar=original_width/original_height;

      psdDoc.close(SaveOptions.DONOTSAVECHANGES);

      //-------------------------CLOSE GUIDE PSD FILE-----------------------------------

       

       

      txtfile_text=new File(working_directory+display_name_txt);

      Window.alert(txtfile_text.open('r') ? "LIST1:OK" :"File Not Found:names list" );

      txtfile_image=new File(working_directory+image_txt);

      Window.alert(txtfile_image.open('r') ? "LIST2:OK" : "File Not Found: Image list");

       

       

       

       

       

       

      while( pic_file= txtfile_image.readln())

      {

      flag_name= txtfile_text.readln();

       

       

      file2=new File(working_directory+pic_file);

      open(file2);

      var picDoc=app.documents.getByName(pic_file);

      app.activeDocument=picDoc;

       

       

       

       

      picDoc.trim(TrimType.TRANSPARENT,true,true,true,true);

      flag_bounds=picDoc.activeLayer.bounds;

      flag_width=Number(picDoc.width);

      flag_height=Number(picDoc.height);

      flag_ar=flag_width/flag_height;

      flag_resolution=Number(picDoc.resolution);

       

       

      diff_width=Math.abs(original_width*original_resolution-flag_width*flag_resolution);

      diff_height=Math.abs(original_height*original_resolution-flag_height*flag_resolution);

       

        picDoc.resizeImage( Number(flag_ar)*Number(original_height), original_height,original_resolution);

        }else{

        picDoc.resizeImage( original_width, (Number(original_width)/flag_ar),original_resolution);

        }

       

       

       

       

       

      //picDoc.resizeImage(original_width,original_height,original_resolution);

      picDoc.selection.selectAll();

      picDoc.selection.copy();

       

       

      picDoc.close(SaveOptions.DONOTSAVECHANGES);

       

       

      //---------------------------OPEN GUIDE PSD FILE ----------------------

      open(new File(working_directory+"guide.psd"));

      psdDoc=app.activeDocument;

      flagLayer=psdDoc.artLayers.getByName("ChangeMe2");

      psdDoc.activeLayer=flagLayer;

      psdDoc.selection.selectAll();

      psdDoc.selection.clear();

      psdDoc.selection.deselect();

      psdDoc.paste();

      pasted_bounds=psdDoc.activeLayer.bounds;

      flag_width=Number(pasted_bounds[2]-pasted_bounds[0]);

      flag_height=Number(pasted_bounds[3]-pasted_bounds[1]);

      //psdDoc.activeLayer.translate(original_bounds[0]-pasted_bounds[0],original_bounds[1]-past ed_bounds[1]);

      //permit vertical alignment

      //psdDoc.activeLayer.translate(0,original_bounds[1]-pasted_bounds[1]);//commented out on 1/14/2009 -ISSUE2

      psdDoc.activeLayer.translate(original_bounds[0]-pasted_bounds[0]+((original_width-flag_wid th)/2),original_bounds[1]-pasted_bounds[1]+((original_height-flag_height)/2));

       

       

      [/code]