Here is a complete script that should do what you want.
main ();
function main () {
var colorName, doc, tbl;
colorName = "Black";
doc = app.ActiveDoc;
if (doc.ObjectValid () === 1) {
tbl = doc.SelectedTbl;
if (tbl.ObjectValid () === 1) {
processSelectedTbl (tbl, doc, colorName);
}
else {
processSelection (doc.TextSelection, doc, colorName);
}
}
}
function processSelection (textRange, doc, colorName) {
var textList, count, i, tbl;
// Process the text range here.
applyTextColor (textRange, colorName, doc);
// See if the text range has any tables in it.
textList = doc.GetTextForRange (textRange, Constants.FTI_TblAnchor);
count = textList.length;
for (i = 0; i < count; i += 1) {
tbl = textList[i].obj;
processTbl (tbl, doc, colorName);
}
}
function processTbl (tbl, doc, colorName) {
var cell;
if (tbl.TblTitlePosition === Constants.FV_TBL_TITLE_ABOVE) {
processTitleOrCell (tbl, doc, colorName);
}
cell = tbl.FirstRowInTbl.FirstCellInRow;
while (cell.ObjectValid () === 1) {
if (cell.CellIsStraddled === 0) {
processTitleOrCell (cell, doc, colorName);
}
cell = cell.NextCellInTbl;
}
if (tbl.TblTitlePosition === Constants.FV_TBL_TITLE_BELOW) {
processTitleOrCell (tbl, doc, colorName);
}
}
function processSelectedTbl (tbl, doc, colorName) {
var cells, count, i;
// See if the title is selected.
if (tbl.TblTitleSelected === 1) {
processTitleOrCell (tbl, doc, colorName);
}
// Get all of the selected cells in the table.
cells = getSelectedCells (tbl);
// Process the cells.
count = cells.length;
for (i = 0; i < count; i += 1) {
processTitleOrCell (cells[i], doc, colorName);
}
}
function processTitleOrCell (obj, doc, colorName) {
// obj is either a table or cell object.
var pgf;
pgf = obj.FirstPgf;
while (pgf.ObjectValid () === 1) {
// Process the paragraph.
processPgf (pgf, doc, colorName);
pgf = pgf.NextPgfInFlow;
}
}
function applyTextColor (textRange, colorName, doc) {
var color, prop;
// Get the color object.
color = doc.GetNamedColor (colorName);
// Make sure the color exists in the document.
if (color.ObjectValid () === 1) {
// Make a property list containing the color.
prop = new PropVal ();
prop.propIdent.num = Constants.FP_Color;
prop.propVal.valType = Constants.FT_Id;
prop.propVal.obj = color;
// Apply the color to the text.
doc.SetTextPropVal (textRange, prop);
}
}
function processPgf (pgf, doc, colorName) {
var textRange;
// Process the paragraph.
textRange = new TextRange ();
textRange.beg.obj = textRange.end.obj = pgf;
textRange.beg.offset = 0;
textRange.end.offset = Constants.FV_OBJ_END_OFFSET;
applyTextColor (textRange, colorName, doc);
}
function getSelectedCells (tbl) {
var cells, row, bottomRow, leftCol, rightCol, cell;
// An array of table cell objects.
cells = [];
// Get the current table selection boundaries.
row = tbl.TopRowSelection;
bottomRow = tbl.BottomRowSelection;
leftCol = tbl.LeftColNum;
rightCol = tbl.RightColNum;
// Loop through the selected rows and pick up the selected cells.
while (row.ObjectValid () === 1) {
cell = row.FirstCellInRow;
while (cell.ObjectValid () === 1) {
if ((cell.CellColNum >= leftCol) && (cell.CellColNum <= rightCol)) {
// Ignore straddled cells; they contain no paragraphs.
if (cell.CellIsStraddled === 0) {
// The cell is within the selection; push it onto the array.
cells.push (cell);
}
}
cell = cell.NextCellInRow;
}
// If we are at the bottom selected row;
// break out of the loop.
if (row.id === bottomRow.id) {
break;
}
row = row.NextRowInTbl;
}
return cells;
}
... View more