10 Replies Latest reply on Feb 3, 2018 1:31 PM by tokuredit

    Help with script that saves with the name of the active layer

    tokuredit Level 1

      Hi!

      I found this wonderful Script in this community:

      What he does!

      It saves properly named files with the name of the active layer inside a subfolder "Rolad Prints";

       

      The script works this way:

      When I have a document and an active layer called "My Image" when executing the script, it saves the document called "My Image.tiff" inside the subfolder "Rolad Prints"

      My goal is:

      1st - Add a suffix to the file whenever I execute the script one or more times:

      Example:

      layer tiva_00. tiff, (Once run)

      layer tiva_01. tiff, (twice run)

      layer tiva_02. tiff ... (three times run)

      on the same document.

      Screenshot_1.png

       

      2º - Add a function that allows to open a "explore window" displaying all the files in the subfolder:

      I tried this but it is on the main path you inves from the subfolder "Rolad Prints"

      docPath.execute ();
      

       

      I also tried:Presented an error.

      subfolder.execute (); //subfolder "Rolad Prints"
      

       

      Is there any intelligent friend capable of making these modifications for me?

      Thank you!

       

      Script:

      var subfolder = "Rolad Prints";  
      var tif_layers = true; // change to false if you want flatten image  
      
      
      // Backup_Estampa  
      (function() {    
          try {    
              if (app.documents.length > 0) {    
                  var thedoc = app.activeDocument;    
                     
                  // getting the name and location;       
                  var docName = thedoc.name;    
                  var basename = docName;    
                  if (docName.indexOf(".") != -1)    
                      basename = docName.match(/(.*)\.[^\.]+$/)[1]    
        
               
               // getting the location, if unsaved save to desktop;    
                  var docPath = "~/Desktop";    
                  try {    
                      docPath = thedoc.path;    
                  } catch (e) {}    
        
      
      
              
              // tif options;         
                  var tiffOptions = new TiffSaveOptions();      
                  with (tiffOptions)  
                      {  
                      alphaChannels      = true;  
                      annotations        = true;  
                      byteOrder          = ByteOrder.IBM;  
                      embedColorProfile  = true;  
                      imageCompression   = TIFFEncoding.NONE;  
                      interleaveChannels = true;  
                      jpegQuality        = 12;  
                      layerCompression   = LayerCompression.ZIP;  
                      layers             = tif_layers;  
                      saveImagePyramid   = false;  
                      spotColors         = true;  
                      transparency       = false;  
                      } 
           
                  //save tif as a copy:      
                  if (!Folder(docPath + "/" + subfolder).exists) Folder(docPath + "/" + subfolder).create();  
                  var saveFile = docPath + '/' + subfolder + '/' + thedoc.activeLayer.name +'.tif';      
                  if (File(saveFile).exists)      
                      saveFile = incrementFile(saveFile);      
                       
                  thedoc.saveAs(new File(saveFile), tiffOptions, true); 
                  docPath.execute(); 
          
          //that’s it; thanks to xbytor;       
              };    
          
              function incrementFile(pathToFile) {    
                  var saveSufixLength = 2,    
                      suffix = zeroPad(0, saveSufixLength),    
                      newPathToFile = pathToFile,    
                      suffixInteger, folder, fileName, extension;    
          
                  while (File(newPathToFile).exists) {    
                      suffixInteger = parseInt(suffix, 10);    
                      suffixInteger += 1;    
                      suffix = zeroPad(suffixInteger, saveSufixLength);    
                      folder = File(pathToFile).path;    
                      fileName = File(pathToFile).name.substring(0, File(pathToFile).name.lastIndexOf("."));    
                      extension = File(pathToFile).name.substring(File(pathToFile).name.lastIndexOf(".") + 1, File(pathToFile).name.length);    
          
                      newPathToFile = folder + "/" + fileName + "_" + suffix + "." + extension;    
                  }    
                  return newPathToFile;    
         
                     function zeroPad(num, digit) {    
                      var tmp = num.toString();    
                      while (tmp.length < digit) {    
                          tmp = "0" + tmp;    
                      }    
                      return tmp;    
                  }    
              }    
          
          } catch (e) {    
              alert(e.toString() + "\nScript File: " + File.decode(e.fileName).replace(/^.*[\|\/]/, '') +    
                  "\nFunction: " + arguments.callee.name +    
                  "\nError on Line: " + e.line.toString())    
          }    
      
      
      
      
      })();             // MODIFICATION 5 !!!!!
      
        • 1. Re: Help with script that saves with the name of the active layer
          Kukurykus Adobe Community Professional
          $.level = 0; try{
               (pth = (aD = activeDocument).path).exists
          
               with(tiff = new TiffSaveOptions()) {
                    alphaChannels = embedColorProfile = interleaveChannels = layers
                    = saveImagePyramid = spotColors = transparency = annotations = true
                    byteOrder = ByteOrder.IBM, imageCompression = TIFFEncoding.NONE
                    jpegQuality = 12, layerCompression = LayerCompression.ZIP
               }
          
               if (!(sub = Folder(pth + '/' +  RP = 'Rolad Prints')).exists &&
               prnt = decodeURI(pth.name) != RP) sub.create(); r = /\d{2}(?=.tif)/i
          
               function fun() {
                    return ((String(n = +(String(iS = (gF = File(pth).getFiles
                    (RegExp(encodeURI(aL + '_.*\.tif'))))[gF.length - 1]))
                    .match(r)).length - 1 || n > 8) && iS ? '' : '0') + (iS ? ++n : '0')
               }
          
               aD.saveAs(File((pth = pth + (prnt ? '/' + RP + '/' : '/')) + (aL = aD
               .activeLayer.name) + '_' + fun() + '.tif'), tiff); File(pth).execute()
          
          }
          catch(err){alert('Script works only with saved documents!')}
          
          1 person found this helpful
          • 2. Re: Help with script that saves with the name of the active layer
            tokuredit Level 1

            It worked perfectly well!

            Wow! Kukurykus, you are phenomenal! I am impressed by your ability to programmer, every day better.

            Thank you!

            • 3. Re: Help with script that saves with the name of the active layer
              geppettol66959005 Level 2

              Kukurykus

              if one wants to save in jpg

              • 4. Re: Help with script that saves with the name of the active layer
                Kukurykus Adobe Community Professional

                Content of with statement replace to JPEGSaveOptions, while tif in regexp to jpe?g and extension in saveAs from tif to jpg.

                • 5. Re: Help with script that saves with the name of the active layer
                  geppettol66959005 Level 2

                  I did as you told me

                  but it does not work

                   

                  where did I go wrong?

                   

                  $.level = 0; try{  
                       (pth = (aD = activeDocument).path).exists  
                    
                       with(tiff = new JPEGSaveOptions()) {  
                            alphaChannels = embedColorProfile = interleaveChannels = layers  
                            = saveImagePyramid = spotColors = transparency = annotations = true  
                            byteOrder = ByteOrder.IBM, imageCompression = TIFFEncoding.NONE  
                            jpegQuality = 12, layerCompression = LayerCompression.ZIP  
                       }  
                    
                       if (!(sub = Folder(pth + '/' +  RP = 'Rolad Prints')).exists &&  
                       prnt = decodeURI(pth.name) != RP) sub.create(); r = /\d{2}(?=.tif)/i  
                    
                       function fun() {  
                            return ((String(n = +(String(iS = (gF = File(pth).getFiles  
                            (RegExp(encodeURI(aL + '_.*\.jpg'))))[gF.length - 1]))  
                            .match(r)).length - 1 || n > 8) && iS ? '' : '0') + (iS ? ++n : '0')  
                       }  
                    
                       aD.saveAs(File((pth = pth + (prnt ? '/' + RP + '/' : '/')) + (aL = aD  
                       .activeLayer.name) + '_' + fun() + '.jpg'), tiff); File(pth).execute()  
                    
                  }  
                  catch(err){alert('Script works only with saved documents!')}  
                  
                  • 6. Re: Help with script that saves with the name of the active layer
                    Kukurykus Adobe Community Professional

                    Original code was naming first file without '00' sufix, while files opened from 'Rolad Prints' folder were saved to another subfolder of same name. Now naming starts from '00' and goes up to 99 (that will be overwtitten with a hundred attpemt). You can save different named active layers to the same destination folder where each new file will get appriopate suffix. No matter you use script on (last or some previous) file from a 'Rolad Prints' folder or that is one level higher in hierarchy it will be always saved to your 'Rolad Prints' folder with suffix one number higher due to prior file with same active layer name.

                     

                    Actually I forgot during tests to change back script to previous version, so that I posted is wrong, change middle of code to:

                     

                    if ((prnt = decodeURI(pth.name) != RP = 'Rolad Prints')
                    && !(sub = Folder(pth + '/' + RP)).exists) sub.create()
                    
                    function fun() {
                         return ((String(n = +(String(iS = (gF = File(pth).getFiles
                         (RegExp(encodeURI(aL + '_.*\.tif'))))[gF.length - 1])).match
                         (/\d{2}(?=.tif)/i)).length - 1 || n > 8) && iS ? '' : '0') + (iS ? ++n : '0')
                    }
                    
                    1 person found this helpful
                    • 7. Re: Help with script that saves with the name of the active layer
                      tokuredit Level 1

                      I had done some testing and it worked perfectly! Today I saw your post I did other tests with other files and actually presented this alert message: Script works only with saved documents!

                       

                      Thankfully you noticed and corrected the problem before you even realized it. Thank you for making mine mines more efficient!

                      • 8. Re: Help with script that saves with the name of the active layer
                        tokuredit Level 1

                        Hello! Kukurykus

                        If I resolve to create a dialog box and add a few options with radio buttons: Save: ".tiff, pdf or jpg" and I would ecolher the option saves the image in tiff .... Certainly the image will be saved in tiff and later the window opens with the saved files, right! File (pth) .execute ();

                         

                        I would like to know if the function File (pth) .execute (); can be executed later, only when I click an "April Folder" button or through checkbox with options: true or false.

                        If your answer is positive, I will create a new discourse addressing this theme: If not I already feel satisfied and I close this discourse here.

                        Thank you.

                        • 9. Re: Help with script that saves with the name of the active layer
                          Kukurykus Adobe Community Professional

                          I tried to do same script for jpg files, but I met trouble. For some reason it deosn't work and can't understand why. Layer name taken to variable and then applied to name of file is missed while what I noticed flattening layers of psd. file during saving. That causes new file is getting flattened (background) layer name instaed of that taken to variable. I'll investigate it.

                          1 person found this helpful
                          • 10. Re: Help with script that saves with the name of the active layer
                            tokuredit Level 1

                            By following your guidelines, I've made changes here to PDF and JPEG formats and they are all working perfectly well with your script! I am very pleased with the results.

                            Screenshot_1.jpg

                             

                            My biggest wish now is to be able to execute the File (pth) .execute () function with a button:

                             

                            Ex:

                            When the save process is completed, a small dialog box opens with a message type: The process has been completed successfully! and just below this message, I will add a button called "Open directory" ... would be the function File (pth) .execute ()

                             

                            I'm going to have to leave now and when I come back later, I'll make a dialog box show how it should work. See you....