4 Replies Latest reply on Nov 11, 2013 12:10 AM by Yuliya Trifonoff

    Centering Text

    Xismo1

      Hi all,

       

      hope you can help me with my query, I'm sure it's pretty simple just suck at JS. I downloaded the AddFileNamePlus script and everything is working perfectly except I would like to have the text layer that gets created to be set as paragraph text taking up full width of the document and text inside centered. Below is the code that I'm currently using which just creates a regular text layer. If anyone knows how to do this it would be greatly appreciated!!

       

      Thanks.

       

       

       

      //Check if a document is open

      if ( documents.length > 0 )

      {

      var originalRulerUnits = preferences.rulerUnits;

      preferences.rulerUnits = Units.PERCENT;

       

      try

      {

      var docRef = activeDocument;

       

      // Create a text layer at the front

       

      var myLayerRef = docRef.artLayers.add();

      myLayerRef.kind = LayerKind.TEXT;

      myLayerRef.name = "Filename";

      var myTextRef = myLayerRef.textItem;

       

      //Set your parameters below this line

       

      //If you wish to show the file extension, change the n to y in the line below, if not use n.

      var ShowExtension = "n";

      // Insert any text to appear before the filename, such as your name and copyright info between the quotes.

      //If you do not want extra text, delete between the quotes (but leave the quotes in).

      var TextBefore = "";

       

      // Insert any text to appear after the filename between the quotes.

      //If you do not want extra text, delete between the quotes (but leave the quotes in).

      var TextAfter = "";

       

      // Set font size in Points

      myTextRef.size = 12;

       

      //Set font - use GetFontName.js to get exact name

      myTextRef.font = "Arial-BoldMT";

       

       

      //Set text colour in RGB values

      var newColor = new SolidColor();

      newColor.rgb.red = 0;

      newColor.rgb.green = 0;

      newColor.rgb.blue = 0;

      myTextRef.color = newColor;

       

      // Set the position of the text - percentages from left first, then from top.

      myTextRef.position = new Array( 20, 98);

       

      // Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.

      myLayerRef.blendMode = BlendMode.NORMAL;

       

      // select opacity in percentage

      myLayerRef.opacity = 100;

       

      // The following code strips the extension and writes tha text layer. fname = file name only

       

      di=(docRef.name).indexOf(".");

      fname = (docRef.name).substr(0, di);

      //use extension if set

      if ( ShowExtension == "y" )

      {

         fname = docRef.name

         } 

       

       

      myTextRef.contents = TextBefore + "  " + fname +  "  " + TextAfter;

       

       

      }

      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: Centering Text
          c.pfaffenbichler Level 9

          Could you give this a try?

          Instead of percent it sets the rulerUnits to pixels and temporarily resizes the image to 72ppi.

           

          #target photoshop
          //Check if a document is open
          if ( documents.length > 0 )
          {
          var originalRulerUnits = preferences.rulerUnits;
          preferences.rulerUnits = Units.PIXELS;
          var originalResolution = app.activeDocument.resolution;
          
          try
          {
          var docRef = activeDocument;
          docRef.resizeImage (undefined, undefined, 72, ResampleMethod.NONE);
          
          // Create a text layer at the front
           
          var myLayerRef = docRef.artLayers.add();
          myLayerRef.kind = LayerKind.TEXT;
          myLayerRef.name = "Filename";
          var myTextRef = myLayerRef.textItem;
          
          //Set your parameters below this line
           
          //If you wish to show the file extension, change the n to y in the line below, if not use n.
          var ShowExtension = "n";
          // Insert any text to appear before the filename, such as your name and copyright info between the quotes.
          //If you do not want extra text, delete between the quotes (but leave the quotes in).
          var TextBefore = "";
           
          // Insert any text to appear after the filename between the quotes.
          //If you do not want extra text, delete between the quotes (but leave the quotes in).
          var TextAfter = "";
           
          // Set font size in Points
          myTextRef.size = 12 * originalResolution / 72;
           
          //Set font - use GetFontName.js to get exact name
          myTextRef.font = "Arial-BoldMT";
           
           
          //Set text colour in RGB values
          var newColor = new SolidColor();
          newColor.rgb.red = 0;
          newColor.rgb.green = 0;
          newColor.rgb.blue = 0;
          myTextRef.color = newColor;
           
          // Set the position of the text.
          myTextRef.justification = Justification.CENTER;
          myTextRef.kind = TextType.PARAGRAPHTEXT;
          myTextRef.width = docRef.width;
          myTextRef.height = docRef.height / 5;
          myTextRef.position = [0, docRef.height * 0.95  ];
          
          
          // Set the Blend Mode of the Text Layer. The name must be in CAPITALS - ie change NORMAL to DIFFERENCE.
          myLayerRef.blendMode = BlendMode.NORMAL;
           
          // select opacity in percentage
          myLayerRef.opacity = 100;
           
          // The following code strips the extension and writes tha text layer. fname = file name only
           
          di=(docRef.name).indexOf(".");
          fname = (docRef.name).substr(0, di);
          //use extension if set
          if ( ShowExtension == "y" )
          {
             fname = docRef.name
             } 
           
           
          myTextRef.contents = TextBefore + "  " + fname +  "  " + TextAfter;
           
           
          }
          catch( e )
          {
          // An error occurred. Restore ruler units, then propagate the error back
          // to the user
          preferences.rulerUnits = originalRulerUnits;
          docRef.resizeImage (undefined, undefined, originalResolution, ResampleMethod.NONE);
          throw e;
          }
           
          // Everything went Ok. Restore ruler units
          preferences.rulerUnits = originalRulerUnits;
          docRef.resizeImage (undefined, undefined, originalResolution, ResampleMethod.NONE);
          }
          else
          {
          alert( "You must have a document open to add the filename!" );
          }
          

          • 2. Re: Centering Text
            Xismo1 Level 1

            That is absolutely wonderful! Works like a charm, thanks!

             

            Just wondering if you know why when the text layer is created there are two empty spaces before and after the text. Also if I ever need to increase the height of the created paragraph text so I can fit say two or three lines instead of one what would I have to modify? I saw you are dividing the height by 5 which I don't quite understand. And finally where do you set the temporary ppi change? Wanna know because in the future I will try to create a script that does automated image resizing as well as inclusion of the filename and images will always have to be at 72 ppi. Sorry for all the questions just trying to understand a bit more. I really need to start learning JS after seeing just how much is possible with scripts in PS =)

            • 3. Re: Centering Text
              c.pfaffenbichler Level 9
              Also if I ever need to increase the height of the created paragraph text so I can fit say two or three lines instead of one what would I have to modify?
              The fifth of the document’s height was arbitrary, to fit more text you should correct these settings:
              myTextRef.height = docRef.height / 5;
              myTextRef.position = [0, docRef.height * 0.95  ];
              
              And finally where do you set the temporary ppi change?
              var originalResolution = app.activeDocument.resolution;
              docRef.resizeImage (undefined, undefined, 72, ResampleMethod.NONE);
              
              and back to the original resolution
              docRef.resizeImage (undefined, undefined, originalResolution, ResampleMethod.NONE);
              
              • 4. Re: Centering Text
                Yuliya Trifonoff

                How can I add to this script to show too the Creation Date and Time of my picture?

                I have not problem to show current date and time but I'd like Creation Date and Time.