Skip to main content
riegens
Inspiring
February 27, 2024
Answered

Change color in tables via "Find/Change"

  • February 27, 2024
  • 1 reply
  • 869 views

I have these tables throuout a catalogue of approx 300 pages. I need to change the grey from a 40% of a defined grey colour to 100% of a new beige colour. 

I have tried combining the 2 swatches, but I end op with the beige in 40% instead of 100%. I have tried Find/Change colour, but that did not change the colour in the table. It worked in all of the rest of the catalogues, just not in the tables.

Is there a way to use Find/Change or do I need to manually change the colour from 40% to 100% in every tables?

This topic has been closed for replies.
Correct answer danaken3

Gotcha — 

 

app.doScript(Main, undefined, undefined, UndoModes.ENTIRE_SCRIPT,"Run Script");
function Main(){

    //Declare variables
    var myDoc = app.activeDocument;
    var beigeSwatch = myDoc.swatches.itemByName("Beige");
    var myCells = myDoc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

    for (var cellCount = 0; cellCount < myCells.length; cellCount++) {
        var myCell = myCells[cellCount];

        //If cell fill tint is 40%, apply new fill color
        if (myCell.fillTint==40) {
            myCell.fillColor = beigeSwatch;
            myCell.fillTint = 100;
        }
    }
}

 

1 reply

danaken3
Participating Frequently
February 28, 2024

I'm assuming you do not have a cell style applied to those cells? (That would be the ideal way to set up these types of tables in the future.)

 

The following script will find all cells with a background tint of 40% (no matter what color) and apply the color swatch called "Beige". So before you run this script, you will need to make sure you have created a "Beige" color swatch, and do not save it inside any swatch folder.

app.doScript(Main, undefined, undefined, UndoModes.ENTIRE_SCRIPT,"Run Script");
function Main(){

    //Declare variables
    var myDoc = app.activeDocument;
    var beigeSwatch = myDoc.swatches.itemByName("Beige");

    // loop pages
    for (var pageCount = 0; myDoc.pages.length > pageCount; pageCount++) {
        var myPage = myDoc.pages[pageCount];

        // loop textframes
        for (var textFrameCount = 0; textFrameCount < myPage.textFrames.length; textFrameCount++) {
            var myTextFrame = myPage.textFrames[textFrameCount];

            // loop tables
            for (var tableCount = 0; tableCount < myTextFrame.tables.length; tableCount++) {
                var myTable = myTextFrame.tables[tableCount];

                // loop cells 
                for (var cellCount = 0; cellCount < myTable.cells.length; cellCount++) {
                    var myCell = myTable.cells[cellCount];

                    //If cell fill color is black, apply new fill color
                    if (myCell.fillTint==40) {
                        myCell.fillColor = beigeSwatch;
                        myCell.fillTint = 100;
                    }
                }
            }
        }
    }
}

If your 40% grey is actually saved as its own swatch (with the tint percentage defined as part of the swatch), I'll need to adjust the script. Just let me know if this doesn't work!

Robert at ID-Tasker
Legend
February 28, 2024

@danaken3

 

In case of doing something on ALL Text objects - you should NEVER iterate through TextFrames collection of the Document - you should iterate through Stories collection of the document - and then texts[0] if you want to make it more universal.

 

Also, you completely unnecessarily are looping through Pages in the most outside loop - TextFrames are also a collection of the Document.

 

Then, instead of those multiple loops - you could get a collection of all cells:

 

var myCells = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements()

 

Then iterate through this. 

 

But, as someone pointed out recently - you would have to do a separate getElements() on footnotes, etc. 

 

danaken3
danaken3Correct answer
Participating Frequently
February 29, 2024

Gotcha — 

 

app.doScript(Main, undefined, undefined, UndoModes.ENTIRE_SCRIPT,"Run Script");
function Main(){

    //Declare variables
    var myDoc = app.activeDocument;
    var beigeSwatch = myDoc.swatches.itemByName("Beige");
    var myCells = myDoc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

    for (var cellCount = 0; cellCount < myCells.length; cellCount++) {
        var myCell = myCells[cellCount];

        //If cell fill tint is 40%, apply new fill color
        if (myCell.fillTint==40) {
            myCell.fillColor = beigeSwatch;
            myCell.fillTint = 100;
        }
    }
}