Copy link to clipboard
Copied
Hello All,
I have a multi line text item. I want to know the number of lines in a text item? How can I do that?
Note that every lines end with the shift+enter.
Example,
This is a
sample.
After line This is a there is (Shift + Enter).
Thanks for any help.
Whenever the user inputs Shift-Enter, Photoshop inserts an [EOT] (End Of Text) control character (\x03 or \u0003) in the string.
Also, in order to split the text according to multiple separators in only one call, it is necessary to use a regular expression instead of a string.
Try replacing:
var theArray = theText.split("\r");
with:
var theArray = theText.split(/[\u0003\r]/);
BTW, you can improve performance by explicitely requesting the textKey property of the current layer object.
Try using:
...r
Copy link to clipboard
Copied
Does this help?
// 2014, use it at your own risk;
#target "photoshop-70.032"
if (app.documents.length > 0) {
var theText = getText ();
var theArray = theText.split("\r");
alert (theArray.length)
};
////////////////////////////////////
////// get some stuff from the active layer //////
function getText () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var hasText = layerDesc.hasKey(stringIDToTypeID("textKey"));
if (hasText == true) {
var textDesc = layerDesc.getObjectValue(stringIDToTypeID('textKey'));
var theText = textDesc.getString(stringIDToTypeID("textKey"));
};
return theText
};
Copy link to clipboard
Copied
Hi c.pfaffenbichler, Thanks for the reply,
Yes I tried this
var theArray = theText.split("\r");
but this works only when there is only an Enter after end of lines or you can say when the line terminates with Enter key only.
This case fails when the line terminates with Shift+Enter key.
Copy link to clipboard
Copied
I used the text you had posted in the original post and it seemed to work.
Copy link to clipboard
Copied
It will work when we terminate the line with Enter key.
It isn't working in the following case,
- Create a point type text item on the photoshop document.
- Write the line like "This is a" . Now press Shift+Enter key (Cursor will move to the next line).
- Write anything like "sample.".
- Now execute the script on this text item. It will return 1.
Copy link to clipboard
Copied
So try \n
Copy link to clipboard
Copied
\n is also not working.
Copy link to clipboard
Copied
Then I’m stumped.
Copy link to clipboard
Copied
Never mind..
Thanks for your patience and help..
I have something in my mind. Let's see if it works, I'll post it here if that works.
Copy link to clipboard
Copied
Best of luck and let us know if you solve the issue! (If no one else posts a solution …)
Copy link to clipboard
Copied
Whenever the user inputs Shift-Enter, Photoshop inserts an [EOT] (End Of Text) control character (\x03 or \u0003) in the string.
Also, in order to split the text according to multiple separators in only one call, it is necessary to use a regular expression instead of a string.
Try replacing:
var theArray = theText.split("\r");
with:
var theArray = theText.split(/[\u0003\r]/);
BTW, you can improve performance by explicitely requesting the textKey property of the current layer object.
Try using:
ref.putProperty( charIDToTypeID("Prpr"), stringIDToTypeID("textKey") );
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
instead of:
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
HTH...
Copy link to clipboard
Copied
That's great, I am looking for exactly like this solution. ![]()
And it is far far better than what I have in my mind.
Thanks for saving my time.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now