Detecting Text Overflow?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

influencebydesign
Posts: 9
Joined: Wed Nov 30, 2016 3:55 am

Detecting Text Overflow?

Post by influencebydesign »

Is it possible to detect text overflow in a document?

What I'd like to do is write a script for proofing that checks all text boxes for overflowing text. This can be hard to detect visually, especially when the overflow begins at the end of a sentence.

If overflowing text is found, the script would select the first instance. If no instances are found, an alert would be displayed stating so.

Many thanks for any help you can provide!
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Detecting Text Overflow?

Post by Kukurykus »

You mean text in Paragraph Text, so not in Point Text field?
influencebydesign
Posts: 9
Joined: Wed Nov 30, 2016 3:55 am

Re: Detecting Text Overflow?

Post by influencebydesign »

That is correct. Paragraph text that overflows the textbox.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Detecting Text Overflow?

Post by Kukurykus »

For the single active text layer you may try following code:

Code: Select all

aHS = (aD = activeDocument).activeHistoryState
contents = (tI = aD.activeLayer.textItem).contents
tI.hyphenation = false, tI.kind = TextType.POINTTEXT
result = contents == tI.contents.replace(/\r/g, '')
aD.activeHistoryState = aHS, alert(result)
false for overflow, true if text is fully displayed / I didn't understand what you mean by first instance.
influencebydesign
Posts: 9
Joined: Wed Nov 30, 2016 3:55 am

Re: Detecting Text Overflow?

Post by influencebydesign »

Many thanks for the code.

By "first instance", I mean if there are multiple paragraph text layers in the document, I'd like to scan the document to check them all for text overflowing the bounds of the container. The script would select the first matching element it finds, or display a message if no matching elements are found. So the workflow would be, to run the script, find text overflowing layer, fix it manually, run the script, find the next text overflowing layer, fix it manually, etc.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Detecting Text Overflow?

Post by Kukurykus »

I basically understand you, but to get the details it will be easier if you upload a compressed document (into a rar file) for testing.
influencebydesign
Posts: 9
Joined: Wed Nov 30, 2016 3:55 am

Re: Detecting Text Overflow?

Post by influencebydesign »

I've attached an example file that has some text layers that overflow and some that don't. Additionally, there are a few additional layers and nested groups to simulate a common scenario.

Much thanks!
Attachments
sample.psd.zip
(27.22 KiB) Downloaded 131 times
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Detecting Text Overflow?

Post by Kukurykus »

It is going to turn off visibility of all layers that overflow Paragraph bounds. Then you can manually turn them on and edit:

Code: Select all

function rplc(v) {return v.contents.replace(/\r/g, ' ')}
(function(v){var lrs = v.layers; for(var i = 0; i < lrs.length;) {
	var lyr = lrs[i++]; if (lyr.typename == 'LayerSet') callee(lyr)
	else if(lyr.kind == 'LayerKind.TEXT') aHS = aD.activeHistoryState,
	cntnts = rplc((tI = lyr.textItem)), tI.hyphenation = false, tI.kind = TextType
	.POINTTEXT, rslt = cntnts == rplc(tI), aD.activeHistoryState = aHS, !rslt && lyr.visible = false
}})(aD = activeDocument)
influencebydesign
Posts: 9
Joined: Wed Nov 30, 2016 3:55 am

Re: Detecting Text Overflow?

Post by influencebydesign »

Many thanks for your help on this...

When I run the script I get a warning:

"Because the type object to be converted does not fit within its bounds some text will be deleted during conversion. Continue?"

Is there any way to prevent that warning?

Probably turning off the visibility of the overflowing layers isn't the best option because depending on the layout someone might not notice that the text is missing. The documents may also have nested layers with some layers already hidden so things might get confusing.

That's why I was thinking the script could select the first overflowing layer so that it could be resolved, or give an alert when no overflow issue are found.
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Detecting Text Overflow?

Post by Kukurykus »

Why almost everyone I met on occaction of similar warnings can't see in left bottom corner a checkbox with a "Don't Show Again" text? Is that info not understandable or it should be placed in other position of 'Prompt' dialog? Click that to turn off these popups (which can be restored from Preferences).

You're right the changed visibility is not best option for all scenarios, but I didn't know which one is yours, so used it to see if that is okay. And I hoped you may help yourself with simple Photoshop tricks to make sure you won't miss any text layer. When script process is finished in the Layers panel filter for type layers (while the Kind category is chosen), or Not Visible with Attribute category.

If you had already invisible text layers, before you run script, Create New Layer Comp (on Layer Comps panel) applying it only for Visibility. Then after you manage all invisible Type layers, Apply Layer Comp (what you can do any time during handling with those layers to make sure the layer state has changed).

To make it even simpler, for type layers that were originally hidden, but did not overflow text area (so the ones you wouldn't like to additionally review after done job), you can make below update to script. After you browse all invisible layers, to make corrections to them, apply original Layer Comp.

change:

Code: Select all

!rslt && lyr.visible = false
to:

Code: Select all

lyr.visible = !rslt