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);
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...
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
Copy link to clipboard
Copied
My bad, I should use line, instead of line, that's why I was getting an empty array.
Thank you takeyamaatsushi.