This question follows on from my previous question on the subject of GREP and find/replace functionality of InDesign. To summarize the situation, my InDesign document has 3 character styles applied to the text at various places:
bolditalic. The names of those character styles are self-explanatory.
I want to write a script that looks for text styled with any of the above character styles and wraps them in an appropriate HTML tag. For example, I want all text styled as
bold to be wrapped in the
<b>...</b> tags, and so on. In my previous question, the accepted answer by Jongware does an excellent job of explaining how to perform a manual Find/Replace using GREP. Now I wish to automate the process by writing a script to handle it. Here's my first attempt:
app.findGrepPreferences=app.changeGrepPreferences=null; app.findGrepPreferences.appliedCharacterStyle="bold"; app.changeGrepPreferences.changeTo="<b>$0</b>"; app.activeDocument.changeGrep();
This works except that it only wraps thew first character of the affected text in the given tags and erases the rest. Thus a text like "This is an example text." changed to "This is an e text." What am I doing wrong?
Never mind. I got it to work with just one extra line of code. Putting the modified code in here for anyone else to benefit:
app.findGrepPreferences=app.changeGrepPreferences=null; app.findGrepPreferences.findWhat=".+"; app.findGrepPreferences.appliedCharacterStyle="bold"; app.changeGrepPreferences.changeTo="<b>$0</b>"; app.activeDocument.changeGrep();
The extra line that was missing from my original snippet is the second one: