InDesign’s formatting points to positions in the text. If you change the contents of the text, the format pointers don’t change. As a result, the text appears messed up. The way to deal with this is to change the formatting with some text tags, remove the formatting, apply your changes, and remove the tags.
Manually this would work as follows:
1. Find: (.+)
Find format: cyan
Change to: _c_$1_c_
Change format: Black swatch
2. Do the replacement that you had in your query.
3. Find: _c_.+?_c_
Find format: <nothing>
Change to: <nothing>
Change format: cyan swatch
Do the above steps for magenta and yellow, using the tags _m_ and _y_, respectively.
Finally, remove the tags:
Find: _[cmy]_
Change to: <nothing>
And clear the two format fields.
All this is captured by the script below.
Peter
//—————————————————————————————
app.findGrepPreferences = app.changeGrepPreferences = null;
changeColour (‘(.+)’, ‘_c_$1_c_’, ‘cyan’, ‘Black’);
changeColour (‘(.+)’, ‘_m_$1_m_’, ‘magenta’, ‘Black’);
changeColour (‘(.+)’, ‘_y_$1_y_’, ‘yellow’, ‘Black’);
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = ‘t(w+)t(w+)t(w+)t(w+)’;
app.changeGrepPreferences.changeTo = ‘t$1t$3t$2t$4’;
app.documents[0].changeGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;
changeColour (‘_c_.+?_c_’, ”, ”, ‘cyan’);
changeColour (‘_m_.+?_m_’, ”, ”, ‘magenta’);
changeColour (‘_y_.+?_y_’, ”, ”, ‘yellow’);
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = ‘_[cmy]_’;
app.changeGrepPreferences.changeTo = ”;
app.documents[0].changeGrep();
function changeColour (f, r, c1, c2) {
app.findGrepPreferences.findWhat = f;
app.changeGrepPreferences.changeTo = r;
app.findGrepPreferences.fillColor = c1;
app.changeGrepPreferences.fillColor = c2;
app.documents[0].changeGrep();
}
//—————————————————————————————