Quantcast
Channel: Adobe Community : Popular Discussions - Photoshop Scripting
Viewing all articles
Browse latest Browse all 12244

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

$
0
0

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.


Viewing all articles
Browse latest Browse all 12244

Trending Articles