13 Replies Latest reply on Jun 9, 2017 9:45 AM by pixxxel schubser

    Save in directory based on filename.

    kareyh56356757 Level 1

      I am very new to scripting.  I am looking to create a script that will save a .tif file with LZW compression to a file in a directory based on the current image filename.  If that directory doesn't exist it will create it.  All of my filenames are based on a 7 digit system. 

       

      For example... I want to take file name  "123-456_A"  and put it in the following folder system...

       

      Product(Starting folder on the server)
      -1
      --12
      ---123
      ----123-
      -----123-4
      ------123-45
      -------123-456(Final Folder)

       

      The "_A" etc will be ignored. Is this possible? 

        • 1. Re: Save in directory based on filename.
          c.pfaffenbichler Level 9

          Please post screenshots illustrating the task and Folder structure better.

           

          And please explain what exactly you mean by

          save a .tif file with LZW compression

          Save a copy of the active File? Save the active File as a new file?

          With Layers or flattened?

          • 2. Re: Save in directory based on filename.
            kareyh56356757 Level 1

            Screen Shot 2017-01-25 at 7.09.24 AM.png
            Would be saving the active file as a new file with the following TIF options.

            Screen Shot 2017-01-25 at 7.11.36 AM.png

            • 3. Re: Save in directory based on filename.
              pixxxel schubser MVP & Adobe Community Professional

              kareyh56356757,

              a folder hierarchy like yours doesn't make any sense for me, sorry. But anyway, try this:

               

              // folder_saveTif_inFolderBasedOnFilename.jsx
              // https://forums.adobe.com/thread/2268221
              // regards pixxxel schubser
              
              // only file names eg 123-456_A or 100-123_Z allowed
              var aDoc = activeDocument, nme = aDoc.name.replace(/\.[^\.]+$/,""), reg = /^\d{3}-\d{3}_./, tmp = nme.split("");
              checkAndSave();
              
              function checkAndSave() {
                  if( reg.test( nme ) == false) {
                      alert ("wrong file name"); return; }
                  else { tmp.splice(3,1); tmp.splice(-2); tmp[2] += "-";
                      for( i = 1; i < 6; i++ ) { tmp[i] = tmp[i-1] + tmp[i]; };
                      var saveFol = Folder ("~/Desktop");
                      for ( i = 0; i < tmp.length; i++ ) {
                          saveFol = Folder( saveFol + "/" + tmp[i] );
                          if (!saveFol.exists) { saveFol.create() };
                      };
                  var saveName = "/" + nme + ".tif";
                  var saveFile = new File( saveFol + saveName );
                  if( saveFile.exists ) { alert( saveFile + "\nFile already exists - not saved now" ); return; };
                  saveAsTIFF( saveFile ); alert( saveFol + saveName + " saved" )
                  //aDoc.close( SaveOptions.DONOTSAVECHANGES );
                  }; return;
              };
              
              function saveAsTIFF( saveFile ) {
                  tiffSaveOptions = new TiffSaveOptions();
                  tiffSaveOptions.layers = true;
                  tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
                  activeDocument.saveAs( saveFile, tiffSaveOptions, false ); 
              }
              

               

               

              This snippet should create the wished folder hierarchy (on your desktop) based on the given example file name.

              Have fun

               

              1 person found this helpful
              • 4. Re: Save in directory based on filename.
                kareyh56356757 Level 1

                I didn't design the folder system.... I just have to use it   Is there anyway I can set an absolute path(?) that points to a location on a sever verses the desktop? I am on a Mac and tried putting in the path but it just returns and error.

                • 5. Re: Save in directory based on filename.
                  kareyh56356757 Level 1

                  I should add that it is an smb:// path

                  • 6. Re: Save in directory based on filename.
                    pixxxel schubser MVP & Adobe Community Professional

                    kareyh56356757,

                    what is the problem?

                     

                    At first: I'm not really familiar with Mac OS and server paths.

                     

                    Do you have a screenshot of the part of your desktop which shows your server icon?

                    Eg the name of the icon is "Transfer". Maybe this name is different to the real name of the server (eg sxyztech)

                     

                    One possible way to find the path is: open a file which is located directly on Transfer/yourExampleFile.psd

                    Run this line of code:

                    alert(activeDocument.path)
                    

                     

                    On your Mac you should get now a result like this:

                    /Transfer/yourExampleFile.psd
                    

                     

                     

                     

                    And now you can try to replace this line of my code:

                    var saveFol = Folder ("~/Desktop");
                    

                     

                    with this:

                    var saveFol = Folder ("/Transfer");
                    

                     

                    Perhaps this helps you to find your way.

                    Have fun

                     

                     

                    And give a feedback please. I'm very interested for your answer.

                    • 7. Re: Save in directory based on filename.
                      kareyh56356757 Level 1

                      That worked great to find the correct path.  Thank you!  One thing that I have noticed is that it is creating the "100-" folder in the wrong place.   This is so close and I am very grateful! 

                       

                      Screen Shot 2017-01-26 at 2.25.18 PM.png
                      It should show up as the highlighted folder above but instead it is showing as "100-"  where it should be "100".

                       

                      To add complication, my predecessor deviated from the folder system in spots.  In the example below the "203-" folder was skipped and went directly into the "203-X" numbering. 
                      Screen Shot 2017-01-26 at 2.28.08 PM.png

                       

                      Because all of these files are linked to another program I can't change the existing structure.  I can make sure it is correct going forward but not anything that is already there.  Is there anyway to include an "If" or "else" to take this into account?  I realize this is asking a lot and I may have to manual correct these items. 

                       

                      Thank you so much for your help so far!  This has been the absolute closest I have come to making his script a reality after lots of trial and error.

                      • 8. Re: Save in directory based on filename.
                        pixxxel schubser MVP & Adobe Community Professional

                        The first one is easy.

                         

                        Replace line #12 and #13 in my code with:

                            else { tmp.splice(-2); //tmp.splice(3,1); tmp.splice(-2); tmp[2] += "-";
                                for( i = 1; i < 7; i++ ) { tmp[i] = tmp[i-1] + tmp[i]; };
                        

                         

                        Have fun

                         

                         

                         

                        The second part is more complex. Sorry but I don't want spend time for check this now.

                        Furthermore it is another question.

                        • 9. Re: Save in directory based on filename.
                          kareyh56356757 Level 1

                          Thank you for all of your help!  It is very appreciated!!!

                          • 10. Re: Save in directory based on filename.
                            kareyh56356757 Level 1

                            This script has been working perfect for me!  Is there a way to alter it for files that have a naming structure of ZZ12345_A ?  Basically I want it to read any 7 digit filename and put it in the coordinating folder. 

                            • 11. Re: Save in directory based on filename.
                              pixxxel schubser MVP & Adobe Community Professional

                              Hi kareyh56356757,

                               

                              hmmh?

                              ZZ12345_A is not a 7 digit filename.

                               

                              Furthermore:

                              which letters and/or digits in the filename are fix and which ones are variable?

                              • 12. Re: Save in directory based on filename.
                                kareyh56356757 Level 1

                                I realized my mistake and fixed it with the following...

                                 

                                var aDoc = activeDocument, nme = aDoc.name.replace(/\.[^\.]+$/,""), reg = /^\w{2}\d{5}_./, tmp = nme.split(""); 

                                checkAndSave();

                                 

                                I had been trying to read the letters as digits. 

                                • 13. Re: Save in directory based on filename.
                                  pixxxel schubser MVP & Adobe Community Professional

                                   

                                   

                                  Regex is not easier way off. You should understand the syntax before.

                                  Try this snippet (standalone):

                                  var a = "1234567_A";
                                  var b = "ZZ12345_A";
                                  
                                  var  nme = a.replace(/\.[^\.]+$/,""), reg = /^\w{2}\d{5}_./, tmp = nme.split("");
                                  checkAndSave(); 
                                  nme = b.replace(/\.[^\.]+$/,""), reg = /^\w{2}\d{5}_./, tmp = nme.split("");
                                  checkAndSave(); 
                                  nme = a.replace(/\.[^\.]+$/,""), reg = /^[A-Z]{2}\d{5}_./, tmp = nme.split("");
                                  checkAndSave(); 
                                  nme = b.replace(/\.[^\.]+$/,""), reg = /^[A-Z]{2}\d{5}_./, tmp = nme.split("");
                                  checkAndSave(); 
                                  
                                  function checkAndSave() {  
                                      if( reg.test( nme ) == false) {  
                                          alert (reg+" better regex do not find: "+ nme); return; }  
                                      else { alert (reg+" found: "+nme) }
                                      }
                                  

                                   

                                  The reason is: \w will find all "word signs" eg digits AND letters

                                   

                                  Have fun

                                   

                                  1 person found this helpful