Skip to main content
Inspiring
February 27, 2019
Question

Creating a selection, then fitting a text box inside.

  • February 27, 2019
  • 1 reply
  • 1842 views

My end goal is a script that the user writes their own text into a new window pop up, then that text is positioned in the bottom 10% of the image.

Start Image:

Window Pop up :

Small Text Outcome:

Long Text Outcome:

This is where I am currently:

#target Photoshop

app.preferences.rulerUnits = Units.PIXELS;

app.preferences.typeUnits = TypeUnits.PIXELS;

var doc = app.activeDocument;

var hgt10 = doc.height / 10;

var FillColor = new SolidColor;

FillColor.rgb.hexValue = 'ff0000';

var newHgt = doc.height + hgt10

doc.resizeCanvas(doc.width, newHgt, AnchorPosition.TOPCENTER);

  var win = new Window('dialog', "Custom Text");

  var grp1 = win.add('group');

  grp1.alignChildren = "top";

  var txt = grp1.add('edittext', [0, 0, 300, 150], "Write your message", {multiline: true, scrolling: true});

  txt.active = true;

  var grp2 = win.add('group');

  var goBtn = grp2.add('button', undefined, "Run");

  grp2.add('button', undefined, "Cancel");

 

  var newLyr = doc.artLayers.add();

  newLyr.kind = LayerKind.TEXT;

  // CONTENTS

  var txtLyr = newLyr.textItem

  txtLyr.font = "ArialMT"

  txtLyr.contents = txt.text

  var uV = new UnitValue(20,'mm')

  txtLyr.size = UnitValue(25,'mm')

  txtLyr.color = FillColor;

  txtLyr.position = [25, 2200] // x, y - Need to fix position to fit in the lower 10%

  goBtn.onClick = function() {

    win.close();

  }

win.show();  

So the areas I would like some assistance with is:

1) Getting the text to be positioned in the new area (created by the resizeCanvas argument extending by 10%)

2) Adding line breaks and automatically sizing to fit a variety of length text files into the field.

Kind Regards,

This topic has been closed for replies.

1 reply

Chuck Uebele
Community Expert
February 27, 2019

I'm not sure there is a good way to do this. I ran across the same thing, and I had to resort to adding the line breaks manually, then applying the text, measuring it, then resizing it to fit in the area I wanted.

Inspiring
February 28, 2019

When you say you added line breaks manually, did you create an array within an if argument to create line breaks after x words/characters?

How did you go about that?

Chuck Uebele
Community Expert
February 28, 2019

I was creating a way to put information in about images into the image. The information was pulled from the images metadata. The users would fill in things like description, title, and other fields using using Bridge. With the UI that I made, the user could then construct what metadata they wanted to use. To add a line break within a metadata field, I would have them insert the carrot symbol "^", as it was something that was never actually used. So my script would creat an array by splitting the text wherever there was that symbol.