Skip navigation
Currently Being Moderated

How do I find character styles applied to a particular paragraph

May 10, 2012 4:15 AM

I have to loop through the paragraphs in my InDesign document, and for each one, see what character styles are applied in the paragraph. For a given paragraph, how do I locate character styles applied to the paragraph? Thank you very much.

 

Rick Quatro

 
Replies
  • Currently Being Moderated
    May 10, 2012 8:23 AM   in reply to frameexpert

    Hmmm, not-so-easy. Thinking out loud:

     

    All "Text" objects in general have a property "appliedCharacterStyle", but inquiring this (and all other properties) when there are a multiple applied to the item you are inquiring -- in your case, per paragraph -- will result in either a (null) value or just pop up an error message.

     

    Try looping over its TextStyleRanges. This has a couple of drawbacks: first, you'll visit each and every sort of override, regardless of how it's applied; and second, I believe this property in its current incarnation suffers from a eeny weeny tiny bug, where the final text style range will happily "leave" the current paragraph and continue into the next one if the formatting is unchanged.

     

    I'd have to try to be sure but I believe even an entirely empty character style will be reported as a separate 'range'. But note that the inverse is not true: not every separate range is a character style of its own; any manual formatting inside a character-styled text will appear to "split" the character styled range in two or more parts.

     
    |
    Mark as:
  • Currently Being Moderated
    May 10, 2012 9:52 AM   in reply to [Jongware]

    [Jongware] wrote:

     

    I believe this property in its current incarnation suffers from a eeny weeny tiny bug, where the final text style range will happily "leave" the current paragraph and continue into the next one if the formatting is unchanged.

     

    Anyone had a chance to test for this in CS6? (One can hope!)

     

    Another drawback with textStyleRanges: grep, nested and line style-applied character styles don't start new ones, so you'll also have to query the appliedNestedStyles property for each textStyleRange, and then possibly break each one down further, if you want to catch those.

     

    You might go the other direction and do a findText() for every character style. Doing a find for a character style applied by a grep style is a good way to make InDesign crash, though.

     

    Jeff

     
    |
    Mark as:
  • Currently Being Moderated
    May 10, 2012 2:47 PM   in reply to frameexpert

    You got it.

     

    To get the handle to an individual paragraph, right from your selection, you don't have to select it in its entirety.

     

    The following trick I learned from ... (Marc? Tomaxxi? Peter K.?) ... someone here on the forum: to get a list of all objects, whether they occur only once or lots of times, you don't have to store them into an array and test for each new one if it's already in there. Just just an associative array -- an object, in fact -- and have the "key" represent the object. The value is of no importance, actually. Or well, maybe it is you want to count occurrences.

    A quick way to inspect the members of your new object is to use "toSource".

     

     

    #target indesign

    var doc = app.documents[0];

     

    // Set a variable for the selected *paragraph*

    var pgf = app.selection[0].paragraphs[0];

     

    var textRanges = pgf.textStyleRanges;

    var textRange = null;

    var page = null;

     

    var charStyleList = {};

     

    for (var i = 0; i < textRanges.length; i += 1) {

              charStyleList[textRanges[i].appliedCharacterStyle.name] = true;

    }

     

    alert (charStyleList.toSource());

     


    .. Hey, Forum Maintenance, where did the Format Javascript button go!? I was going to use that!

     
    |
    Mark as:
  • Currently Being Moderated
    May 10, 2012 4:00 PM   in reply to [Jongware]

    Jong, you have to go back and edit the message for the advanced options to show before you can use the syntax highlighting. At least this is how it was described on the Illustrator scripting forum.

     
    |
    Mark as:
  • John Hawkinson
    5,572 posts
    Jun 25, 2009
    Currently Being Moderated
    May 10, 2012 4:46 PM   in reply to Larry G. Schneider

    Yes, the advanced editor problem is well known. Visit the General > Forum Comments forum to find the long long list of issues compiled by the forum admins. The implication is that this (and many others) will be fixed soonish.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points