Skip to main content
dublove
Legend
July 1, 2025
Answered

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

  • July 1, 2025
  • 2 replies
  • 654 views

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.

Correct answer dublove

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' },
    ];

 

2 replies

rob day
Community Expert
Community Expert
July 1, 2025

You can count the lines in a selection like this:

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

 

dublove
dubloveAuthor
Legend
July 1, 2025

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

m1b
Community Expert
Community Expert
July 1, 2025

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

dublove
dubloveAuthor
Legend
July 1, 2025

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.