select a text and apply no break using javascript
Copy link to clipboard
Copied
Hi ,
Please anyone give idea to apply no break for a letter at the end line in photoshop using javascript.
Explore related tutorials & articles
Copy link to clipboard
Copied
app.activeDocument.activeLayer.textItem.contents += "\u00A0"
app.activeDocument.activeLayer.textItem.contents = app.activeDocument.activeLayer.textItem.contents.replace (/\r/g,"\u00A0")
Copy link to clipboard
Copied
Hi Jazz,
thanks for your responce, here i shared snap now.
i need to apply no break for selected text using javascript, please your idea how to select the any work and the end of line,
Copy link to clipboard
Copied
Unfortunately, in Photoshop, the script cannot be executed while the text is in edit mode (including selection).
Copy link to clipboard
Copied
in generally we can replace space into non breaking space at the line?
Please refer this video,
Copy link to clipboard
Copied
The algorithm of this script is different from what you originally wrote - it removes the “orphaned” words (that is, it adds inextricable spaces after ALL short words, and not just at the end of the line).
To make such a scenario, you need to: get the text, break it into words (for example, using regular expressions), determine the length of each word, if it is shorter than three characters or matches a specific pattern, add an inextricable space to this word, collect all text in reverse order (excluding the usual spaces in those cases where there is already inextricable).
P.S. keep in mind that not all fonts contain an inextricable space character and you may get incorrect display of text in some cases.
Copy link to clipboard
Copied
Jazz,
I am begginner in javascript, please share some coding how to select words by using grep.
Copy link to clipboard
Copied
I do not have ready-made code for this solution.
The simplest (and very rude, without punctuation and various character separators) example:
var txt = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum'
words = txt.split (' ')
for (n=0; n<words.length; n++) {
if (words[n].length <= 3) words[n] += "\u00A0"
}
txt = (words.join (' ')).replace (/\u00A0 /g, "\u00A0")
* you can get and set text from app.activeDocument.activeLayer.textItem.contents
** you can break down into words in a variety of ways, from the trivial txt.split (' '), to txt.match (/\w+/g) and more complex constructions which you can easily find on the Internet or write it yourself after exploring the syntax of regular expressions
*** this is not photoshop-specific task, so you can find many existing javascript solutions: orphaned words js
Copy link to clipboard
Copied
Jazz,
non breaking space in Photoshop does not work, even i tried insert in glpyhs also. please share any alternative .
Copy link to clipboard
Copied
It works. Uncheck hyphenate in paragraph options.
That disable all hyphens for active layer:
#target photoshop
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('textKey'));
r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
var p = executeActionGet(r).getObjectValue(p).getList(s2t('paragraphStyleRange'))
var newParagraphList = new ActionList
for (var i = 0; i < p.count; i++) {
var paragraph = p.getObjectValue(i),
pStyle = paragraph.getObjectValue(s2t('paragraphStyle'));
pStyle.putBoolean(s2t('hyphenate'), false)
paragraph.putObject(s2t('paragraphStyle'), s2t('paragraphStyle'), pStyle)
newParagraphList.putObject(s2t('paragraphStyleRange'), paragraph)
}
(t = new ActionDescriptor).putList(s2t('paragraphStyleRange'), newParagraphList);
(r = new ActionReference).putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
(d = new ActionDescriptor).putReference(s2t('target'), r);
d.putObject(s2t('to'), s2t('textLayer'), t);
executeAction(s2t('set'), d, DialogModes.NO);
Copy link to clipboard
Copied
Jazz,
Is possible to select two words and apply no break?.
Copy link to clipboard
Copied
No. Photoshop is not a text editor. There are many limitations when working with text.
The "no break" function that you see in the menu is implemented through text styles. It works regardless of the state of the hyphenate option. But this approach will be much more difficult due to the need not only to break the text into words, but also to break the styles of the text into parts.
Copy link to clipboard
Copied
Jazz,
I am still facing difficults to add non breaking space, please help me out.
Advance thanks
Copy link to clipboard
Copied
I don’t have enough free time to write good code that solves your problem. Working with text style sheets in Photoshop is not a pleasant experience.
I wrote to the author a video to which you gave a link indicating this topic in the community. Perhaps he will answer you and try to help.
Copy link to clipboard
Copied
it removes the “orphaned” words (that is, it adds inextricable spaces after ALL short words, and not just at the end of the line).
Please refer this video,
Copy link to clipboard
Copied
If this is intended to be a feature request please post over on Adobe Photoshop Family
Copy link to clipboard
Copied
There is no audio explaining what is being displayed or what problem is being addressed of if the texts always fits within the resized text area, Most here will not be able to read you text for the language is not their's. You have not defined your meaning of “orphaned” words for you languages syntax. You seem to be highlighting characters at both the beginning and ends of lines in the video. I would expect the way Photoshop would flow text would be language dependent. You also dropped some JavaScript onto Photoshop that I believe may have modified the text paragraph in the document but you still resized the text box and heighlitted text. Is the script your solution to a problem? If so why not post the script. We would most likely understand your script better than your video.
Copy link to clipboard
Copied
Hi,
This is not my script, I have searched for my need on the internet. This video can be found on YouTube at the time, and this video fits my needs.
i need to avoid orphans words(1/2/3 letters) at the end of line, instead of i changed the space into non break space for all above 3 letter words, my lines also copied below. but unforualtly i unbale to get final results.
var doc = app.activeDocument;
var layer = doc.layers
for (var i=0;i<layer.length;i++){
if(layer[i].kind == "LayerKind.TEXT"){
var txt = layer[i].textItem.contents; //get layer texts
var words = txt.match(/\w+\s/g); // Split by words including space
for (n=0; n<words.length; n++){
if(words[n].length <= 3){ //Filter words below 3 letters
var find = words[n].match(/\s/g); // find the space in the word
var replce = "\u00A0"; // non-breaking space
layer[i].textItem.contents = layer[i].textItem.contents.replace(find,replce); change space into non-breaking space.
}
}
}
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
var doc = app.activeDocument;
var layer = doc.layers
for (var i=0;i<layer.length;i++){
if(layer[i].kind == "LayerKind.TEXT"){
var txt = layer[i].textItem.contents;
var words = txt.match(/\w+\s/g);
for (n=0; n<words.length; n++){
if(words[n].length <= 3){
var find = words[n].match(/\s/g);
var replce = "\u00A0";
layer[i].textItem.contents = layer[i].textItem.contents.replace(find,replce);
}
}
}
}
Hi,
i unable to get final results, please analyze my code and said any changes requires.
Thanks
Copy link to clipboard
Copied
You didn’t answer last time - do you have the hyphenation option turned off in the paragraph settings? If this option is enabled, then Photoshop ignores all types of separators, including non-breaking spaces.
Copy link to clipboard
Copied
Jazz, I make sure the hyphens are turned off, but I couldn't get the end result to what I implemented.
Copy link to clipboard
Copied
Apparently, you need to update the boundaries of the text box so that it refresh the contents.
Copy link to clipboard
Copied
"it removes the “orphaned” words"
Might I ask how text-heavy your Photoshop file is that you can't take care of this manually? If there is a lot of text, Photoshop is not the best application, and you may be much happier with the type controls that come with InDesign. Here are the steps in InDesign to avoid runts:
- Create a Character style called No Break with the character attibute of No Break
- Create a Paragraph style. In the GREP tab, apply the No Break character automatically to as many characters as you want to keep together at the end.
To keep the last eight characters together at the end of the paragraph, for example, you would use this code: .{8}$
To keep 10 characters together, use .{10}$
I understand that you may have no choice and have to use Photoshop, but it was never meant for text-heavy documents and is lacking a lot of features for good typography.
~ Jane
Copy link to clipboard
Copied
Reupload video, maybe these days someone else will help you with your problem.

