Photoshop CS JS Adjust paragraph text

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

Larry Ligon

Photoshop CS JS Adjust paragraph text

Post by Larry Ligon »

This JavaScript will demonstrate how to insert a long line of text into a text layer and then convert it to paragraph text. It will adjust the paragraph height to accommodate the number of lines in the text based on the desired text width. This runs really slow on my computer. I don't know why. Wait until the dialog window pops up telling you that the script is finished.

Save in a file named Adjust paragraph text.js

Code: Select allalert("This script may take about 1 minute to run. \nWait for the finished dialog to display.");

var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.INCHES;
app.preferences.typeUnits = TypeUnits.POINTS;

var docRef = app.documents.add(8.5,11, 100,"Text adjust",NewDocumentMode.GRAYSCALE);
app.preferences.rulerUnits = Units.POINTS;
// suppress all dialogs
app.displayDialogs = DialogModes.NO;

var textColor = new SolidColor;
textColor.rgb.red = 0;
textColor.rgb.green = 0;
textColor.rgb.blue = 0;

var newTextPositionY = 36;

//First add a text layer in point text format.  This will extend off the canvas

var newTextLayer = docRef.artLayers.add();
newTextLayer.kind = LayerKind.TEXT;
newTextLayer.textItem.kind = TextType.POINTTEXT;
newTextLayer.textItem.justification = Justification.LEFT;
newTextLayer.textItem.position = Array(36,newTextPositionY );
newTextLayer.textItem.size = 17;
newTextLayer.textItem.font = "Arial";
newTextLayer.textItem.color = textColor;
newTextLayer.textItem.antiAliasMethod = AntiAlias.NONE;
var strMessage = "abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ"
      strMessage += "0123456789!@#$%^&*()?<\=>/abcdefghijklmnopqrstuvwxy"
      strMessage += "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()?<\=>/";
newTextLayer.textItem.contents = strMessage

//Now change the text layer type to paragraph so you can determine the width of the text
newTextLayer.textItem.kind = TextType.PARAGRAPHTEXT;

//This is the width of the text layer in points that I want it to be.
//You can adjust this to any width that you want.
var newTextWidth = "540 pt";

//Now calculate the number of lines in the text string and set the paragaph height accordingly
if (newTextLayer.textItem.width > newTextWidth ) {
    newTextLayer.textItem.height = newTextLayer.textItem.height * (Math.round(newTextLayer.textItem.width/newTextWidth ));
    newTextLayer.textItem.width = newTextWidth  ;
   };
newTextLayer.textItem.justification = Justification.CENTERJUSTIFIED;

docRef = null;
textColor = null;
newTextLayer = null;

alert("Finished");

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Larry Ligon

Photoshop CS JS Adjust paragraph text

Post by Larry Ligon »

Here is a version that will work in CS 2.

Code: Select allalert("This script may take about 1 minute to run. \nWait for the finished dialog to display.");

var strtRulerUnits = app.preferences.rulerUnits;
var strtTypeUnits = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.INCHES;
app.preferences.typeUnits = TypeUnits.POINTS;

var docRef = app.documents.add(8.5,11, 100,"Text adjust",NewDocumentMode.GRAYSCALE);
app.preferences.rulerUnits = Units.PIXELS;

// Resize the image to 72 dpi so that the paragraph.width returns the correct value
var originalDPI = docRef.resolution;
var resampleMethod = ResampleMethod.NONE;
activeDocument.resizeImage(null,null,72,resampleMethod );

app.preferences.rulerUnits = Units.POINTS;

// suppress all dialogs
app.displayDialogs = DialogModes.NO;

var textColor = new SolidColor;
textColor.rgb.red = 0;
textColor.rgb.green = 0;
textColor.rgb.blue = 0;

var newTextPositionY = 36;

//First add a text layer in point text format.  This will extend off the canvas

var newTextLayer = docRef.artLayers.add();
newTextLayer.kind = LayerKind.TEXT;
newTextLayer.textItem.kind = TextType.POINTTEXT;
newTextLayer.textItem.justification = Justification.FULLYJUSTIFIED;
newTextLayer.textItem.position = Array(36,newTextPositionY );
newTextLayer.textItem.size = 18;
newTextLayer.textItem.font = "Arial";
newTextLayer.textItem.color = textColor;
//newTextLayer.textItem.antiAliasMethod = AntiAlias.NONE;
newTextLayer.textItem.minimumLetterScaling = 0;
newTextLayer.textItem.desiredLetterScaling = 0;
var strMessage = "abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ"
      strMessage += "0123456789!@#$%^&*()?<\=>/abcdefghijklmnopqrstuvwxy"
      strMessage += "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()?<\=>/";
newTextLayer.textItem.contents = strMessage

//Now change the text layer type to paragraph so you can determine the width of the text
newTextLayer.textItem.kind = TextType.PARAGRAPHTEXT;
newTextLayer.textItem.minimumLetterScaling = 0;
newTextLayer.textItem.desiredLetterScaling = 0;

//alert("Original width = " + newTextLayer.textItem.width+ "\nOriginal height = " + newTextLayer.textItem.height );

//This is the width of the text layer in points that I want it to be.
//You can adjust this to any width that you want.
var newTextWidth = "540 pt";

//Now calculate the number of lines in the text string and set the paragaph height accordingly
if (newTextLayer.textItem.width > newTextWidth ) {
    newTextLayer.textItem.height = newTextLayer.textItem.height * (Math.round(newTextLayer.textItem.width/newTextWidth ));
    newTextLayer.textItem.width = newTextWidth  ;
   };
newTextLayer.textItem.justification = Justification.CENTERJUSTIFIED;

//alert("New width = " + newTextLayer.textItem.width + "\nNew height = " + newTextLayer.textItem.height );
docRef = null;
textColor = null;
newTextLayer = null;

// Restore the original image resolution
// Resize the image to 72 dpi so that the paragraph.width returns the correct value
activeDocument.resizeImage(null,null,originalDPI ,resampleMethod );

app.preferences.rulerUnits = strtRulerUnits;
app.preferences.typeUnits = strtTypeUnits;

alert("Finished");

This still runs really slow.

Larry
MickM

Photoshop CS JS Adjust paragraph text

Post by MickM »

What do you think is going wrong here then Larry? It looks like the old points units bug where the ruler units have to be in points for text or layer positioning to work but that bug was in CS as well. I haven't time right now to check out your code as I have to go to work.
Guest

Photoshop CS JS Adjust paragraph text

Post by Guest »

MickM,
No. If you look at the first set of code, I have the ruler set to points. It only works if the resolution is 72.

Larry
abdul
Posts: 3
Joined: Wed Dec 01, 2021 11:22 am

Re: Photoshop CS JS Adjust paragraph text

Post by abdul »

how can i do this
if in my text layer content is "Abdul karim mia"
and i want to Resize it
abdul will be 10 and Karim mia will be 5
and its a paragraph text
Last edited by abdul on Wed Dec 01, 2021 11:30 am, edited 1 time in total.
User avatar
Kukurykus
Posts: 529
Joined: Mon Jul 25, 2016 12:36 pm

Re: Photoshop CS JS Adjust paragraph text

Post by Kukurykus »

Could you post before and after result screenshots?
abdul
Posts: 3
Joined: Wed Dec 01, 2021 11:22 am

Re: Photoshop CS JS Adjust paragraph text

Post by abdul »

its done i have fixed my issue
thanks
User avatar
Kukurykus
Posts: 529
Joined: Mon Jul 25, 2016 12:36 pm

Re: Photoshop CS JS Adjust paragraph text

Post by Kukurykus »

Still you can share a method you used - others with similar problem will appreciate it ;)