Skip to main content
dublove
Legend
Jul 1, 2025 8:12 AM UTC
Answered

Is there any way to apply different object styles according to the number of lines in the caption?

Use scripts to apply different object styles depending on the number of lines.

For example:
In the case of two or more lines, "A object style" is applied.
In the case of a one-line note, apply "B object style"?

 

Thank you.

dublove
dubloveCorrect answer
Legend
Jul 1, 2025 6:58 PM UTC

Hi @m1b 

You can't use two findWhat's at the same time?
One alone is fine.

    var queries = [
        { findWhat: '\&', objectStyleName: 'Cap-C' },
        { findWhat: '\@', objectStyleName: 'Cap-C' },
    ];

 

13 replies

rob day
Community Expert
Community Expert
Jul 1, 2025 2:16 PM UTC

You can count the lines in a selection like this:

 
var l = app.documents[0].selection[0].lines;
alert("Selection has " + l.length + " lines")

 

Translate
dublove
dubloveAuthor
Legend
Jul 1, 2025 4:14 PM UTC

The code of m1b is nested a bit too much.
I'm still learning and researching ......

Translate
m1b
Community Expert
Community Expert
Jul 1, 2025 8:18 AM UTC

Hi @dublove We probably can make a script to do this, but it is rarely needed, because there are usually ways that the elements can be structured (especially as anchored objects) that handle this smoothly. If you don't think this is possible in your case, can you share a sample .indd showing the problem?

- Mark

Translate
dublove
dubloveAuthor
Legend
Jul 1, 2025 10:16 AM UTC

Hi @m1b 

I'm sorry. 
I finally found the original post and it looks like you helped fix it too.

https://community.adobe.com/t5/indesign-discussions/how-to-find-some-characters-with-grep-and-then-apply-object-style-to-that-textbox/m-p/15077037#M604623

 

I'm trying to determine, based on the number of lines in the caption:
Use A object style (left justified).
or B object style (center aligned).

As for the A and B styles, set them manually.

Translate