11 Replies Latest reply: Jan 13, 2011 8:52 AM by bfox2718 RSS

    Need help Saving file via script with a different file name

    bfox2718

      Hi... new to scripting here, but I think this can be done... just not sure how.

       

      I have to run an action on 1000s of images,and then save each file with a slightly modified filename.

       

      • original filename:  12345_1.jpg
      • I need to save it as 12345_3.jpg

       

       

      Basically.... from what you see above, I need to change the last digit from a 1 to a 3.

       

      I will also be doing this for changing a 2 to a 4

       

      • original filename:  12345_2.jpg
      • I need to save it as 12345_4.jpg

       

       

      Any help would be greatly appreciated.

        • 1. Re: Need help Saving file via script with a different file name
          c.pfaffenbichler Community Member

          You could give this a try:

          // saves jpg into same folder and changes last number 1 to 3 and 2 to 4;
          // be advised: this  overwrites existing jpgs of the same name without prompting. 
          // 2011, use it at your own risk;
          #target photoshop;
          if (app.documents.length > 0) {
          var thedoc = app.activeDocument;
          // getting the name and location;
          var docName = thedoc.name;
          if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
          else {var basename = docName};
          // change number at end of name;
          var lastLetter = basename.slice(basename.length - 1);
          switch (lastLetter) {
          case "1":
          basename = basename.slice(0, basename.length - 1) + "3";
          break;
          case "2":
          basename = basename.slice(0, basename.length - 1) + "4";
          break;
          default:
          break;
          };
          // getting the location, if unsaved save to desktop;
          try {var docPath = thedoc.path}
          catch (e) {var docPath = "~/Desktop"};
          // jpg options;
          var jpegOptions = new JPEGSaveOptions();
          jpegOptions.quality = 9;
          jpegOptions.embedColorProfile = true;
          jpegOptions.matte = MatteType.NONE;
          //save jpg as a copy:
          thedoc.saveAs((new File(docPath+'/'+basename+'.jpg')),jpegOptions,true);
          //that’s it; thanks to xbytor;
          };
          
          • 2. Re: Need help Saving file via script with a different file name
            Paul Riggott Community Member

            This should do what you want, make sure you edit the doAction to suit!

             

            #target Photoshop
            main();
            function main(){
            var inputFolder= Folder.selectDialog ("Please select folder to process");
            if(inputFolder == null) return;
            var outputFolder= Folder.selectDialog ("Please select output folder");
            var fileList = inputFolder.getFiles("*.jpg");
            for (var a in fileList){
            var f = fileList[a];
            open(f);
            var Name = decodeURI(f.name).replace(/\.[^\.]+$/, '');
            var Ext = decodeURI(f.name).replace(/^.*\./,''); 
            var prefix = Name.match(/^\d+/);
            var suffix = Number(Name.match(/\d+$/))+1;
            var newName = prefix+"_"+suffix+"."+Ext;
            var saveFile = File(outputFolder+"/"+newName);
            ///////////////////////////////////////////////////////////////////////////////////
            //Amend to suit, this is case sensitive!
            doAction("ActionName","ActionSet");
            //////////////////////////////////////////////////////////////////////////
            SaveJPEG(saveFile, 9);
            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                }
            }
            function SaveJPEG(saveFile, jpegQuality){
            jpgSaveOptions = new JPEGSaveOptions();
            jpgSaveOptions.embedColorProfile = true;
            jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
            jpgSaveOptions.matte = MatteType.NONE;
            jpgSaveOptions.quality = jpegQuality; 
            activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
            }
            
            • 3. Re: Need help Saving file via script with a different file name
              bfox2718 Community Member

              Paul-

               

              What you showed me below has been very helpful...

               

              I ultimately want to run a different action on *_1.jpg files and *_2.jpg files.  Then save them as *_3.jpg and *_4.jpg respectively.

               

              I am attempting to modify your script to do this, and I think I am getting something wrong... can you review this, and see what I am missing please?

               

              It gets through the _1 poses fine... then I get a failure starting the _2 poses.

              Untitled-1.jpg

               

               

              #target Photoshop
              main();
              function main(){
              var inputFolder= Folder.selectDialog ("Please select folder to process","J:\DRUMSFILES");
              if(inputFolder == null) return;
              var outputFolder= Folder.selectDialog ("Please select output folder","J:\DRUMSFILES");
              var fileListPose1 = inputFolder.getFiles("*_1.jpg");
              for (var a in fileListPose1){
              var f1 = fileListPose1[a];
              open(f1);
              var Name = decodeURI(f1.name).replace(/\.[^\.]+$/, '');
              var Ext = decodeURI(f1.name).replace(/^.*\./,'');
              var prefix = Name.match(/^\d+/);
              var suffix = Number(Name.match(/\d+$/))+2;
              var newName = prefix+"_"+suffix+"."+Ext;
              var saveFile = File(outputFolder+"/"+newName);
              ///////////////////////////////////////////////////////////////////////////////////
              //Amend to suit, this is case sensitive!
              doAction("FINAL4","FINAL_4");
              //////////////////////////////////////////////////////////////////////////
              SaveJPEG(saveFile, 9);
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                  }
              }
              function SaveJPEG(saveFile, jpegQuality){
              jpgSaveOptions = new JPEGSaveOptions();
              jpgSaveOptions.embedColorProfile = true;
              jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
              jpgSaveOptions.matte = MatteType.NONE;
              jpgSaveOptions.quality = jpegQuality;
              activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
              }

               

              var fileListPose2 = inputFolder.getFiles("*_2.jpg");
              for (var a in fileListPose2){
              var f2 = fileListPose2[a];
              open(f2);
              var Name = decodeURI(f2.name).replace(/\.[^\.]+$/, '');
              var Ext = decodeURI(f2.name).replace(/^.*\./,'');
              var prefix = Name.match(/^\d+/);
              var suffix = Number(Name.match(/\d+$/))+2;
              var newName = prefix+"_"+suffix+"."+Ext;
              var saveFile = File(outputFolder+"/"+newName);
              ///////////////////////////////////////////////////////////////////////////////////
              //Amend to suit, this is case sensitive!
              doAction("Rough Pencil Shading","Spring 2011");
              //////////////////////////////////////////////////////////////////////////
              SaveJPEG(saveFile, 9);
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                  }

               

              function SaveJPEG(saveFile, jpegQuality){
              jpgSaveOptions = new JPEGSaveOptions();
              jpgSaveOptions.embedColorProfile = true;
              jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
              jpgSaveOptions.matte = MatteType.NONE;
              jpgSaveOptions.quality = jpegQuality;
              activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
              }

              • 4. Re: Need help Saving file via script with a different file name
                Paul Riggott Community Member

                All you need to do is change :

                var suffix = Number(Name.match(/\d+$/))+1;

                to

                var suffix = Number(Name.match(/\d+$/))+2;

                 

                That will then increment the last numbers by 2.

                Hope this helps.

                • 5. Re: Need help Saving file via script with a different file name
                  bfox2718 Community Member

                  Thank you

                   

                  I have the saving of the files working correctly.   Its differentiating which action to run where i am struggling.

                   

                  I need to run TWO DIFFERENT Actions on files within the same directory based on the file name

                   

                  for example:

                   

                  • I have have 1000 customers, each with 2 images.
                  • each file is named the customer number and pose number  ( XXXXX_1   and XXXX_2)
                  • I need to run "Action_A" on _1 files and save it as _3    (which is already working in yuour script which I modified).
                  • I need to run "Action_B" on _2 files and save it as _4    this is where I am struggling.

                   

                  Can you review my script and see if you can help that?

                  • 6. Re: Need help Saving file via script with a different file name
                    Paul Riggott Community Member

                    Please try this now...

                    #target Photoshop
                    main();
                    function main(){
                    var inputFolder= Folder.selectDialog ("Please select folder to process");
                    if(inputFolder == null) return;
                    var outputFolder= Folder.selectDialog ("Please select output folder");
                    var fileList = inputFolder.getFiles("*.jpg");
                    for (var a in fileList){
                    var f = fileList[a];
                    open(f);
                    var Name = decodeURI(f.name).replace(/\.[^\.]+$/, '');
                    var Ext = decodeURI(f.name).replace(/^.*\./,''); 
                    var prefix = Name.match(/^\d+/);
                    var suffix = Number(Name.match(/\d+$/));
                    ///////////////////////////////////////////////////////////////////////////////////
                    //Amend to suit, this is case sensitive!
                    if(suffix == 1 ) doAction("FINAL4","FINAL_4");
                    if(suffix == 2 ) doAction("Rough Pencil Shading","Spring 2011");
                    suffix += 2;
                    //////////////////////////////////////////////////////////////////////////
                    var newName = prefix+"_"+suffix+"."+Ext;
                    var saveFile = File(outputFolder+"/"+newName);
                    SaveJPEG(saveFile, 9);
                    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                        }
                    }
                    function SaveJPEG(saveFile, jpegQuality){
                    jpgSaveOptions = new JPEGSaveOptions();
                    jpgSaveOptions.embedColorProfile = true;
                    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                    jpgSaveOptions.matte = MatteType.NONE;
                    jpgSaveOptions.quality = jpegQuality; 
                    activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
                    }
                    
                    • 7. Re: Need help Saving file via script with a different file name
                      Michael L Hale Community Member

                      The reason you are getting that error is you defined inputFolder inside the function main(). When you define a var inside a function it can only be accesed by the code in that function. You chould add the code for the second action call inside main() Like this

                      function main(){
                      var inputFolder= Folder.selectDialog ("Please select folder to process","J:\DRUMSFILES");
                      if(inputFolder == null) return;
                      var outputFolder= Folder.selectDialog ("Please select output folder","J:\DRUMSFILES");
                      var fileListPose1 = inputFolder.getFiles("*_1.jpg");
                      for (var a in fileListPose1){
                      var f1 = fileListPose1[a];
                      open(f1);
                      var Name = decodeURI(f1.name).replace(/\.[^\.]+$/, '');
                      var Ext = decodeURI(f1.name).replace(/^.*\./,'');
                      var prefix = Name.match(/^\d+/);
                      var suffix = Number(Name.match(/\d+$/))+2;
                      var newName = prefix+"_"+suffix+"."+Ext;
                      var saveFile = File(outputFolder+"/"+newName);
                      ////////////////////////////////////////////////////////////////////// /////////////
                      //Amend to suit, this is case sensitive!
                      doAction("FINAL4","FINAL_4");
                      ////////////////////////////////////////////////////////////////////// ////
                      SaveJPEG(saveFile, 9);
                      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                          }
                      var fileListPose2 = inputFolder.getFiles("*_2.jpg");
                      for (var a in fileListPose2){
                      var f2 = fileListPose2[a];
                      open(f2);
                      var Name = decodeURI(f2.name).replace(/\.[^\.]+$/, '');
                      var Ext = decodeURI(f2.name).replace(/^.*\./,'');
                      var prefix = Name.match(/^\d+/);
                      var suffix = Number(Name.match(/\d+$/))+2;
                      var newName = prefix+"_"+suffix+"."+Ext;
                      var saveFile = File(outputFolder+"/"+newName);
                      ////////////////////////////////////////////////////////////////////// /////////////
                      //Amend to suit, this is case sensitive!
                      doAction("Rough Pencil Shading","Spring 2011");
                      ////////////////////////////////////////////////////////////////////// ////
                      SaveJPEG(saveFile, 9);
                      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                          }
                      }
                      

                      Or define inputFolder outside the function main() so the other code can access.

                       

                      Or handle both actions in one loop as Paul as done.

                      • 8. Re: Need help Saving file via script with a different file name
                        bfox2718 Community Member

                        Great... that works perfectly!  I modified it slightly to fit my needs.  see below:

                         

                        #target Photoshop
                        Selective_Action_byPose();

                        function Selective_Action_byPose(){
                            var inputFolder= Folder.selectDialog ("Please select folder to process");
                            if(inputFolder == null) return;
                            //var outputFolder= Folder.selectDialog ("Please select output folder");
                            var outputFolder= inputFolder;

                            var fileList = inputFolder.getFiles("*.jpg");
                            for (var a in fileList) {
                                var f = fileList[a];
                                open(f);
                                var Name = decodeURI(f.name).replace(/\.[^\.]+$/, '');
                                var Ext = decodeURI(f.name).replace(/^.*\./,'');
                                var prefix = Name.match(/^\d+/);
                                var suffix = Number(Name.match(/\d+$/));

                                ///////////////////////////////////////////////////////////////////////////////////
                                //Amend to suit, this is case sensitive!
                                if(suffix == 1 ) doAction("FINAL4","FINAL_4");
                                if(suffix == 2 ) doAction("Rough Pencil Shading","Spring 2011");
                                suffix += 2;
                                //////////////////////////////////////////////////////////////////////////

                                var newName = prefix+"_"+suffix+"."+Ext;
                                var saveFile = File(outputFolder+"/"+newName);
                                SaveJPEG(saveFile, 9);
                                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
                            }
                        }

                        function SaveJPEG(saveFile, jpegQuality) {
                            jpgSaveOptions = new JPEGSaveOptions();
                            jpgSaveOptions.embedColorProfile = true;
                            jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                            jpgSaveOptions.matte = MatteType.NONE;
                            jpgSaveOptions.quality = jpegQuality;
                            activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
                        }

                        • 9. Re: Need help Saving file via script with a different file name
                          bfox2718 Community Member

                          So, Ive got the script running on a test job now, but i want to add a Message Box when it is all completed.

                           

                          I want to the message box to display the number of files processed and the total amount of time the script took to run.

                           

                          Any ideas on what I need to modify to make it work?

                           

                          I am suspecting this will work, but I am not sure where to place it or how to calculate the difference between the two and keep the file count.

                           

                          Use the Date object in JavaScript.

                           

                          At the start of the script place this line:

                          var startTime = new Date().getTime();

                           

                          And at the end place this one:

                          var endTime = new Date().getTime();

                           

                          The difference between endTime and startTime is the time (in ms) it took your script to run.

                           

                          Ultimately I would life for a message box to show the following:

                           

                          1000 Images have completed processing.

                          It took 5 hours and 32 minutes.

                           

                           

                          Below is the current script I am using.

                           

                          #target Photoshop
                          Selective_Action_byPose();

                          function Selective_Action_byPose(){
                              var inputFolder= Folder.selectDialog ("Please select folder to process.  New Poses will also be saved in this directory.","J:\DRUMSFILES");
                              if(inputFolder == null) return;
                              //var outputFolder= Folder.selectDialog ("Please select output folder");
                              var outputFolder= inputFolder;
                              var startTime = new Date().getTime();

                              var fileList = inputFolder.getFiles("*.jpg");
                              for (var a in fileList) {
                                  var f = fileList[a];
                                  open(f);
                                  var Name = decodeURI(f.name).replace(/\.[^\.]+$/, '');
                                  var Ext = decodeURI(f.name).replace(/^.*\./,'');
                                  var prefix = Name.match(/^\d+/);
                                  var suffix = Number(Name.match(/\d+$/));

                                  ///////////////////////////////////////////////////////////////////////////////////
                                  //Amend to suit, this is case sensitive!
                                  if(suffix == 1 ) doAction("FINAL4","FINAL_4");
                                  if(suffix == 2 ) doAction("Rough Pencil Shading","Spring 2011");
                                  suffix += 2;
                                  //////////////////////////////////////////////////////////////////////////

                                  var newName = prefix+"_"+suffix+"."+Ext;
                                  var saveFile = File(outputFolder+"/"+newName);
                                  SaveJPEG(saveFile, 9);
                                  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);




                              }
                          }

                          function SaveJPEG(saveFile, jpegQuality) {
                              jpgSaveOptions = new JPEGSaveOptions();
                              jpgSaveOptions.embedColorProfile = true;
                              jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
                              jpgSaveOptions.matte = MatteType.NONE;
                              jpgSaveOptions.quality = jpegQuality;
                              activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);

                          • 10. Re: Need help Saving file via script with a different file name
                            Muppet Mark-QAl63s Community Member

                            There are most likely much neater math solutions out there for doing this… but I think this should work with your millisecond time gap.

                             

                            var t = 185000; // ms
                            
                            var h = Math.floor(t/3600000000);
                            var r = t % 3600000000
                            
                            var m = Math.floor(r/60000);
                            var r = r % 60000;
                            
                            var s = Math.floor(r/1000);
                            
                            alert('Hours: '+h+' Minutes: '+m+' Seconds: '+s);
                            

                             

                            As for the number of files don't you just want…

                             

                            alert(fileList.length);
                            

                            • 11. Re: Need help Saving file via script with a different file name
                              bfox2718 Community Member

                              Thank you..  That was a big help as well.  I have my script "finalized" and ready for production in our lab!