• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Count text letters & set font size

Explorer ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

hi, i am using photoshop variables to import csv file into photoshop.

the csv file contains three type of text lines.
1. 50 letter text line
2. 75 letter text line
3. 100 letter text line

i dont no anything about scripting. so i need help
i want a script that do following things.

for example, if text line contains "50 letters" set font size to "140 px".
if text line contains "75 letters" set font size to "110 px"
if text line contains "100 letters" set font size to "80 px"

TOPICS
Actions and scripting

Views

3.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 29, 2018 Apr 29, 2018

This should keep the visibility the same:

#target photoshop

var doc = activeDocument;

var txtLayer1 = doc.layers.getByName('Text1');

var txtLayer2 = doc.layers.getByName('Text2');

var txtLayer3 = doc.layers.getByName('Text3');

setSize (txtLayer1)

setSize (txtLayer2)

setSize (txtLayer2)

function setSize(layer){

    var vis = layer.visible

    doc.activeLayer = layer;

    try{

        if(layer.textItem.contents.length<51){layer.textItem.size = 140}

        else if(layer.textItem.contents.length<76){layer.textIt

...

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

You give no information about the Photoshop document. Canvas size pixel wide pixel high print resolution, Text also has a font  all are not the same size  characters vary in size in some and are fixed size in other.  Text is very complex.  Fully expand some actions that add text and look at the expended text steps. 

JJMack

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2018 Apr 27, 2018

Copy link to clipboard

Copied

i have excel file

& i  am using photoshop variables feature to import this data.

I am using variables visibility feature to switch between this three layer

I am exporting this files via "Data sets as files" feature, which creates 200+ psd files.

i want a script that do following things.

for example, if  contains "25 to 50 letters" set font size to "140 px".
if  contains "51 to 75 letters" set font size to "110 px"
if contains "76 to 100 letters" set font size to "80 px"

then i will create an action like this one.

Untitled.png

& apply them to all 200+ psd files via batch function.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 26, 2018 Apr 26, 2018

Copy link to clipboard

Copied

You need to import your cvs file into a string variable and break it into lines using the split command:

var doc = activeDocument;

var cvsLines = myCVSstring.split('\n');

for(var i=0;i<cvsLines.length;i++){

     //put code here to make a text layer

     var txtLayer = doc.activeLayer;//need code to create this text layer.

     if(cvsLines.length<60){txtLayer.size = 140}

     else if(cvsLines.length<85){txtLayer.size = 110}

     else{txtLayer.size = 80}

     }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2018 Apr 27, 2018

Copy link to clipboard

Copied

thank you for a script sir,

i have save your code as jsx file.

but when i load your script, it gives me following error

Untitled.png

one again, thanks for your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2018 Apr 27, 2018

Copy link to clipboard

Copied

My code is incomplete. It just shows how to split the text in a CVS string into lines and get their length, and then apply a size to an existing text layer. You still need to write the code to open the cvs file, and put its contents into a string. You also need to write the code to create the text layers, if they don't already exist.

Will have to look at your files later, when I'm at my computer.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 27, 2018 Apr 27, 2018

Copy link to clipboard

Copied

Try this after you import your data sets.

#target photoshop

var doc = activeDocument;

var txtLayer1 = doc.layers.getByName('Text1');

var txtLayer2 = doc.layers.getByName('Text2');

var txtLayer3 = doc.layers.getByName('Text3');

setSize (txtLayer1)

setSize (txtLayer2)

setSize (txtLayer2)

function setSize(layer){

    doc.activeLayer = layer;

    try{

        if(layer.textItem.contents.length<51){layer.textItem.size = 140}

        else if(layer.textItem.contents.length<76){layer.textItem.size = 110}

        else{layer.textItem.size = 80}

        }

    catch(e){}

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 29, 2018 Apr 29, 2018

Copy link to clipboard

Copied

Thank you so much, your code work perfectly.

However their is one problem

When i export files via "Data sets as files", it creates 200+ files

Each file will have 1 active text layer & 2 hidden text layers.

For example, the 3rd dataset file have

Text1 & Text2 layer are hidden &

Text3 layer is visible

This script working fine but it is making all three layers visible

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 29, 2018 Apr 29, 2018

Copy link to clipboard

Copied

This should keep the visibility the same:

#target photoshop

var doc = activeDocument;

var txtLayer1 = doc.layers.getByName('Text1');

var txtLayer2 = doc.layers.getByName('Text2');

var txtLayer3 = doc.layers.getByName('Text3');

setSize (txtLayer1)

setSize (txtLayer2)

setSize (txtLayer2)

function setSize(layer){

    var vis = layer.visible

    doc.activeLayer = layer;

    try{

        if(layer.textItem.contents.length<51){layer.textItem.size = 140}

        else if(layer.textItem.contents.length<76){layer.textItem.size = 110}

        else{layer.textItem.size = 80}

        }

    catch(e){}

    layer.visible = vis

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 29, 2018 Apr 29, 2018

Copy link to clipboard

Copied

All problems are solved, thnx you one again.

You made my day, god bless you

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
May 01, 2018 May 01, 2018

Copy link to clipboard

Copied

Bonjour

Why the same problem on two forums ?

Here

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

hi, i got one more problem after using your script.

The problem is with "Line Spacing"

Untitled.png

if line contains only few words, above problem occurs.

so if u can  change your script to do following things, it will very helpfull

for example, if Line contains "25 to 50 letters" set font size to "140 px".& Set Line Spacing to "250"

if Line contains "51 to 75 letters" set font size to "110 px" & Set Line Spacing to "170"

if Line contains "76 to 100 letters" set font size to "80 px" & Set Line Spacing to "130"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

& if can you do following thing with your script

align Text layer center to the canvas

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

You can try this:

#target photoshop

var doc = activeDocument;

var txtLayer1 = doc.layers.getByName('Text1');

var txtLayer2 = doc.layers.getByName('Text2');

var txtLayer3 = doc.layers.getByName('Text3');

setSize (txtLayer1)

setSize (txtLayer2)

setSize (txtLayer2)

function setSize(layer){

    var vis = layer.visible

    doc.activeLayer = layer;

    try{

        if(layer.textItem.contents.length<51){

            layer.textItem.size = 140

            layer.textItem.leading = 140   

            alignLayer ();

            }

        else if(layer.textItem.contents.length<76){

            layer.textItem.size = 110

            layer.textItem.leading = 110

            alignLayer ();

            }

        else{

            layer.textItem.size = 80

            layer.textItem.leading = 80

            alignLayer ();

            }

        }

    catch(e){}

    layer.visible = vis

    }

function alignLayer(){

    var idslct = charIDToTypeID( "slct" );

        var desc2 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref1 = new ActionReference();

            var idLyr = charIDToTypeID( "Lyr " );

            ref1.putName( idLyr, "Background" );

        desc2.putReference( idnull, ref1 );

        var idselectionModifier = stringIDToTypeID( "selectionModifier" );

        var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );

        var idaddToSelection = stringIDToTypeID( "addToSelection" );

        desc2.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );

        var idMkVs = charIDToTypeID( "MkVs" );

        desc2.putBoolean( idMkVs, false );

        var idLyrI = charIDToTypeID( "LyrI" );

            var list1 = new ActionList();

            list1.putInteger( 1 );

            list1.putInteger( 2 );

        desc2.putList( idLyrI, list1 );

    executeAction( idslct, desc2, DialogModes.NO );

    var idAlgn = charIDToTypeID( "Algn" );

        var desc4 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref2 = new ActionReference();

            var idLyr = charIDToTypeID( "Lyr " );

            var idOrdn = charIDToTypeID( "Ordn" );

            var idTrgt = charIDToTypeID( "Trgt" );

            ref2.putEnumerated( idLyr, idOrdn, idTrgt );

        desc4.putReference( idnull, ref2 );

        var idUsng = charIDToTypeID( "Usng" );

        var idADSt = charIDToTypeID( "ADSt" );

        var idAdCH = charIDToTypeID( "AdCH" );

        desc4.putEnumerated( idUsng, idADSt, idAdCH );

    executeAction( idAlgn, desc4, DialogModes.NO );

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

hi, thank you for your response.

your script works very well.

however their is one problem with align.

the script is aligning, Text layer center to the canvas horizontally not vertically.

i mean script is centering textlayer horizontally, but it is not centering textlayer vertically.

thank you ones again for your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

Do you want it to center both horizontally and vertically? Or just vertically?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

This script will align in both directions.

#target photoshop

var doc = activeDocument;

var txtLayer1 = doc.layers.getByName('Text1');

var txtLayer2 = doc.layers.getByName('Text2');

var txtLayer3 = doc.layers.getByName('Text3');

setSize (txtLayer1)

setSize (txtLayer2)

setSize (txtLayer2)

function setSize(layer){

    var vis = layer.visible

    doc.activeLayer = layer;

    try{

        if(layer.textItem.contents.length<51){

            layer.textItem.size = 140

            layer.textItem.leading = 140   

            alignLayer ();

            }

        else if(layer.textItem.contents.length<76){

            layer.textItem.size = 110

            layer.textItem.leading = 110

            alignLayer ();

            }

        else{

            layer.textItem.size = 80

            layer.textItem.leading = 80

            alignLayer ();

            }

        }

    catch(e){}

    layer.visible = vis

    }

function alignLayer(){

    var idslct = charIDToTypeID( "slct" );

        var desc2 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref1 = new ActionReference();

            var idLyr = charIDToTypeID( "Lyr " );

            ref1.putName( idLyr, "Background" );

        desc2.putReference( idnull, ref1 );

        var idselectionModifier = stringIDToTypeID( "selectionModifier" );

        var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );

        var idaddToSelection = stringIDToTypeID( "addToSelection" );

        desc2.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );

        var idMkVs = charIDToTypeID( "MkVs" );

        desc2.putBoolean( idMkVs, false );

        var idLyrI = charIDToTypeID( "LyrI" );

            var list1 = new ActionList();

            list1.putInteger( 1 );

            list1.putInteger( 2 );

        desc2.putList( idLyrI, list1 );

    executeAction( idslct, desc2, DialogModes.NO );

    var idAlgn = charIDToTypeID( "Algn" );  //Align Horizontally

        var desc4 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref2 = new ActionReference();

            var idLyr = charIDToTypeID( "Lyr " );

            var idOrdn = charIDToTypeID( "Ordn" );

            var idTrgt = charIDToTypeID( "Trgt" );

            ref2.putEnumerated( idLyr, idOrdn, idTrgt );

        desc4.putReference( idnull, ref2 );

        var idUsng = charIDToTypeID( "Usng" );

        var idADSt = charIDToTypeID( "ADSt" );

        var idAdCH = charIDToTypeID( "AdCH" );

        desc4.putEnumerated( idUsng, idADSt, idAdCH );

    executeAction( idAlgn, desc4, DialogModes.NO );

   

    var idAlgn = charIDToTypeID( "Algn" );  //Align Vertically

        var desc3 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref1 = new ActionReference();

            var idLyr = charIDToTypeID( "Lyr " );

            var idOrdn = charIDToTypeID( "Ordn" );

            var idTrgt = charIDToTypeID( "Trgt" );

            ref1.putEnumerated( idLyr, idOrdn, idTrgt );

        desc3.putReference( idnull, ref1 );

        var idUsng = charIDToTypeID( "Usng" );

        var idADSt = charIDToTypeID( "ADSt" );

        var idAdCV = charIDToTypeID( "AdCV" );

        desc3.putEnumerated( idUsng, idADSt, idAdCV );

    executeAction( idAlgn, desc3, DialogModes.NO );   

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

thank you so so much.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2018 May 24, 2018

Copy link to clipboard

Copied

hello Chuck Uebele,

i got one problem,

Untitled.png

as you can see in above picture, some paragraph fit perfectly in textbox

2Untitled.png

& some does't,

i want the above image should look like this.

5Untitled.png

i want a script that can do following things :

if paragraph have 5 lines (like 1st image) then its ok ,do nothing , but

if paragraph have 4 lines (like 2nd image) then change layer's vertical location (Y: position)

ex. if layer's position are like this =

x: 900px

y: 500px

change its y: position to 520px.

so, 4 line paragraph look like 3rd image.

note: i have tried to center text layer both horizontally & vertically. it is not working. i mean i am not able to get result like 3rd image.

hope! you can help me.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2018 May 24, 2018

Copy link to clipboard

Copied

That's a hard one, as if you use a text box, I don't know of anyway to center just the text within the box. I've always used hard returns for new lines, when I need to center text like that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 24, 2018 May 24, 2018

Copy link to clipboard

Copied

https://forums.adobe.com/people/Chuck+Uebele

That's a hard one, as if you use a text box, I don't know of anyway to center just the text within the box. I've always used hard returns for new lines, when I need to center text like that.

To fully align the text in textbox, you can try this script

// Horizontal alignment

activeDocument.activeLayer.textItem.justification = Justification.CENTER;

// Vertical alignment

var r = new ActionReference(); 

r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));     

r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum")); 

var yy = 1; 

try { yy = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy")); } catch(e) {} 

var box_height = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getList(stringIDToTypeID("textShape")).getObjectValue(0).getObjectValue(stringIDToTypeID("bounds")).getDouble(stringIDToTypeID("bottom")); 

box_height = box_height*yy; 

app.preferences.rulerUnits = Units.POINTS;

var shift = (box_height - (activeDocument.activeLayer.bounds[3].value - activeDocument.activeLayer.bounds[1].value))/2;

activeDocument.activeLayer.textItem.baselineShift = -shift;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 25, 2018 May 25, 2018

Copy link to clipboard

Copied

thanx for your reply,

5Untitled.png

your script works. for 4line paragraph

but it didn't work for 5line paragraph

6Untitled.png

any idea ! what to do?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 25, 2018 May 25, 2018

Copy link to clipboard

Copied

Give please a simple file with an example of text that is not aligning

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 25, 2018 May 25, 2018

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 25, 2018 May 25, 2018

Copy link to clipboard

Copied

OK. Your file is not compatible with CS6, but nevertheless here's a new script that works in CC2018.

// Horizontal alignment 

activeDocument.activeLayer.textItem.justification = Justification.CENTER; 

 

// Vertical alignment 

var r = new ActionReference();   

r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textKey"));       

r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));   

 

var yy = 1;   

try { yy = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getObjectValue(stringIDToTypeID("transform")).getDouble(stringIDToTypeID("yy")); } catch(e) {}   

var d = executeActionGet(r).getObjectValue(stringIDToTypeID("textKey")).getList(stringIDToTypeID("textShape")).getObjectValue(0).getObjectValue(stringIDToTypeID("bounds"));   

var box_height = d.getDouble(stringIDToTypeID("bottom")) - d.getDouble(stringIDToTypeID("top"));   

 

box_height = box_height*yy;   

 

app.preferences.rulerUnits = Units.POINTS; 

 

var shift = (box_height - (activeDocument.activeLayer.bounds[3].value - activeDocument.activeLayer.bounds[1].value))/2; 

 

activeDocument.activeLayer.textItem.baselineShift = -shift; 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines