var doc, textRange;
doc = app.ActiveDoc;
textRange = doc.TextSelection;
applyTextColor (textRange, "Black", doc);
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);
}
}
@frameexpert - THANK YOU!!!!
I got the script working. Seems to do fine. Here is the full find and replace script in case anyone searches this thread:
main();
function main(){
var doc = app.ActiveDoc;
if(!doc.ObjectValid())
{
alert("No active document. Cannot continue.");
return;
}
FindAndReplaceColor(doc, doc.MainFlowInDoc, 'Red', 'Black') ;
}
function FindAndReplaceColor(doc, flow, findString, replaceString) {
// https://community.adobe.com/t5/framemaker-discussions/find-replace/td-p/3581679#4032650
// https://community.adobe.com/t5/framemaker-discussions/find-and-replace-text/td-p/14438481#M82917
// https://community.adobe.com/t5/framemaker-discussions/script-to-change-red-text-to-black/m-p/15293234
var tr = new TextRange();
var restoreTR, frame = 0;
var loopCounter = 0, replacementCounter = 0;
var findParams = new PropVals();
//if the flow object is not valid, assume the main flow
if(doc.ObjectValid() && !flow.ObjectValid()){
flow = doc.MainFlowInDoc;
}
//get the first text frame in the flow, a starting point to
//find the first paragraph
if(flow.ObjectValid()){
frame = flow.FirstTextFrameInFlow;
}
//At this point, if we don't have a frame object, might as well abort.
if(!frame.ObjectValid()){
Alert("Could not find a starting point for the search. Cannot continue." , Constants.FF_ALERT_CONTINUE_WARN);
return replacementCounter;
}
//store the original text selection as an amenity to restore after the action.
restoreTR = doc.TextSelection;
//now, set up the starting text range as the very beginning
//of the flow. We'll move straight from beginning to end.
tr.beg.obj = tr.end.obj = frame.FirstPgf;
tr.beg.offset = tr.end.offset = 0;
//set up our find parameters. We want to configure it to look
//for the color format. We don't need
//the find to wrap because we are controlling the flow from
//beginning to end.
findParams = AllocatePropVals(1);
var color = doc.GetNamedColor (findString);
findParams[0].propIdent.num = Constants.FP_Color;
findParams[0].propVal.valType = Constants.FT_Id;
findParams[0].propVal.obj = color;
//initialize the errno global, which will be used to
//track the progress of the find and replace
FA_errno = Constants.FE_Success;
//and do an initial find to get started.
tr = doc.Find(tr.beg, findParams);
//now, run the find and replace loop as long as we keep finding things.
//The loop counter is just an emergency back door in case something
//goes critically wrong and causes an endless loop.
while(FA_errno === Constants.FE_Success && loopCounter++ < 1000){
//set up the text range to change the color
doc.TextSelection = tr;
//clear it - Uncomment this line to test if the search is finding the correct items.
//activeDoc.Clear(0);
// Change the color:
applyTextColor (tr, replaceString, doc);
//increment our return counter
if(FA_errno === Constants.FE_Success){
replacementCounter++;
}
//...and find the next instance. We'll reset FA_errno again just in case
//something screwy happened while we were replacing text.
FA_errno = Constants.FE_Success;
tr = doc.Find(tr.beg, findParams);
}
//we're done. Restore the document to it's original area of display
doc.TextSelection = restoreTR;
doc.ScrollToText(restoreTR);
return replacementCounter;
} // End FindAndReplaceString
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);
}
} // End applyTextColor