Highlighted

I have few regulat expression code that need to be find and replaced -Need extend script to automate

Explorer ,
Jul 13, 2020

Copy link to clipboard

Copied

 am copy pasting them all these expression one by one in sequence.

i have this extend script code, but where should i have to keep the search and replace text?

 

function FindAndReplaceString(activeDoc, flow, findString, replaceString, considerCase) {

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(activeDoc.ObjectValid() && !flow.ObjectValid()){
flow = activeDoc.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 = activeDoc.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 a string and perhaps be case sensitive. We don't need
//the find to wrap because we are controlling the flow from
//beginning to end.
findParams = AllocatePropVals(2);

findParams[0].propIdent.num = Constants.FS_FindText;
findParams[0].propVal.valType = Constants.FT_String;
findParams[0].propVal.sval = findString;

findParams[1].propIdent.num = Constants.FS_FindCustomizationFlags;
findParams[1].propVal.valType = Constants.FT_Integer;
if(considerCase){
findParams[1].propVal.ival = Constants.FF_FIND_CONSIDER_CASE;
}
else{
findParams[1].propVal.ival = 0;
}

//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 = activeDoc.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 clear the original text
activeDoc.TextSelection = tr;

//clear it
activeDoc.Clear(0);

//insert the new text. We should be able to use the
//original beginning of the text range where the old text was
//found.
activeDoc.AddText(tr.beg, replaceString);

//now, lets jimmy the text range in memory to place it directly
//after the string we just inserted, so the find picks back up after that.
tr.beg.offset += replaceString.length;

//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 = activeDoc.Find(tr.beg, findParams);
}

//we're done. Restore the document to it's original area of display
activeDoc.TextSelection = restoreTR;
activeDoc.ScrollToText(restoreTR);

return replacementCounter;
}

Views

19

Likes

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

I have few regulat expression code that need to be find and replaced -Need extend script to automate

Explorer ,
Jul 13, 2020

Copy link to clipboard

Copied

 am copy pasting them all these expression one by one in sequence.

i have this extend script code, but where should i have to keep the search and replace text?

 

function FindAndReplaceString(activeDoc, flow, findString, replaceString, considerCase) {

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(activeDoc.ObjectValid() && !flow.ObjectValid()){
flow = activeDoc.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 = activeDoc.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 a string and perhaps be case sensitive. We don't need
//the find to wrap because we are controlling the flow from
//beginning to end.
findParams = AllocatePropVals(2);

findParams[0].propIdent.num = Constants.FS_FindText;
findParams[0].propVal.valType = Constants.FT_String;
findParams[0].propVal.sval = findString;

findParams[1].propIdent.num = Constants.FS_FindCustomizationFlags;
findParams[1].propVal.valType = Constants.FT_Integer;
if(considerCase){
findParams[1].propVal.ival = Constants.FF_FIND_CONSIDER_CASE;
}
else{
findParams[1].propVal.ival = 0;
}

//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 = activeDoc.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 clear the original text
activeDoc.TextSelection = tr;

//clear it
activeDoc.Clear(0);

//insert the new text. We should be able to use the
//original beginning of the text range where the old text was
//found.
activeDoc.AddText(tr.beg, replaceString);

//now, lets jimmy the text range in memory to place it directly
//after the string we just inserted, so the find picks back up after that.
tr.beg.offset += replaceString.length;

//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 = activeDoc.Find(tr.beg, findParams);
}

//we're done. Restore the document to it's original area of display
activeDoc.TextSelection = restoreTR;
activeDoc.ScrollToText(restoreTR);

return replacementCounter;
}

Views

20

Likes

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
Jul 13, 2020 0

Have something to add?

Join the conversation