1 Reply Latest reply on Apr 11, 2011 9:13 PM by JJMack

    Using Javascript & Actions to resize an image and add the filename as text

    meagan82

      Hello,

       

      I am currently working on a way to take an image, resize it and add exactly what is in it's file name (before extension) as a text layer. I have succeded in this BUT am having issues with file names that require more than one line of text.

       

      I need the text to align to the bottom of the image & the script or action must then resize the image so that the text does not overlap.

       

       

      Any ideas on how this can be done?

       

      At the moment I am using:

      -"Fit Image.jsx" to resize my image to a specific size (This was included in the Photoshop CS5 scripts)

      - A script to add the file name without extension and place the file name at a specific position.

       

      AdFileName.jsx

      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 middle
        myTextRef.position = new Array( docRef.width / 2, docRef.height / 2 );
        myTextRef.size = 12;
              myTextRef.font="Arial-BoldMT"
             
              //set position of text
              myTextRef.justification = Justification.CENTER;
              myTextRef.kind = TextType.PARAGRAPHTEXT;
              myTextRef.width= docRef.width;
              myTextRef.height= docRef.height/ 2;
              myTextRef.position= [0, docRef.height * 0.88];
             
      }
      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!" );
      }

       

      Can the position be changed to allow more rows of text but keep it aligned with the bottom of the layer?

      How can I script in a way to make the image resize based on the amount of text lines?

       

      Any help would be greatly appreciated.

        • 1. Re: Using Javascript & Actions to resize an image and add the filename as text
          JJMack Most Valuable Participant

          You can add a text layer any where the only thing you need to worry about is the size of the font you use.  You can normally calculate a font size by saving the images resoltuion and then setting its resolution to 72 DPI calculate a font size basied of the images pixel width and the number of characters you want in a line. After adding the text layer you can restore the image to its original resolution and align the text layer by making a selection and alignint the text layer to the selection.  There are nine posibilites like the positions in the selection you can align to like a tick tack toe board. You need to use a script to add the text layer because your retrieving the filename.  The positioning of the text layer could be done easily in an action the would use the scriot to add a text layer the do a select all  the align the added text layer to the selection.

           

          About your script don't make text paragraph just add new line characters to make a multi line text layer So it can be positioned easily

           

          I do just that with my stampexif action.  The action uses my stampexif Photoshop java script to add a multi line text layer containing some formatted EXIF data then the action centers the text layer and add a layer style to it. Link http://www.mouseprints.net/old/dpr/StampExif.jsx Example

          http://mouseprints.net/old/dpr/Examples/StampEXIF.jpg