2 Replies Latest reply on Oct 24, 2017 10:18 AM by Fjuds

    Stripping first 4 characters in a 8 character file name inside a script?

    Fjuds Level 1

      I have a script to add the file number to the front of the image for proofing. I would like to remove the first 4 characters in the 8 character file name in the script.

      I don't code so I'm looking for the lines of code to add to the script.

      It would help reduce paperwork errors when we show portraits.

       

      Thanks for any help,

      Fjuds

       

      // 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;

       

        // off set the text to be in the bottom center

        myTextRef.position = new Array( docRef.width / 3, docRef.height / 1.03 );

        myTextRef.size = 60;

        }

        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!" );

      }

        • 1. Re: Stripping first 4 characters in a 8 character file name inside a script?
          SuperMerlin Level 5
          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;
            myTextRef.contents = decodeURI(docRef.name).replace(/\.[^\.]+$/, '').match(/(....)(.+)/)[2];
            // off set the text to be in the bottom center
            myTextRef.position = new Array( docRef.width / 3, docRef.height / 1.03 );
            myTextRef.size = 60;
            }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!" );
          };
          
          
          • 2. Re: Stripping first 4 characters in a 8 character file name inside a script?
            Fjuds Level 1

            Hello Super Merlin,

             

            Thanks it works great. It should solve a periodic problem I have with mis marked orders. It definitely simplifies the file name on the proof.

             

            I had made miner mods to the script but adding code is out of my wheel house.

             

            Thanks Again,

            Frank