Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to find number of lines in the text content?

Contributor ,
Oct 03, 2014 Oct 03, 2014

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.

TOPICS
Actions and scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
Oct 06, 2014 Oct 06, 2014

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

...
Translate
Adobe
Community Expert ,
Oct 04, 2014 Oct 04, 2014

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

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 05, 2014 Oct 05, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 05, 2014 Oct 05, 2014

I used the text you had posted in the original post and it seemed to work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 06, 2014 Oct 06, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2014 Oct 06, 2014

So try \n

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 06, 2014 Oct 06, 2014

\n is also not working.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2014 Oct 06, 2014

Then I’m stumped.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 06, 2014 Oct 06, 2014

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2014 Oct 06, 2014

Best of luck and let us know if you solve the issue! (If no one else posts a solution …)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 06, 2014 Oct 06, 2014

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...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 06, 2014 Oct 06, 2014
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines