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

Script to search all text that is not specific color

Explorer ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

498

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 1 Correct answer

Community Expert , Jan 26, 2021 Jan 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 )

Votes

Translate

Translate
Community Expert ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

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 )

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
Explorer ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

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.

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 ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

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 
* @Param fill color of text to skip 
* @Param 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 
* @Param the document to add the condition to 
* @Param 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);
}

 

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 ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

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 )

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
Explorer ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

@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.

 

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 ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

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 
* @Param fill color of text to skip 
* @Param 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 
* @Param the document to add the condition to 
* @Param 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);
}

 

 

Screen Shot 14.pngScreen Shot 15.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 ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

Hi Uwe, I should have been more clear—the script is expecting the text fill to be a Swatch named "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
Community Expert ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

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 
* @Param the document to add the condition to 
* @Param 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
* @Param 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 
* @Param the document to add the condition to 
* @Param 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);
}

 

 

 

 

 

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
Explorer ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

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:

Screenshot_1.png

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

Screenshot_2.png

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

Screenshot_4.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 ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

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 )

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
Explorer ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

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)

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 ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

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

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 ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

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 )

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 ,
Jan 26, 2021 Jan 26, 2021

Copy link to clipboard

Copied

LATEST

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 )

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