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
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;
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:
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
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
Copy link to clipboard
Copied
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:
Copy link to clipboard
Copied
Or maybe just this:
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.appliedConditions = ['red'];
app.selection[0].changeGrep();
Peter
Copy link to clipboard
Copied
Maybe š
@+
Marc
Copy link to clipboard
Copied
Both working fine for me . Thanks lot.
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?
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].
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.
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.
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.
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 )
Copy link to clipboard
Copied
Thank you very much, this was very helpful.
Copy link to clipboard
Copied
Just for reference, is it also possible to add a condition to all open documents?
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'});
}
}