Skip to main content
Mohamed Hameed21513110
Inspiring
December 30, 2021
Question

Modify code to enter new text but within the boundaries of the document itself

  • December 30, 2021
  • 1 reply
  • 1932 views

Welcome to the men of experience and knowledge
I have a code that writes a new text inside the document via Dilog using edittextbox
- But when writing texts with many lines, the text is created, but the line is very long, sometimes even reaching outside the document

- I want to modify this code so that the text is inserted like what was written inside the Dilog inside the edittextbox and the lines that were written are within the boundaries of the document itself

 

- I know there is an idea to edit that line
TextRef.contents = HW.text.replace(/\n/g,'\r');
In order to accept the insertion of lines in the same place they are inside the edittextbox

 

- This is the code that I relied on

dlg = new Window("dialog");   dlg.text = "Add New Text"; 
dlg.preferredSize  = [300, 150];

var HW = dlg.add("edittext", [0,0,500,300], "", {multiline: true, wantReturn: true}); HW.active=true;
HW.graphics.font = ScriptUI.newFont ("Arial", "Bold", 25);
Add=dlg.add("button", undefined, "Add Text"  );
Add.alignChildren = ['fill', 'fill'];

var docRef = app.activeDocument;
                var LayerRef = null;
                var TextRef = null;
                var pos = 0;
                

Add.onClick = function () {
LayerRef = docRef.artLayers.add();
                    LayerRef.kind = LayerKind.TEXT; 
                    TextRef = LayerRef.textItem;
                    TextRef.contents  = HW.text.replace(/\n/g,'\r'); 
                    TextRef.position = new Array(docRef.width/2, docRef.height/2);
                    preferences.rulerUnits = Units.CM;
                    TextRef.size = 45;
                    TextRef.kind = TextType.PARAGRAPHTEXT;
                    TextRef.font = 'Calibri-Bold';
                    TextRef.justification = Justification.CENTER;
                    TextRef.autoKerning = AutoKernType.METRICS;
                    TextRef.useAutoLeading = true;
dlg.close()
}
dlg.show();

 waiting for help

Thank You

This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
December 30, 2021

If it is paragraph text the text must fit within the paragraph boundaries. If the text layer is not a Paragraph  text will not flow automatically you will need to add the new line carriage return into the text to flow the text and  position it over the canvas and set the font size  so the text will fit on canvas.  Text is very complex.  Skills above my pay grade is required.  To deal with size  you may need to change the document resolution to 72 for you size calculation  I believe font size is based on points being  72 pixels. Your script seems to add the artlayer makes its a text layer  position it document center and set its content from the dialog.  Size 45 Calibri-Bold.   There is no checking to see if the text  Size 45 Calibri-Bold will fit in the Document. No  document size check no checking of the canvases resolution.  The only constraint I see   may be "edittext", [0,0,500,300], "" but then I do not know ScriptUI  or know if the document canvas bounds is the  Paragraph Bounds.  Text is not something I script.  

Here size 24pt is to large] where 18pt is fine.

 

 

 

JJMack
JJMack
Community Expert
Community Expert
December 30, 2021

Running you script it look like  the Document canvas size does become the Paragraph bounds and you positing  to the documents center so 3/4 of the paragraph area is off canvas.

 

Moving the layer more on canvas I see the bounds is not the canvas size perhaps its your dialog edit text areas

0,0 - 500,300 which is still taller than my canvas size

 

 

JJMack
Mohamed Hameed21513110
Inspiring
December 30, 2021

@JJMack 

Thank you for your interest and cooperation

I just want you to copy this article

If it is paragraph text the text must fit within the paragraph boundaries. If the text layer is not a Paragraph  text will not flow automatically you will need to add the new line carriage return into the text to flow the text and  position it over the canvas and set the font size  so the text will fit on canvas.  Text is very complex.  Skills above my pay grade is required.  To deal with size  you may need to change the document resolution to 72 for you size calculation  I believe font size is based on points being  72 pixels. Your script seems to add the artlayer makes its a text layer  position it document center and set its content from the dialog.  Size 45 Calibri-Bold.   There is no checking to see if the text  Size 45 Calibri-Bold will fit in the Document. No  document size check no checking of the canvases resolution.  The only constraint I see   may be "edittext", [0,0,500,300], "" but then I do not know ScriptUI  or know if the document canvas bounds is the  Paragraph Bounds.  Text is not something I script.  

Inside the edittextbox and click on ok and see what happens it will give you a long text that may reach outside the design limits
I want the text to fit within the borders of the document