Skip to main content
Known Participant
June 9, 2023
Question

Response of FontStyle in TextStyleRange when Grep Style is active

  • June 9, 2023
  • 8 replies
  • 677 views

I bumped on one thing while scripting InDesign and just wanted to ask if anybody already know this issue since I think it is some kind of oversight of mine or just not working OK. I understand that this one is at least harder one... 

 

The issue is weird logic when you are checking the FontStyle of TextStyleRange (TextFrame -> ParentStory -> Paragraphs -> Item -> TextStyleRanges -> Item -> FontStyle) in situation when you have some active GrepStyle applied... just because the grep style application is not a part of TextStyleRange and if you want to check a FontStyle of that part of text, you will get different response in situation either grep styles applies on first character in that range or not....

 

So... main issue is that grep style is not included in textStyleRange or presentation of grep style attribute in font style field. Either grep style shoud be included in TextStyleRange, either if grep style is active - than it should not change the fontstyle information because it is not applied by a "human" 🙂 - maybe this would be soulution.

 

tnx. bye...

This topic has been closed for replies.

8 replies

mmargetaAuthor
Known Participant
August 1, 2023

Thank you all for replies. I see that topic was discussed in some previous post and this is just the confirmation of what I bumped on.

Thank you also for suggestions. As you probably guess - the goal is specific parsing of content in InDesign and returning it into database (a workflow between editors and layouters in our editorial system (CMS) called StoryEditor). In our case we are just looking for styles "bold", "italic", "bolditalic" which are represented with regular html tags in our CMS.

This is just working all well, but one of our clients reported this and when investigating we found this issue which is understandable in a way. The only thing is that my expectation on TextStyleRange functions is that it will return the situation what you see on page in InDesign but in this very situation - obviusly not.

Our workaround  is that in situations like this - when we are applying grep or nested or something else - we are using either fonts or font styles that are not a part of styles "bold", "italic", "bolditalic"... and then the applied grep or nested styles are not returned in DB and on second update are applied again in InDesign.

Tnx to all!

 

m1b
Community Expert
Community Expert
July 25, 2023

Hi @mmargeta, you may find this useful.

 

I've written a function that "embeds" nested grep styles, so that they are no longer grep styles, but are just individually-applied character styles. You can give the function either a TextFrame or Text object, or a ParagraphStyle. See example 1 and example 2 for these different modes (uncomment/comment as needed).

 

After "embedding" you can see that the text will then have the expected textStyleRanges. If you don't want to embed the grep styles, but just interrogate the textStyleRanges, then you could run this function on a duplicate. Hope that makes sense. Here what I get before and after running script on my demo text in screenshot below:

myText.textStyleRanges.length = 1
myText.textStyleRanges.length = 22

 

Please let me know if you find any bugs. It's freshly written and largely untested.

- Mark

 

 

 

/**
 * Examples of "embedding" nested grep styles.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/response-of-fontstyle-in-textstylerange-when-grep-style-is-active/m-p/13950327
 */
function main() {

    var doc = app.activeDocument;

    /* 
        Example 1: remove grep styles from selected text 
        - Select some text first.
    */

    var myText = doc.selection[0];
    // $.writeln('myText.textStyleRanges.length = ' + myText.textStyleRanges.length);
    embedGrepStyles(myText);
    // $.writeln('myText.textStyleRanges.length = ' + myText.textStyleRanges.length);

    /*
        Example 2: embed grep styles and remove from a paragraph style
        - Will target all text with the paragraph style applied.
    */

    // var myStyle = doc.paragraphStyles.itemByName('MyStyleWithGrep');
    // if (myStyle.isValid)
    //     embedGrepStyles(myStyle);


};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Embed Grep Styles');

/**
 * Embeds nested grep styles, ie. converts grep
 * styles to individually-applied character styles.
 * @author m1b
 * @version 2023-07-25
 * @param {TextFrame|Text|ParagraphStyle} target - the target of this function.
 */
function embedGrepStyles(target) {

    var removed = false,
        found,
        expressions,
        styles;

    if (target == undefined)
        return;

    if (target.constructor.name == 'TextFrame')
        target = target.texts[0];

    if (target.constructor.name == 'ParagraphStyle') {

        expressions = target.nestedGrepStyles.everyItem().grepExpression;
        styles = target.nestedGrepStyles.everyItem().appliedCharacterStyle;

        removeNestedGrepStyles(target);
        removed = true;

        app.findGrepPreferences = NothingEnum.NOTHING;
        app.changeGrepPreferences = NothingEnum.NOTHING;
        app.findGrepPreferences.appliedParagraphStyle = target;

        found = getDocument(target).findGrep();

    }

    else if (!target.hasOwnProperty('nestedGrepStyles'))
        return;

    if (expressions == undefined) {
        expressions = target.nestedGrepStyles.everyItem().grepExpression;
        styles = target.nestedGrepStyles.everyItem().appliedCharacterStyle;
    }

    found = found || [target];

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

        if (!removed)
            removeNestedGrepStyles(found[i]);

        // do each grep change
        for (var j = 0; j < expressions.length; j++) {
            app.findGrepPreferences = NothingEnum.NOTHING;
            app.changeGrepPreferences = NothingEnum.NOTHING;
            app.findGrepPreferences.findWhat = expressions[j];
            app.changeGrepPreferences.appliedCharacterStyle = styles[j];
            found[i].changeGrep();
        }

    }

};

/**
 * Returns the document of a thing.
 * @param {*} thing - the thing to get the document from.
 * @returns {Document}
 */
function getDocument(thing) {

    while (
        thing.constructor.name !== 'Document'
        && thing.hasOwnProperty('parent')
    )
        thing = thing.parent;

    if (thing.constructor.name == 'Document')
        return thing;

};

/**
 * Removes all nested grep styles.
 * @param {ParagraphStyle|Text} target - the thing to remove from.
 */
function removeNestedGrepStyles(target) {

    for (var i = target.nestedGrepStyles.length - 1; i >= 0; i--)
        target.nestedGrepStyles[i].remove();

};

 

Edit 2023-07-26: improved getDocument function. Added screen recording and console output to improve understanding.

Community Expert
July 20, 2023

Hm.

One way out could be to export to HTML and inspect the <span> tags.

But that's a lot of effort.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Community Expert
July 20, 2023

Hi @mmargeta ,

you could look into the properties of every single character to find the difference.

But even that could fail if there are some GREP styles applied that trump each other in a circular way.

Have to look up a sample for this, but as I recall we had a discussion about this very case in the old InDesign Scripting Forum about ten years ago. ( Dirk Becker was there and also Marc Auret ) .

 

In the meanwhile also see into more recent forum threads like this one:

 

TextStyleRange bug ? CC 2023 v18
@Robert Tkaczyk , Feb 01, 2023

https://community.adobe.com/t5/indesign-discussions/textstylerange-bug-cc-2023-v18/m-p/13547272

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Community Expert
July 20, 2023

Found two of the older discussions; but not the one that I remembered:

 

Nested styles and textStyleRanges
fivan, Jan 02, 2014
https://community.adobe.com/t5/indesign-discussions/nested-styles-and-textstyleranges/td-p/5704687


Get text selection and properties
poortip87, Dec 18, 2013
https://community.adobe.com/t5/indesign-discussions/get-text-selection-and-properties/td-p/5734157

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Legend
July 20, 2023

This one?

https://community.adobe.com/t5/indesign-discussions/waxrun/m-p/7044582#M371622

 

Anyway, isn't there a way to remove all styles and replace them with overrides - on a copy of the document or until undo? The TextStyleRuns might look better after that, but I never tried it. Or do I confuse that with a similar feature for bullets and numbering? Sorry for adding more questions than answers.

Peter Kahrel
Community Expert
Community Expert
July 20, 2023

Nope, that's not it. Recomposing makes no difference.

Legend
July 20, 2023

Ouch. You're right, I should have tried. While I'm reworking a similar script right now, that part of it has been offloaded to a plug-in where I compute my style runs from own criteria. Adding another test case.

Legend
July 20, 2023

Call recompose() and look again.

While it usually works without explicit call, composition is a deferred task and might just not have reached your text.

Peter Kahrel
Community Expert
Community Expert
July 19, 2023

Correct: formatting applied by nested styles (both grep and 'normal' nested styles) doesn't affect the textStyleRanges object. 

Community Expert
July 19, 2023

Not really sure what the solution is - or what all the script is you have is doing or your end goal. 

 

But off the top of my head not sure if this helps or not. But more examples and full explanation of what you need would be great.

 

var characterRange = myTextStyleRange.characters;
var firstCharacter = characterRange[0]; // Get the first character in the TextStyleRange

var actualFontStyle = {
  appliedFont: firstCharacter.appliedFont,
  fontStyle: firstCharacter.fontStyle,
  // Include other relevant font attributes if needed
};