13 Replies Latest reply: Apr 14, 2014 6:29 AM by csuebele RSS

    combine "Save As" with suffix for file's name

    saigon80 Community Member

      I have 600x600px file named "ABC". I want to save this file in three versions within 1 folder which different to original folder:

      ver1   600x600px   name  ABC_1  in JPEG

      ver2   300x300px   name  ABC_2  in PSD

      ver 3  300x300px   name ABC_3   in JPEG.

       

      I can do this with Save As command but cannot add suffix.  I can add suffix with a script but I dont know how to create a UI interface to choose file type and file address like we do with Save As . Thanks in advanced for your script's  help .

        • 1. Re: combine "Save As" with suffix for file's name
          csuebele CommunityMVP

          Do you want to just select a folder with the UI to put all the files in or do you want to have the save as dialog box come up each time you save a file or both?  To have the saveas dialog box appear, you just do something like this:

           

          var docRef = activeDocument;

          var jpgOptions = new JPEGSaveOptions();

          jpgOptions.quality = 8

           

          app.displayDialogs = DialogModes.ALL;//turns on dialog box

          docRef.saveAs (new File('filepath_and_name'), jpgOptions)

          app.displayDialogs = DialogModes.NO;//turns off dialog box

          • 2. Re: combine "Save As" with suffix for file's name
            saigon80 Community Member

            thanks csuebele , but I can not automatically add suffix for file name with that script.

            • 3. Re: combine "Save As" with suffix for file's name
              csuebele CommunityMVP

              You just have to add lines to add the suffix.  I'm only use Win, so here's what I would do for that.  I would get the file name:

               

              var fileName = docRef.name.split('.')[0];

               

              Then I would use that in saving the files:

              docRef.saveAs (new File('/c/images/' + fileName + '_1.jpg'), jpgOptions);

               

              I would just save the image three times with the sizes and file types you want.

              • 4. Re: combine "Save As" with suffix for file's name
                saigon80 Community Member

                May I have a full script for my case above. I dont know much about script so ...please help ! Thank you !

                • 5. Re: combine "Save As" with suffix for file's name
                  csuebele CommunityMVP

                  Try this.  You will want to add whatever path to the line that defines "saveFolder" so that you don't have to keep inputting it.  Same with the file.  put in a starting path.  Also, I din't want to spend all the time putting in error catching features, so if you cancel selecting a file or folder, it will throw an error - maybe some others I don't know about too.

                   

                  #target photoshop

                   

                  app.preferences.rulerUnits = Units.PIXELS;

                   

                  var file = new File();

                  var saveFolder = new Folder('/c/');

                  var docRef

                  var jpgOptions = new JPEGSaveOptions();

                  jpgOptions.quality = 10

                  var psdOptions = new PhotoshopSaveOptions()

                  psdOptions.layers = true

                   

                  var dlg = new Window('dialog','Process Images');

                  dlg.fileGp = dlg.add('group');

                  dlg.fileGp.sTxt = dlg.fileGp.add('statictext',undefined,'Target File');

                  dlg.fileGp.btn = dlg.fileGp.add('button',undefined,file.fsName);

                      dlg.fileGp.btn.size = [500,20];

                      dlg.fileGp.btn.onClick = function(){

                          file = file.openDlg ('Select a file');

                      dlg.fileGp.btn.text = file.fsName;}

                  dlg.folderGp = dlg.add('group');

                  dlg.folderGp.sTxt = dlg.folderGp.add('statictext',undefined,'Save Folder');

                  dlg.folderGp.btn = dlg.folderGp.add('button',undefined,saveFolder.fsName);

                      dlg.folderGp.btn.size = [500,20];

                      dlg.folderGp.btn.onClick = function(){

                          saveFolder = Folder.selectDialog ('Select a save folder')

                      dlg.folderGp.btn.text = saveFolder.fsName   };

                  dlg.btnGp = dlg.add('group');

                  dlg.btnGp.okay = dlg.btnGp.add('button',undefined,'Okay');

                  dlg.btnGp.cancel = dlg.btnGp.add('button',undefined,'Cancel');

                   

                  dlg.btnGp.okay.onClick = function(){

                      if(!file.exists){alert('The file selected does not exist.')}

                      if(!saveFolder.exists){alert('The save folder selected does not exist.')}

                      if(file.exists && saveFolder.exists){

                          dlg.close()

                          runProg()}

                      }

                   

                  dlg.btnGp.onClick = function(){

                      dlg.close()};

                   

                  dlg.show();

                   

                  function runProg (){

                      docRef = open(file)

                      var fileName = docRef.name.split('.')[0];

                      var docSize = Math.max(docRef.width,docRef.height)

                      if(docRef.width>docRef.height){docRef.resizeImage(600, undefined, undefined)}

                      else{docRef.resizeImage(undefined, 600, undefined)}

                      docRef.saveAs (new File(saveFolder+'/' + fileName + '_1.jpg'), jpgOptions);

                   

                      if(docRef.width>docRef.height){docRef.resizeImage(300, undefined, undefined)}

                      else{docRef.resizeImage(undefined, 300, undefined)}

                      docRef.saveAs (new File(saveFolder+'/' + fileName + '_2.psd'), psdOptions);

                      docRef.saveAs (new File(saveFolder+'/' + fileName + '_3.jpg'), jpgOptions);

                      }

                  • 6. Re: combine "Save As" with suffix for file's name
                    saigon80 Community Member

                    So many Thanks to Csuebele ! It works very well if openning file is .jpg  . Others fomat (.png  .psd) do not show the result we want.

                    • 7. Re: combine "Save As" with suffix for file's name
                      csuebele CommunityMVP

                      What is happening with .png and .psd?

                      • 8. Re: combine "Save As" with suffix for file's name
                        saigon80 Community Member

                        after loading file , Save As window appears  so I have to change name of three version manually . If the input file is JPG , the script running without stop and the output files' name  is correct

                        • 9. Re: combine "Save As" with suffix for file's name
                          csuebele CommunityMVP

                          Add this line after #target photoshop:

                          app.displayDialogs = DialogModes.NO;

                          • 10. Re: combine "Save As" with suffix for file's name
                            saigon80 Community Member

                            I want to change one thing in the script : i dont want to choose file , i just want to choose the address for the outputs of the openning file. With your current scipts , I have to browse file, one after one. How can I modify the script with only address output button. Thankyou !

                            • 11. Re: combine "Save As" with suffix for file's name
                              csuebele CommunityMVP

                              Try this:

                               

                              #target photoshop  

                               

                              app.preferences.rulerUnits = Units.PIXELS;    

                               

                              var targetFolder = new Folder('/c/');

                              var saveFolder = new Folder('/c/');

                              var docRef;

                              var jpgOptions = new JPEGSaveOptions();

                              jpgOptions.quality = 10;

                              var psdOptions = new PhotoshopSaveOptions();

                              psdOptions.layers = true;    

                               

                              var winFiles = app.windowsFileTypes;

                              var macFiles = app.macintoshFileTypes;

                               

                              var dlg = new Window('dialog','Process Images');

                                  dlg.targetGp = dlg.add('group');

                                  dlg.targetGp.sTxt = dlg.targetGp.add('statictext',undefined,'Target Folder');

                                  dlg.targetGp.btn = dlg.targetGp.add('button',undefined,targetFolder.fsName);

                                      dlg.targetGp.btn.size = [500,20];

                               

                                      dlg.targetGp.btn.onClick = function(){

                                          targetFolder = Folder.selectDialog ('Select a target folder');

                                          dlg.targetGp.btn.text = targetFolder.fsName;}

                               

                                  dlg.folderGp = dlg.add('group');

                                  dlg.folderGp.sTxt = dlg.folderGp.add('statictext',undefined,'Save Folder');

                                  dlg.folderGp.btn = dlg.folderGp.add('button',undefined,saveFolder.fsName);

                                      dlg.folderGp.btn.size = [500,20];

                               

                                      dlg.folderGp.btn.onClick = function(){

                                          saveFolder = Folder.selectDialog ('Select a save folder')

                                          dlg.folderGp.btn.text = saveFolder.fsName};

                               

                                  dlg.btnGp = dlg.add('group');

                                  dlg.btnGp.okay = dlg.btnGp.add('button',undefined,'Okay');

                                  dlg.btnGp.cancel = dlg.btnGp.add('button',undefined,'Cancel');    

                               

                                  dlg.btnGp.okay.onClick = function(){

                                      if(!targetFolder.exists){alert('The target folder selected does not exist.')}

                                      if(!saveFolder.exists){alert('The save folder selected does not exist.')}

                                      if(targetFolder.exists && saveFolder.exists){

                                          dlg.close();

                                          runProg()}

                                      };    

                               

                                  dlg.btnGp.cancel.onClick = function(){

                                      dlg.close()};    

                               

                                  dlg.show();

                               

                               

                               

                              function runProg (){

                                  var fileList = targetFolder.getFiles ()

                                  var filesToUse = new Array()

                               

                                  for (var i=0;i<fileList.length;i++){

                                      if(IsFileOneOfThese( fileList[i], winFiles )){filesToUse.push(fileList[i])}

                                      else if(IsFileOneOfTheseTypes( fileList[i], macFiles )){filesToUse.push(fileList[i])}

                                      };//end loop 1

                               

                                  for (var i=0;i<filesToUse.length;i++){   

                                      try{

                                           docRef = open(filesToUse[i]);

                                           var fileName = docRef.name.split('.')[0];

                                           var docSize = Math.max(docRef.width,docRef.height);

                               

                                           if(docRef.width>docRef.height){docRef.resizeImage(600, undefined, undefined)}

                                           else{docRef.resizeImage(undefined, 600, undefined)};

                               

                                           docRef.saveAs (new File(saveFolder+'/' + fileName + '_1.jpg'), jpgOptions);

                               

                                           if(docRef.width>docRef.height){docRef.resizeImage(300, undefined, undefined)}

                                           else{docRef.resizeImage(undefined, 300, undefined)};

                               

                                           docRef.saveAs (new File(saveFolder+'/' + fileName + '_2.psd'), psdOptions);

                                           docRef.saveAs (new File(saveFolder+'/' + fileName + '_3.jpg'), jpgOptions);

                                           docRef.close(SaveOptions.DONOTSAVECHANGES);

                                        }

                                        catch(e){}

                                      };//end loop 2

                               

                                  };

                               

                              function IsFileOneOfThese( inFileName, inArrayOfFileExtensions ) {

                                  var lastDot = inFileName.toString().lastIndexOf( "." );

                                  if ( lastDot == -1 ) {

                                      return false;

                                  }

                                  var strLength = inFileName.toString().length;

                                  var extension = inFileName.toString().substr( lastDot + 1, strLength - lastDot );

                                  extension = extension.toUpperCase();

                                  for (var i = 0; i < inArrayOfFileExtensions.length; i++ ) {

                                      if ( extension == inArrayOfFileExtensions[i] ) {

                                          return true;

                                      }

                                  }

                                  return false;

                              };

                               

                              // given a file name and a list of types

                              // determine if this file is one of the provided types. Always returns false on platforms other than Mac.

                              function IsFileOneOfTheseTypes( inFileName, inArrayOfFileTypes ) {

                                  if ( ! IsMacintoshOS() ) {

                                      return false;

                                      }

                                  var file = new File (inFileName);

                                  for (var i = 0; i < inArrayOfFileTypes.length; i++ ) {

                                      if ( file.type == inArrayOfFileTypes[i] ) {

                                          return true;

                                      }

                                  }

                                  return false;

                              };

                               

                              function IsWindowsOS() {

                                  if ( $.os.search(/windows/i) != -1 ) {

                                      return true;

                                  } else {

                                      return false;

                                  }

                              }

                               

                              function IsMacintoshOS() {

                                  if ( $.os.search(/macintosh/i) != -1 ) {

                                      return true;

                                  } else {

                                      return false;

                                  }

                              }

                              • 12. Re: combine "Save As" with suffix for file's name
                                saigon80 Community Member

                                you're amazing. your scipts is far above my knowlwedge. In fact, i dont run the script like the way you write the scripts. OK I make it clear one more time and I really need your help : I have 100 files need to retouch and resize, for example . Each file I do retouching manually so my files often are .psd or .tiff with some layers. After finish retouching work  , I crop tight, resize, add canvas(margin) and save  ver1   600x600px   name  ABC_1  in JPEG.  Then I make some new canvas (margin) and save ver2   300x300px   name  ABC_2  in PSD, then I change the canvas one more time (request by customer) and save ver 3  300x300px   name ABC_3   in JPEG. I often do this "size and margin" work by using action. But with action , I cannot add suffix automatically. So I want a scripts that can be used within my action ,allow me to choose output address and add suffix "_1" , then the action continue do canvas size , after that the action continue load the scripts and save version 2 .... . I dont want scripts for the margin because each customer has their own request so I use action for this task (I can control it easier than scripts). I dont run action for batch , I run action for each openning file - after retouching work.

                                My English is not good so I hope you can understand my explanation .  Thank you so much ! 

                                • 13. Re: combine "Save As" with suffix for file's name
                                  csuebele CommunityMVP

                                  Hopefully I understand now.  You want to do editing between saving each version.  If so here's what you can do.  You will need to create three scripts which you can then run in your action.  They are all based on the same script, but will have some slight modifications.  The script I'm posting below still has the user interface dialog box, but I think you would want to delete that part (the section between the "//////////////////" lines and just edit line 6 by putting in the exact path you want to use to save your images.

                                   

                                  Here's the first script and the base for the others:

                                   

                                  #target photoshop

                                  app.displayDialogs = DialogModes.NO;

                                  app.preferences.rulerUnits = Units.PIXELS;

                                  var file = new File();

                                  var saveFolder = new Folder('/c/');//Change this to where you want to save.

                                  var docRef;

                                  var jpgOptions = new JPEGSaveOptions();

                                  jpgOptions.quality = 10;

                                  var psdOptions = new PhotoshopSaveOptions();

                                  psdOptions.layers = true;

                                  ///////////////////////////////////////////////////////

                                  var dlg = new Window('dialog','Process Images');

                                  dlg.folderGp = dlg.add('group');

                                  dlg.folderGp.sTxt = dlg.folderGp.add('statictext',undefined,'Save Folder');

                                  dlg.folderGp.btn = dlg.folderGp.add('button',undefined,saveFolder.fsName);

                                  dlg.folderGp.btn.size = [500,20];

                                  dlg.folderGp.btn.onClick = function(){

                                  saveFolder = Folder.selectDialog ('Select a save folder')

                                  dlg.folderGp.btn.text = saveFolder.fsName   };

                                  dlg.btnGp = dlg.add('group');

                                  dlg.btnGp.okay = dlg.btnGp.add('button',undefined,'Okay');

                                  dlg.btnGp.cancel = dlg.btnGp.add('button',undefined,'Cancel');

                                  dlg.btnGp.okay.onClick = function(){

                                  if(!saveFolder.exists){alert('The save folder selected does not exist.')}

                                  else{

                                  dlg.close()

                                  runProg()}

                                  }

                                  dlg.btnGp.cancel.onClick = function(){

                                  dlg.close()};

                                  dlg.show();

                                  //////////////////////////////////////////////////////////////

                                  f

                                  unction runProg (){

                                  docRef = activeDocument

                                  var fileName = docRef.name.split('.')[0];

                                  var docSize = Math.max(docRef.width,docRef.height)

                                  if(docRef.width>docRef.height){docRef.resizeImage(600, undefined, undefined)}

                                  else{docRef.resizeImage(undefined, 600, undefined)}

                                  docRef.saveAs (new File(saveFolder+'/' + fileName + '_1.jpg'), jpgOptions);    

                                  /*

                                  if(docRef.width>docRef.height){docRef.resizeImage(300, undefined, undefined)}

                                  else{docRef.resizeImage(undefined, 300, undefined)}

                                   

                                  docRef.saveAs (new File(saveFolder+'/' + fileName + '_2.psd'), psdOptions);

                                  docRef.saveAs (new File(saveFolder+'/' + fileName + '_3.jpg'), jpgOptions);

                                  */       

                                  }

                                   

                                  for the second script change the end of the first script to this:

                                   

                                  function runProg (){

                                  docRef = activeDocument

                                  var fileName = docRef.name.split('.')[0];

                                  var docSize = Math.max(docRef.width,docRef.height)

                                  if(docRef.width>docRef.height){docRef.resizeImage(300, undefined, undefined)}

                                  else{docRef.resizeImage(undefined, 300, undefined)}

                                  docRef.saveAs (new File(saveFolder+'/' + fileName + '_2.psd'), psdOptions);

                                  }

                                   

                                  For the last script change the end to this:

                                   

                                  function runProg (){

                                  docRef = activeDocument

                                  var fileName = docRef.name.split('.')[0];

                                  var docSize = Math.max(docRef.width,docRef.height)

                                  if(docRef.width>docRef.height){docRef.resizeImage(300, undefined, undefined)}

                                  else{docRef.resizeImage(undefined, 300, undefined)}

                                  docRef.saveAs (new File(saveFolder+'/' + fileName + '_3.jpg'), jpgOptions);

                                  }