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

Delete conditional text

Engaged ,
Oct 05, 2015 Oct 05, 2015

Copy link to clipboard

Copied

In my selected text having two conditional texts (red and green). I needs to remvoe the red conditional text from the selected text

How to do this

TOPICS
Scripting

Views

1.4K

Translate

Translate

Report

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 2 Correct answers

Guide , Oct 05, 2015 Oct 05, 2015

Try this:

(function removeConditionalText(/*Text*/tx,/*UIColor*/uiColor,/*bool=false*/STRICT)

// -------------------------------------

// Remove any text within tx having a uiColor condition applied.

// (If STRICT is true, do not affect text undergoing multiple conditions.)

{

    var ranges = tx.textStyleRanges,

        a = ranges.everyItem().getElements(),

        conds = ranges.everyItem().appliedConditions,

        i = a.length,

        t, c;

    while( i-- )

        {

        t = conds;

   

...

Votes

Translate

Translate
Community Expert , May 19, 2023 May 19, 2023

Hi @Chase29247725h4qr , In case Marc does not see your question /*Text*/ is an efficient way of commenting on the parameters—tells you the tx parameter needs to be a Text object. You could also write comments above the function, depends on your coding preferences, might be something like this where the comments are in green. Code between /* and */ gets commented out:

 

Screen Shot 26.png

 

Votes

Translate

Translate
Guide ,
Oct 05, 2015 Oct 05, 2015

Copy link to clipboard

Copied

Try this:

(function removeConditionalText(/*Text*/tx,/*UIColor*/uiColor,/*bool=false*/STRICT)

// -------------------------------------

// Remove any text within tx having a uiColor condition applied.

// (If STRICT is true, do not affect text undergoing multiple conditions.)

{

    var ranges = tx.textStyleRanges,

        a = ranges.everyItem().getElements(),

        conds = ranges.everyItem().appliedConditions,

        i = a.length,

        t, c;

    while( i-- )

        {

        t = conds;

        if( STRICT && t.length != 1 ) continue;

        while( (c=t.pop()) && !(c=c.indicatorColor==uiColor) );

        if( c ) a.remove();

        }

   

})(app.selection[0],UIColors.RED);

@+

Marc

Votes

Translate

Translate

Report

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
New Here ,
May 19, 2023 May 19, 2023

Copy link to clipboard

Copied

Marc, we are eight years away from this post but I have some questions. I am new to JavaScript and Indesign scripting in general. When you declared the function at the beginning of you script, the function is:

function removeConditionalText(/*Text*/tx,/*UIColor*/uiColor,/*bool=false*/STRICT


What does /*Text*/tx actually do?

It seems like a paramater, but I do not understand the '/' and the '*'. Are they operators?

Much appreciated, 

Chase

Votes

Translate

Translate

Report

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 ,
May 19, 2023 May 19, 2023

Copy link to clipboard

Copied

LATEST

Hi @Chase29247725h4qr , In case Marc does not see your question /*Text*/ is an efficient way of commenting on the parameters—tells you the tx parameter needs to be a Text object. You could also write comments above the function, depends on your coding preferences, might be something like this where the comments are in green. Code between /* and */ gets commented out:

 

Screen Shot 26.png

 

Votes

Translate

Translate

Report

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, 2015 Oct 05, 2015

Copy link to clipboard

Copied

Or maybe just this:

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.appliedConditions = ['red'];

app.selection[0].changeGrep();

Peter

Votes

Translate

Translate

Report

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
Guide ,
Oct 05, 2015 Oct 05, 2015

Copy link to clipboard

Copied

Maybe 😉

@+

Marc

Votes

Translate

Translate

Report

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
Engaged ,
Oct 05, 2015 Oct 05, 2015

Copy link to clipboard

Copied

Both working fine for me . Thanks lot.

Votes

Translate

Translate

Report

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 Beginner ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

Soory for opening up this old thread, but it doesn't work out for me.

When I try

 

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedConditions = ['red'];
app.selection[0].changeGrep();

 

I only receive "undefined is not an object" - I'm working with the 2021 release of InDesign.

I use the ExtendScript Toolkit, InDesign is selected as target and the file is saved as .jsx.

Do I miss something?

 

Votes

Translate

Translate

Report

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 ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

The script expects some selected text or a text frame. To target a document without selecting anything, change app.selection[0] to app.documents[0].

Votes

Translate

Translate

Report

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 Beginner ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

Thank you for the fast answer Peter.

Unfortunatly this neither worked out for me.

Maybe I'm doing something wrong.

So here is my scenario:

I've got several documents open and I'd like to delete in all documents the applied condition, while the text of the condition is preserved.

I only use the mentioned script, nothing more.

 

Votes

Translate

Translate

Report

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 ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

Until you get the script working, you can do a Find/Change for the condition and replace it with nothing. Then delete the condition. 

 

Aside: I could have sworn ID used to give a dialog box asking about what to do with the applied conditional text when you delete a condition. I know FrameMaker does. 

David Creamer: Community Expert (ACI and ACE 1995-2023)

Votes

Translate

Translate

Report

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 ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

To delete just the conditions in all open documents and leave intact the texts to which it applied, use this:

for (i = 0; i < app.documents.length; i++) {
  if (app.documents[i].conditions.item('red').isValid) {
    app.documents[i].conditions.item('red').remove();
  }
}

P.

Votes

Translate

Translate

Report

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 ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

blockstation said:

"I've got several documents open and I'd like to delete in all documents the applied condition, while the text of the condition is preserved."

 

Hi blockstation,

you want to do something entirely else than our OP here wanted. You want to remove conditions, he wanted to remove text where a specific condition was applied to.

So you need different code. It has nothing to do with a GREP Find/Replace action.

Conditions are properties of the document. And can be removed if you know the names.

 

Examples:

So you should be able to remove a condition named "foo" from the document like that:

app.documents[0].conditions.itemByName("foo").remove();

If you want to do that for all open documents:

app.documents.everyItem().conditions.itemByName("foo").remove();

 

If you want to remove all conditions from all open documents:

app.documents.everyItem().conditions.everyItem().remove();

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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 Beginner ,
Nov 23, 2021 Nov 23, 2021

Copy link to clipboard

Copied

Thank you very much, this was very helpful.

 

Votes

Translate

Translate

Report

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 Beginner ,
Nov 24, 2021 Nov 24, 2021

Copy link to clipboard

Copied

Just for reference, is it also possible to add a condition to all open documents?

Votes

Translate

Translate

Report

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 ,
Nov 24, 2021 Nov 24, 2021

Copy link to clipboard

Copied

Sure:

for (i = 0; i < app.documents.length; i++) {
  if (!app.documents[i].conditions.item('red').isValid) {
    app.documents[i].conditions.add({name: 'red'});
  }
}

Votes

Translate

Translate

Report

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