2 Replies Latest reply on Sep 22, 2015 6:17 AM by dadus33

    How to "save as" a document using scripts


      I want to use scripts to allow me to automatize creation of name badges. I have a PSD template of the badge that includes a slot for the photo and a text field for the name. The aproach I took in my script is simple:
      1. Prompt the user to select the source folder for the photos.

      2. For each folder in the folder, do the following:
          - resize the photo

          - copy the background layer of the photo

          - close the document and focus back on the PSD template

          - place the copied photo as a new layer

          - translate it to a certain position so it fits into the photo slot of the template

          - change the text in the text layer to the name of the photo we placed (which is the actual name of the person)

      3. Save the photo as jpg with some given save options in a certain output folder.


      Step 1 and 2 work just as needed. However, when I have to save the badge, I get a really strange error saying: 2QrAQ3T.png


      Here's the whole code (in JS) :

      var sourceFolder = Folder.selectDialog ("Selecteaza folderul cu pozele: ");  //prompt the user to select the source folder
      var files = new Array();
      files = sourceFolder.getFiles ();  //put the files from the folder inside an array
          var startRulerUnits = app.preferences.rulerUnits;  //save the initial units used
          var startTypeUnits = app.preferences.typeUnits;
          app.preferences.rulerUnits = Units.PIXELS;  //change the units to pixels so the translate command will work as expected
          app.preferences.typeUnits = TypeUnits.PIXELS;
      var mainDoc = app.documents.getByName ("Ecuson.psd"); //this is the name badge template
      var so = new JPEGSaveOptions(); //the save options i'll use with the save command
      so.embedColorProfile = true;
      so.formatOptions = FormatOptions.STANDARDBASELINE;
      so.matte = MatteType.WHITE;
      so.quality = 10;
      for(var i = 0; i<files.length; ++i){ //loop thru each photo in the folder
          var img = app.open(files[i]); //open the image
          var saveFile = new File("C:/Users/Eugen/Desktop/salvate/" + String.valueOf(i+1)); //this is the file where I'll save the jpg file
          var nume = img.name.replace(".jpg", ''); //this is the name of the image I just opened. I remove the jpg extension so only the name is left
          app.preferences.rulerUnits = startRulerUnits; //I change the units back t their original value as for this phase I need to use CM
          app.preferences.typeUnits = startTypeUnits;
          img.resizeImage (3, 4, 300); //resize the image to 3x4 cm at 300 dpi
          app.preferences.rulerUnits = Units.PIXELS; //change the units to pixels again
          app.preferences.typeUnits = TypeUnits.PIXELS;
          img.backgroundLayer.copy(); //copy the background (and only) layer of the image
          img.close(SaveOptions.DONOTSAVECHANGES); //close the document without saving changes
          app.activeDocument = mainDoc; //focus back on the badge template
          var pLayer = mainDoc.paste(); //paste the layer I just copied into the template
          pLayer.translate (-314, 33); //place the new layer where I need it to be
          mainDoc.layers.getByName("Name").textItem.contents = nume; //change the name on the badge to the name of the original file
          mainDoc.saveAs (saveFile, so, true, Extension.LOWERCASE); //this is ehere I have issues...
          app.preferences.rulerUnits = startRulerUnits; //change back to centimeters
          app.preferences.typeUnits = startTypeUnits;


      I guess it can only be a problem with the saveFile agument, as I can save the jpg manually from photoshop.

      PS: using CS6



        • 1. Re: How to "save as" a document using scripts
          Chuck Uebele Adobe Community Professional & MVP

          I think you're issue is line 17. I'm not sure what you're doing with: String.valueOf(i+1). Are you just trying to name this files with just a number? If that's the case, just put: i+1. You are also using the wrong syntax for your path. You also don't have the jpg extension. The line should read:


          var saveFile = new File("/c/Users/Eugen/Desktop/salvate/" + (i+1) + ".jpg"); 
          • 2. Re: How to "save as" a document using scripts
            dadus33 Level 1

            Well, thanks for the answer, but it's not totally right. My problem was, indeed related to the part with String.valueOf(); I did that cause I'm ot familliar with JS, which is a rather NOT type specific language. If I remember right, if you were to do this in C++ it wouldn't work. But nothing was wrong with the path actually, nor with not adding the .jpg extension. Photoshop took care of that. But thanks for solving the issue!