Skip to main content
Participant
April 1, 2020
Answered

Setting firstLineIndent with Javascript not working

  • April 1, 2020
  • 2 replies
  • 783 views

I have a javascript-program that worked in 2014 for adding text given in a text file to images as captions. Today I wanted to reuse it with Photoshop 21.1.1 and it ran almost succesfully. Only setting the indent for the created Text Item with the "firstLineIndent"-property is not taken; it is always 0, but  no errors are shown. 

    var artLayerRef = app.activeDocument.artLayers.add();
    artLayerRef.name = lname;
    artLayerRef.kind = LayerKind.TEXT;

    //Get a reference to the text item so that we can add the text and format it a bit
    textItemRef = artLayerRef.textItem;
    textItemRef.kind = TextType.PARAGRAPHTEXT;
    textItemRef.font = fface;
    textItemRef.color = textColor;
    textItemRef.size = size;

    textItemRef.contents = content;
    textItemRef.position = new Array(tX, tY);
    textItemRef.width = wdth;
    textItemRef.height = hght;

    textItemRef.justification = just;
    textItemRef.firstLineIndent = 32;   // ERROR: the property is not set to the new value
    textItemRef.firstLineIndent.value = 32;   // This is not working too
Any idea what goes wrong or what was changed since 2014?

Regards, Rolf
This topic has been closed for replies.
Correct answer JJMack

I only hack at Photoshop.  I do not know that much about JavaSctipt or Adobe Photoshop Scripting.

your code it looks like your code

 

 //Get a reference to the text item so that we can add the text and format it a bit
textItemRef = artLayerRef.textItem;

 

initalizes textItemRef.firstLineIndent to "0 pt"

and your statement: 

textItemRef.firstLineIndent = indent;

Does not change that setting and does not throw an error where indent is 32???

 

I change your code a bit and through in a couple of aletrs

 

 

// Create Image with 1382 x 787 Pixel
app.preferences.rulerUnits = Units.PIXELS;
app.documents.add(1382, 787, 300, "Image Caption", NewDocumentMode.RGB);
createText("Caption", "Calibri", 8.0, 0, 0, 0, "Description Text", 5, 179, 324, 14, Justification.LEFT, 32, 0);

function createText(lname, fface, size, colR, colG, colB, content, tX, tY, wdth, hght, just, indent, rot) {

    // Add a new layer in the new document
    var artLayerRef = app.activeDocument.artLayers.add();
    artLayerRef.name = lname;
    // Specify that the layer is a text layer
    artLayerRef.kind = LayerKind.TEXT;
    artLayerRef.rotate(rot);

    //This section defines the color of the text
    textColor = new SolidColor();
    textColor.rgb.red = colR;
    textColor.rgb.green = colG;
    textColor.rgb.blue = colB;

    //Get a reference to the text item so that we can add the text and format it a bit
    textItemRef = artLayerRef.textItem;
    textItemRef.kind = TextType.PARAGRAPHTEXT;
    textItemRef.font = fface;
    textItemRef.color = textColor;
    textItemRef.size = size;


    textItemRef.position = new Array(tX, tY); //pixels from the left, pixels from the top
    textItemRef.width = wdth;
    textItemRef.height = hght;

    textItemRef.justification = just;
    //app.preferences.typeUnits = TypeUnits.POINTS;
    //app.preferences.Units = Units.POINTS;
alert("textItemRef.firstLineIndent=" + textItemRef.firstLineIndent);
    textItemRef.firstLineIndent = indent;
alert("indent=" + indent + " textItemRef.firstLineIndent=" + textItemRef.firstLineIndent);
/*
    ind = new UnitValue(indent, "pt");
    textItemRef.firstLineIndent = ind;
*/
    textItemRef.contents = content;
}

 

I doe not understand what  for I see this so what is wrong with 32?

 


Your script works in CS6 Creative Cloud version 13.1.2. It does not work in CC 2014, CC 2018, CC 2019 or PS 2020.  It works in CS3 also.... Report the bug to Adobe  at photoshop_family 

 

Adobe need to improve their development and testing......I read other Adobe program are also full of bugs. Its the main reason I do not install other creative cloud applications. I work around the buge the bite me in Photoshop. Adobe does not fix all reported bugs. They will fix bugs that crash Photoshop other types of bugs may not be fixed.

 

 

The easy work aroynd to to set no indent in any version of Photoshop and add the requiured spaces in front of content

 

 

    textItemRef.contents =  requiredspaces + content;

 

 

2 replies

JJMack
Community Expert
Community Expert
April 1, 2020

We can not see the rest of your script so can see if there is a problem like you wrote or a problem in your script.  The code you posted has many variable  but  their set code is not included. Including "just". I have no idea what indent 32 would do with center or right justification.

JJMack
Participant
April 1, 2020

Thanks you both for your immediate response and sorry to not provide sufficient info.

Here is a standalone example, where the indent is not working as expected. Tried with setting units, but maybe not the right way.

// Create Image with 1382 x 787 Pixel
app.preferences.rulerUnits = Units.PIXELS;
app.documents.add(1382, 787, 300, "Image Caption", NewDocumentMode.RGB);
createText("Caption", "Calibri", 8.0, 0, 0, 0, "Description Text", 5, 179, 324, 14, Justification.LEFT, 32, 0);

function createText(lname, fface, size, colR, colG, colB, content, tX, tY, wdth, hght, just, indent, rot) {

    // Add a new layer in the new document
    var artLayerRef = app.activeDocument.artLayers.add();
    artLayerRef.name = lname;
    // Specify that the layer is a text layer
    artLayerRef.kind = LayerKind.TEXT;
    artLayerRef.rotate(rot);

    //This section defines the color of the text
    textColor = new SolidColor();
    textColor.rgb.red = colR;
    textColor.rgb.green = colG;
    textColor.rgb.blue = colB;

    //Get a reference to the text item so that we can add the text and format it a bit
    textItemRef = artLayerRef.textItem;
    textItemRef.kind = TextType.PARAGRAPHTEXT;
    textItemRef.font = fface;
    textItemRef.color = textColor;
    textItemRef.size = size;

    textItemRef.contents = content;
    textItemRef.position = new Array(tX, tY); //pixels from the left, pixels from the top
    textItemRef.width = wdth;
    textItemRef.height = hght;

    textItemRef.justification = just;
    app.preferences.typeUnits = TypeUnits.POINTS;
    app.preferences.Units = Units.POINTS;
    // textItemRef.firstLineIndent = indent;
    ind = new UnitValue(indent, "pt");
    textItemRef.firstLineIndent = ind;
}

Setting the indent of the text box, that was created by the script, manually in PS works fine.

 

Rolf

 

JJMack
Community Expert
Community Expert
April 1, 2020

I only hack at Photoshop.  I do not know that much about JavaSctipt or Adobe Photoshop Scripting.

your code it looks like your code

 

 //Get a reference to the text item so that we can add the text and format it a bit
textItemRef = artLayerRef.textItem;

 

initalizes textItemRef.firstLineIndent to "0 pt"

and your statement: 

textItemRef.firstLineIndent = indent;

Does not change that setting and does not throw an error where indent is 32???

 

I change your code a bit and through in a couple of aletrs

 

 

// Create Image with 1382 x 787 Pixel
app.preferences.rulerUnits = Units.PIXELS;
app.documents.add(1382, 787, 300, "Image Caption", NewDocumentMode.RGB);
createText("Caption", "Calibri", 8.0, 0, 0, 0, "Description Text", 5, 179, 324, 14, Justification.LEFT, 32, 0);

function createText(lname, fface, size, colR, colG, colB, content, tX, tY, wdth, hght, just, indent, rot) {

    // Add a new layer in the new document
    var artLayerRef = app.activeDocument.artLayers.add();
    artLayerRef.name = lname;
    // Specify that the layer is a text layer
    artLayerRef.kind = LayerKind.TEXT;
    artLayerRef.rotate(rot);

    //This section defines the color of the text
    textColor = new SolidColor();
    textColor.rgb.red = colR;
    textColor.rgb.green = colG;
    textColor.rgb.blue = colB;

    //Get a reference to the text item so that we can add the text and format it a bit
    textItemRef = artLayerRef.textItem;
    textItemRef.kind = TextType.PARAGRAPHTEXT;
    textItemRef.font = fface;
    textItemRef.color = textColor;
    textItemRef.size = size;


    textItemRef.position = new Array(tX, tY); //pixels from the left, pixels from the top
    textItemRef.width = wdth;
    textItemRef.height = hght;

    textItemRef.justification = just;
    //app.preferences.typeUnits = TypeUnits.POINTS;
    //app.preferences.Units = Units.POINTS;
alert("textItemRef.firstLineIndent=" + textItemRef.firstLineIndent);
    textItemRef.firstLineIndent = indent;
alert("indent=" + indent + " textItemRef.firstLineIndent=" + textItemRef.firstLineIndent);
/*
    ind = new UnitValue(indent, "pt");
    textItemRef.firstLineIndent = ind;
*/
    textItemRef.contents = content;
}

 

I doe not understand what  for I see this so what is wrong with 32?

 

JJMack
Legend
April 1, 2020

Nothing has changed. You probably need to change your units to points before you set this property.