9 Replies Latest reply on Aug 10, 2010 5:43 AM by ocphotog

    Parsing a filename script

    ocphotog Level 1

      HI,

      I'm trying to take the image filename and parse it so that I only get the pose number left. For example: the filename is Jones12345_002.jpg. I only want to get the 002 part of the filename. I've looked at the addfilename scripts but haven't found anything that will separate the name.

       

      I've tried this code but it seems to skip the parsing code I added:

       

      //start code

       

      // this script is a variation of the script addTimeStamp.js that is installed with PH7

      if ( documents.length > 0 )
      {
      var originalRulerUnits = preferences.rulerUnits;
      preferences.rulerUnits = Units.PIXELS;

      try
      {
        var docRef = activeDocument;

        // Now create a text layer at the front
        var myLayerRef = docRef.artLayers.add();
        myLayerRef.kind = LayerKind.TEXT;
        myLayerRef.name = "Filename";
       
        var myTextRef = myLayerRef.textItem;
       
        // strip the extension off
        var fileNameNoExtension = docRef.name;
        fileNameNoExtension = fileNameNoExtension.split( "." );
        if ( fileNameNoExtension.length > 1 ) {
         fileNameNoExtension.length--;
        }
        fileNameNoExtension = fileNameNoExtension.join(".");
        
        myTextRef.contents = fileNameNoExtension;
       
       
      // test trimming filename to last 3 characters

      var sFileName = " ";
      yStr = myTextRef.contents;
      function getFname(yStr) {

      for (nloop=yStr.length-1;nloop>1;nloop--) {
          if (yStr.charAt (nloop) =="_") {
            sFileName=yStr.substring(nloop+1,yStr.length);
             break;
      }
      }
      return sFileName;

      }
      myTextRef.contents = sFileName;

      // end trimming filename to last 3 characters
       

       
        // off set the text to be in the middle
        myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
        myTextRef.size = 20;
      }
      catch( e )
      {
        // An error occurred. Restore ruler units, then propagate the error back
        // to the user
        preferences.rulerUnits = originalRulerUnits;
        throw e;
      }

      // Everything went Ok. Restore ruler units
      preferences.rulerUnits = originalRulerUnits;
      }
      else
      {
      alert( "You must have a document open to add the filename!" );
      }

       

      //end code

       

      Thanks for any help

        • 1. Re: Parsing a filename script
          Paul Riggott Level 6

          Here is an example...

          var file = File( "/c/folder/folder2/Jones12345_002.jpg");
          var Name =  decodeURI(file.name).replace(/\.[^\.]+$/, '');
          var lastNumbers = Name.match(/\d+$/);
          alert(lastNumbers);
          
          • 2. Re: Parsing a filename script
            ocphotog Level 1

            Hi Paul,

            thanks for your quick response but I'm a bit confused with your code (Newbie here!) I have a file open in photoshop and I want to add a text layer to the file that just has the last 3 numbers of the filename in it. I think the code you provided is a bit more than I need. Can you help me parsing name of the open file?  Thanks in advance

            • 3. Re: Parsing a filename script
              Paul Riggott Level 6

              Okay here is your code with the modification...

              if ( documents.length > 0 )
              {
              var originalRulerUnits = preferences.rulerUnits;
              preferences.rulerUnits = Units.PIXELS;
              try
              {
                var docRef = activeDocument;
                // Now create a text layer at the front
                var myLayerRef = docRef.artLayers.add();
                myLayerRef.kind = LayerKind.TEXT;
                myLayerRef.name = "Filename";
                
                var myTextRef = myLayerRef.textItem;
                
                // strip the extension off
              var Name =  decodeURI(docRef.name).replace(/\.[^\.]+$/, '');
              var fileNameNoExtension = Name.match(/\d+$/).toString();  
              myTextRef.contents = fileNameNoExtension; 
                // off set the text to be in the middle
                myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
                myTextRef.size = 20;
              }
              catch( e )
              {
                // An error occurred. Restore ruler units, then propagate the error back
                // to the user
                preferences.rulerUnits = originalRulerUnits;
                throw e;
              }
              // Everything went Ok. Restore ruler units
              preferences.rulerUnits = originalRulerUnits;
              }
              else
              {
              alert( "You must have a document open to add the filename!" );
              }
               
              //end code
              
              • 4. Re: Parsing a filename script
                ocphotog Level 1

                Paul, THANK YOU, THANK YOU! It works perfectly. I wasted 5 hours and wasn't even close to getting it.

                • 5. Re: Parsing a filename script - Got a glitch
                  ocphotog Level 1

                  Hi Paul,

                   

                  Got the last 3 numbers to show up fine but now I came across a glitch. If I have a filename that is  "  Jones12345_002BW.jpg or  Jones12345_002_BG  "  I get an error that says "Error 54 Uncaught Javascript exception: TypeError: null is not an object. Line 37  -> throw e; "

                  I know it's from the BW or the _BG added. Is there a way to make an    if/else statement to account for those two events along with the regular Jones12345_002.jpg that I'll be encountering?

                  Hoping you can help me once again.

                  Thanks

                  Mark

                  • 6. Re: Parsing a filename script - Got a glitch
                    Paul Riggott Level 6

                    Please try this now, just one more line added

                     

                    if ( documents.length > 0 )
                    {
                    var originalRulerUnits = preferences.rulerUnits;
                    preferences.rulerUnits = Units.PIXELS;
                    try
                    {
                      var docRef = activeDocument;
                      // Now create a text layer at the front
                      var myLayerRef = docRef.artLayers.add();
                      myLayerRef.kind = LayerKind.TEXT;
                      myLayerRef.name = "Filename";
                      
                      var myTextRef = myLayerRef.textItem;
                      
                      // strip the extension off
                    var Name =  decodeURI(docRef.name).replace(/\.[^\.]+$/, '');
                    Name = Name.replace(/[a-zA-Z_]+$/gi,'');
                    var fileNameNoExtension = Name.match(/\d+$/).toString();  
                    myTextRef.contents = fileNameNoExtension; 
                      // off set the text to be in the middle
                      myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
                      myTextRef.size = 20;
                    }
                    catch( e )
                    {
                      // An error occurred. Restore ruler units, then propagate the error back
                      // to the user
                      preferences.rulerUnits = originalRulerUnits;
                      throw e;
                    }
                    // Everything went Ok. Restore ruler units
                    preferences.rulerUnits = originalRulerUnits;
                    }
                    else
                    {
                    alert( "You must have a document open to add the filename!" );
                    }
                     
                    //end code
                    

                     

                    Or condense it down....

                    if ( documents.length > 0 ){
                    var originalRulerUnits = preferences.rulerUnits;
                    preferences.rulerUnits = Units.PIXELS;
                    try{
                      var docRef = activeDocument;
                      var myLayerRef = docRef.artLayers.add();
                      myLayerRef.kind = LayerKind.TEXT;
                      myLayerRef.name = "Filename";
                      var myTextRef = myLayerRef.textItem;
                    myTextRef.contents =  decodeURI(docRef.name).replace(/[a-zA-Z_\.]+$/gi,'').match(/\d+$/).toString();
                    myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
                    myTextRef.size = 20;
                    }catch( e ){
                      preferences.rulerUnits = originalRulerUnits;
                      throw e;
                    }
                    preferences.rulerUnits = originalRulerUnits;
                    }else{
                    alert( "You must have a document open to add the filename!" );
                    }
                    

                    • 7. Re: Parsing a filename script - Got a glitch
                      ocphotog Level 1

                      Hi Paul,

                       

                      Thanks again for jumping in and helping. I think I didn't make myself clear enough though. What you gave me does get rid of anything after the 3 number pose but what I really needed was to have the script be able to display any of the following:

                       

                      original filename: Jones12345_003.jpg.    result needed:  003

                      original filename: Jones12345_003BW.jpg    result needed:  003BW

                      original filename: Jones12345_003_BW.jpg   result needed: 003_BW

                       

                       

                      This would allow me to send it any file and have it give back the filenumber after the " _  " so that I can have some images in color with the pose # and some images that may be in B&W also show the pose# and the BW.

                       

                      Hope you can help again.  Thanks

                      Mark

                      • 8. Re: Parsing a filename script - Got a glitch
                        Paul Riggott Level 6

                        Sorry about that (Its old age ) Please try this now..

                        if ( documents.length > 0 ){
                        var originalRulerUnits = preferences.rulerUnits;
                        preferences.rulerUnits = Units.PIXELS;
                        try{
                          var docRef = activeDocument;
                          var myLayerRef = docRef.artLayers.add();
                          myLayerRef.kind = LayerKind.TEXT;
                          myLayerRef.name = "Filename";
                          var myTextRef = myLayerRef.textItem;
                        var Numbers =  decodeURI(docRef.name).replace(/[a-zA-Z_\.]+$/gi,'').match(/\d+$/).toString();
                        var End =  decodeURI(docRef.name).replace(/\.[^\.]+$/, '').toString().match(/[^\d]+$/);
                        var Name ='';
                        if(End != null) {
                             Name = Numbers +End;
                             }else{
                                  Name = Numbers;
                                  }
                        myTextRef.contents =  Name;
                        myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
                        myTextRef.size = 20;
                        }catch( e ){
                          preferences.rulerUnits = originalRulerUnits;
                          throw e;
                        }
                        preferences.rulerUnits = originalRulerUnits;
                        }else{
                        alert( "You must have a document open to add the filename!" );
                        }
                        

                        1 person found this helpful
                        • 9. Re: Parsing a filename script - Got a glitch
                          ocphotog Level 1

                          Paul, Thank you again. It's perfect! Not sure how it all works but it does the job just as I need it to.

                          Thanks

                          Mark