Skip to main content
MiodragZ
Inspiring
January 25, 2021
Answered

Script to search all text that is not specific color

  • January 25, 2021
  • 8 replies
  • 1011 views

Greetings could someone point me to script or how to make one that will search whole document for text that is not red, and i would like to add all such text to conditional text that at end of script i would want to be hidden.

 

Could someone aid me with this one?

 

Thanks.

This topic has been closed for replies.
Correct answer Laubender

One workaround for hiding whole tables that do not run through several text columns or text frames:

Put every table in its own text frame and anchor that frame to text.

Set conditional text to the anchor character and set the condition to "hide".

 

Regards,
Uwe Laubender

( ACP )

8 replies

LaubenderCommunity ExpertCorrect answer
Community Expert
January 26, 2021

One workaround for hiding whole tables that do not run through several text columns or text frames:

Put every table in its own text frame and anchor that frame to text.

Set conditional text to the anchor character and set the condition to "hide".

 

Regards,
Uwe Laubender

( ACP )

Community Expert
January 26, 2021

Hi Rob,

hiding a table is an issue in itself. Cannot be done with Conditional Text.

What can be done: Apply a condition to text inside a text cell and set the condition to hide the text.

Next step would be to set the height of a row to 0. And you cannot do this. Not in the UI, not by scripting.

Even if you set all cell insets to 0. That's simply impossible with InDesign versions higher than 7 or 7.5.

 

FWIW: Once it was possible with InDesign CS4 and CS5, I think; by scripting only.

 

Regards,
Uwe Laubender

( ACP )

rob day
Community Expert
Community Expert
January 26, 2021

Doesn’t look like conditional text works on tables, with or without a script.

Community Expert
January 26, 2021

OP said: "Greetings, GREP script is the best so far, but all scripts have same issue - paragraph marks get condition so after you hide condition some text gets merged together, while tables disapear:"

 

That's why I said:

"Be careful. Do all this on a duplicate of your document, because hide and later unhide a condition can cause issues to paragraph formatting if hidden and not hidden text is parted with an end-of-paragraph marker."

 

And that means: You cannot hide the paragraph markers.

Although this could lead to another issue: Empty paragraphs that are left overs.

I do not know a workaround for this.

 

Regards,
Uwe Laubender

( ACP )

MiodragZ
MiodragZAuthor
Inspiring
January 26, 2021

Workaround for empty paragraphs is easy.

 

Hide all second ^p in search of ^p^p (where paragraph mark (^p) is followed by another paragraph mark - meaning second one it is empty)

rob day
Community Expert
Community Expert
January 26, 2021

I looked more closely at the Grep approach and this seems to work—this is a scripted version of Uwe’s first post:

 

 

 

 

 

 

//the text fill color swatch to search for–change as needed:
var r = "Red"

var doc = app.documents.item(0);
applyFillCondition(doc.colors.itemByName(r), [makeCondition(doc, "RedText")]);
checkCondition([makeCondition(doc, "NotRedText")]);
var rc = doc.conditions.itemByName("RedText");
rc.remove();

/**
* Search for text with a specified fill color and apply a text condition 
* @9397041 the document to add the condition to 
* @9397041 condition name 
* @Return the new condition 
* 
*/
function applyFillCondition(fc, cc){
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.fillColor = fc;
    app.changeGrepPreferences.appliedConditions = cc;
    app.activeDocument.changeGrep()
}

/**
* Finds text with no condition applied and adds a new condition to the found text
* @9397041 the condition to apply 
* @Return void 
* 
*/
function checkCondition(cc){
    app.findGrepPreferences = NothingEnum.NOTHING;
    app.changeGrepPreferences = NothingEnum.NOTHING;
    app.findGrepPreferences.appliedConditions = [];
    app.changeGrepPreferences.appliedConditions = cc;
    app.activeDocument.changeGrep()
}

/**
* Makes a new text condition 
* @9397041 the document to add the condition to 
* @9397041 condition name 
* @Return the new condition 
* 
*/
function makeCondition(d, n){
    var nc;
    try {
        d.conditions.add({name:n});
    }catch(e) {
        nc = d.conditions.itemByName(n);
    } 
    return d.conditions.itemByName(n);
}

 

 

 

 

 

MiodragZ
MiodragZAuthor
Inspiring
January 26, 2021

Greetings, GREP script is the best so far, but all scripts have same issue - paragraph marks get condition so after you hide condition some text gets merged together, while tables disapear:

 

1 we have following situation:

2 after we apply script we get following result - observe end of paragraphs:

3 when I hide condition I get following result - observe how 1st and 3rd paragraph got merged and how entire table disapeared:

 

Community Expert
January 26, 2021

Hi Rob,

it's totally unclear what color "red" actually means in this context.

Could be 100% of a swatch named "red", could be something else.

 

It depends on the version of InDesign how we could tackle this.

With InDesign 2021 it should be possible to find a specific color with the new feature in the Find/Change dialog.

I did not script that yet.

 

With older versions you could go through all stories and loop textStyleRanges and test for property fillColor with value color named "red" if there is a range of percentages of "red" that should be found. I cannot tell what our OP expects, because there are "texts in a lot of colors". It's too early, I think, to suggest some code. What we really need is a typical sample document…

 

Regards,
Uwe Laubender

( ACP )

MiodragZ
MiodragZAuthor
Inspiring
January 26, 2021

@Laubender 

Greetings color Red means RGB 255,0,0.

 

@rob day 

Yours script is totaly in right direction. I tought it would be much more simple. Like:

 

for (any character or space) {

if (character color =! red) {

apply condition hiddentext;

}

}

condition hiddentext visible = false;

 

// for sake of number of interations, maybe it is wiser first to search for words and after it for individual characters.

// I asume that words hidden by condition would not be in new interations, but only what is left visible

------------

Currently yours script does not hide spaces of character color that is different than red. Other than that it works.

PS Please do exciuse my haiku coding.

 

rob day
Community Expert
Community Expert
January 26, 2021

Currently yours script does not hide spaces of character color that is different than red. Other than that it works.

 

You could check every character, but it could get slow. A find and change can also be scripted, but I’m not seeing a way to search for all the text that is not filled with red.

 

This loops thru the doc’s characters:

 

var sn = "Red"
var doc = app.documents.item(0);
applyCondition(sn, [makeCondition(doc, "NotRedText")])


/**
* Apply a text condition to text 
* @9397041 fill color of text to skip 
* @9397041 the condition to apply 
* @Return void 
* 
*/
function applyCondition(sn, cn){
    for(var i=0; i < doc.stories.length; i++){  
        var s = doc.stories.item(i);  
        for(var j=0; j < s.characters.length; j++){  
            var p=s.characters.item(j);  
            if (p.fillColor != doc.colors.itemByName(sn)) {
                p.appliedConditions = cn
            } 
        }  
    }  
}




/**
* Makes a new text condition 
* @9397041 the document to add the condition to 
* @9397041 condition name 
* @Return the new condition 
* 
*/
function makeCondition(d, n){
    var nc;
    try {
        d.conditions.add({name:n});
    }catch(e) {
        nc = d.conditions.itemByName(n);
    } 
    return d.conditions.itemByName(n);
}

 

 

rob day
Community Expert
Community Expert
January 26, 2021

I couldn’t figure out how to script a grep to add a text condition to everything but red filled text—I’m guessing Uwe might know how.

 

You can loop through the document words, which is probably slower, but this seems to work:

 

var sn = "Red"
var doc = app.documents.item(0);
applyCondition(sn, [makeCondition(doc, "NotRedText")])


/**
* Apply a text condition to text 
* @9397041 fill color of text to skip 
* @9397041 the condition to apply 
* @Return void 
* 
*/
function applyCondition(sn, cn){
    for(var i=0; i < doc.stories.length; i++){  
        var s = doc.stories.item(i);  
        for(var j=0; j < s.words.length; j++){  
            var p=s.words.item(j);  
            if (p.fillColor != doc.colors.itemByName(sn)) {
                p.appliedConditions = cn
            } 
        }  
    }  
}




/**
* Makes a new text condition 
* @9397041 the document to add the condition to 
* @9397041 condition name 
* @Return the new condition 
* 
*/
function makeCondition(d, n){
    var nc;
    try {
        d.conditions.add({name:n});
    }catch(e) {
        nc = d.conditions.itemByName(n);
    } 
    return d.conditions.itemByName(n);
}

 

Community Expert
January 25, 2021

Hi miodragz46273948,

I do not think you need a script for this task.

Do it in two steps:
[1] Search text that is actually in that color.

Apply a condition to the found text.

[2] Search for text that has no condition.

Apply a different condition that you will hide later.

 

Be careful. Do all this on a duplicate of your document, because hide and later unhide a condition can cause issues to paragraph formatting if hidden and not hidden text is parted with an end-of-paragraph marker.

 

Regards,
Uwe Laubender

( ACP )

MiodragZ
MiodragZAuthor
Inspiring
January 25, 2021

Greegtings, I have situation where I have huge number of files. Also files contain texts in a lot of colors.

 

I believe that this script would be easy to write, and I have a way to execute script to be applied on multiple files.

 

Also search method would make tables hidden - so it is not good solution, but thanks for yours interest to aid me.