0

I will try to ask this question as best I can, but the problem is a challenging one.

I have a text box that one part of my script will change the text in. I want to code a boolean variable to check if that text fits on one line or not.

Ex.

 ___________________
|This returns true  |
|                   |
|___________________|

but

 ___________________
|This longer version|
|returns false      |
|___________________|

Character count won't work since I'm not using a monospaced font. Any ideas?

1 Answer 1

2

The best I could come up with was this:

After new text is applied...

  1. Check layer height
  2. Turn on "No Break"
  3. Remove all "\r"
  4. Check layer height again
    • If layer height is now less than it was before = multiline
    • Because if the text is already in a single line, step 2. and step 3. wouldn't affect the height.
  5. Rollback the history state if necessary.

var doc = app.activeDocument;
var layer = doc.activeLayer;

var historyRollback = doc.activeHistoryState;
var rulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.POINTS;

var height1 = size( layer )[1];
layer.textItem.noBreak = true;
layer.textItem.contents = layer.textItem.contents.replace(/\r/g,'');
var height2 = size( layer )[1];

var singleline = height1 > height2 ? false : true;

app.preferences.rulerUnits = rulerUnits;
doc.activeHistoryState = historyRollback;
doc.activeLayer = layer;

alert( singleline )

function size( layer ) {

    var bounds = layer.boundsNoEffects;
    var top = bounds[1].value;
    var right = bounds[2].value;
    var bottom = bounds[3].value;
    var left = bounds[0].value;
    var width = right - left;
    var height = bottom - top;

    return [ width, height ];

}
4
  • So I think that this might fail for what I'm trying to accomplish. In the example I gave, for instance, the longer version would not return a change in height after removing all of the spaces, since the photoshop textbox would simply wrap extra characters to the next line. It also won't notice a change in size for strings that are already one line. Both of those examples would return singleline = true in your code. Commented Nov 28, 2018 at 22:56
  • @RyanWalker, It's possible I overlooked something, but I couldn't reproduce these issues. There should be no wrapping of extra characters if "no break" is applied... So maybe for some reason it failed to do so in your file? I don't understand your point about noticing change for stings that are already one line. I made a test file and test code, which goes through all the layers and outputs results in the Javascript Console when you run it through the ESTK application. The code is here and the test file link is on line 3.
    – Joonas
    Commented Nov 29, 2018 at 0:19
  • I don't know what I did differently in my test (PEBKAC for sure), but this works now. ¯_(ツ)_/¯ Thanks for providing the test document! Commented Nov 29, 2018 at 0:27
  • @RyanWalker, if you still encounter this issue, you'll probably want to check each "step" separately to see where it goes wrong. Check if height1 and height 2 are what you expect. Check if "No break" was applied, check if line breaks were removed. You might even want to check if the input text is as it should be. It is still very possible there is some scenario that returns the wrong value and I just didn't think of it.
    – Joonas
    Commented Nov 29, 2018 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.