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

Add text width from previous text line to text position

Community Beginner ,
Jun 23, 2018 Jun 23, 2018

Copy link to clipboard

Copied

Hi,

I don't know how to use value of the text width from previous line and add it to a current text line position.

Function createText() is creating a text and function txtPreWidth() should return the text width from previous line.

When I alert the txtPreWidth(), it's giving me the right value, but when I put it in createText() function I'm getting undefined.

I think it should work like this but it's not.

Maybe I should store the text width in an object and then I don't need to create another function.

This is the script:

function myScript(thisObj){
     function myScript_buildUI(thisObj){
         var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", undefined, {resizeable:true});
//================================================================================================================
var textBox = myPanel.add("edittext", undefined, "My text goes here!", {multiline:true}); 
    textBox.minimumSize = [300,100];

var myButton = myPanel.add("button",undefined,"Create Markers");
myButton.onClick = createMain;
//================================================================================================================

function createMain(){
var textBoxContents = textBox.text;
var lines = textBoxContents.split(/\n|\r/);

createText(lines,0);
}
//==================================================================

function txtPreWidth(line){ // This function should return value of text Width from previous text line.
    for (var i = 0; i < line.length; i++) {
        var textlayer = app.project.activeItem.layers.addText(line);
        var textlayer_TextProp = textlayer.property("ADBE Text Properties").property("ADBE Text Document");
        var textlayer_TextDocument = textlayer_TextProp.value;
            textlayer_TextDocument.fontSize = 100;
            textlayer_TextProp.setValue(textlayer_TextDocument);

  var txtWidth = (textlayer.sourceRectAtTime(0,false).width);

    textlayer.remove()

    return txtWidth;
    }
}
//================================================================

function createText(line, yPos){
var y = yPos;
    for (var i = 0; i < line.length; i++) {
        var textlayer = app.project.activeItem.layers.addText(line);
        var textlayer_TextProp = textlayer.property("ADBE Text Properties").property("ADBE Text Document");
        var textlayer_TextDocument = textlayer_TextProp.value;
            textlayer_TextDocument.fontSize = 100;
            textlayer_TextProp.setValue(textlayer_TextDocument);
        textlayer.moveToEnd();

  var txtWidth = (textlayer.sourceRectAtTime(0,false).width); // Text width from current line.

  y += 30 + txtWidth; // The script should use the text width from previes line and add it to it. That value is in txtWdith();

        textlayer.property("ADBE Transform Group").property("ADBE Position").setValue([200,y,0]);
    }
}
//================================================================================================================
        myPanel.layout.layout(true);
        myPanel.minimumSize = myPanel.size;

        myPanel.layout.resize();
        myPanel.onResizing = myPanel.onResize = function(){this.layout.resize()};


         return myPanel;
         }
        var myScriptPal = myScript_buildUI (thisObj);

        if((myScriptPal != null) && (myScriptPal instanceof Window)){
            myScriptPal.center();
            myScriptPal.show();
        }
}
myScript(this);

TOPICS
Scripting

Views

418

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 ,
Jun 24, 2018 Jun 24, 2018

Copy link to clipboard

Copied

Hi

        function txtPreWidth(line) { // This function should return value of text Width from previous text line.

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

                var textlayer = app.project.activeItem.layers.addText(line);

                var textlayer_TextProp = textlayer.property("ADBE Text Properties").property("ADBE Text Document");

                var textlayer_TextDocument = textlayer_TextProp.value;

                textlayer_TextDocument.fontSize = 100;

                textlayer_TextProp.setValue(textlayer_TextDocument);

                var txtWidth = (textlayer.sourceRectAtTime(0, false).width);

                textlayer.remove()

                return txtWidth;

            }

        }

this code return txtWidth when i=0 only

if you want to return about all lines,

        function txtPreWidth(line) { // This function should return value of text Width from previous text line. 

            var txtWidths = [];

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

                var textlayer = app.project.activeItem.layers.addText(line);

                var textlayer_TextProp = textlayer.property("ADBE Text Properties").property("ADBE Text Document");

                var textlayer_TextDocument = textlayer_TextProp.value;

                textlayer_TextDocument.fontSize = 100;

                textlayer_TextProp.setValue(textlayer_TextDocument);

                var txtWidth = (textlayer.sourceRectAtTime(0, false).width);

                textlayer.remove()

                txtWidths.push(txtWidth);

            }

            return txtWidths;     //Array

        }

or want to return previous line only,

        function txtPreWidth(line) { // This function should return value of text Width from previous text line. 

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

                var textlayer = app.project.activeItem.layers.addText(line);

                var textlayer_TextProp = textlayer.property("ADBE Text Properties").property("ADBE Text Document");

                var textlayer_TextDocument = textlayer_TextProp.value;

                textlayer_TextDocument.fontSize = 100;

                textlayer_TextProp.setValue(textlayer_TextDocument);

                var txtWidth = (textlayer.sourceRectAtTime(0, false).width);

                textlayer.remove()

                if(_______){     //is previous line check

                    return txtWidth;

                }

            }

            return 0;

        }

Sorry my poor English...

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 Beginner ,
Jun 24, 2018 Jun 24, 2018

Copy link to clipboard

Copied

Thank you for the fast reply takeyamaatsushi,

I need previous line only, to use it in createText function.

When I put "alert(txtPreWidth(line));" in createText function I don't get any value, it just create an empty text layer.

Is it possible to use if statement in the createText function to get the previous line value, or I have to create seperate function.

I'm totaly confused, I tried your third function but I don't know what tu put in "if(_______){     //is previous line check".

Can you please help me?

Thanks

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 Beginner ,
Jun 24, 2018 Jun 24, 2018

Copy link to clipboard

Copied

LATEST

My bad, I should use line, instead of line, that's why I was getting an empty array.

Thank you takeyamaatsushi.

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