Ahh, challenges are my bread and butter. Preferably, also with a slice of Gouda and a dash of mustard.
Good thing you mentioned this:
… in my real-life case, anchored text boxes
because, indeed, it takes a bit more processing to search inside textframes that are inside textframes. See the nested loop that iterates one level deeper in the frame-that-is-on-a-page.
if (app.selection.length != 1 || !app.selection[0].hasOwnProperty(“baseline”))
{
alert (“Please put your text cursor in a (preferably) empty text frame!”);
exit(0);
}
app.findTextPreferences = null;
app.findTextPreferences.appliedCharacterStyle = app.activeDocument.characterStyles.item(“Cursief”);
for (p=0; p<app.activeDocument.pages.length; p++)
for (f=0; f<app.activeDocument.pages[p].textFrames.length; f++)
{
if (app.activeDocument.pages[p].textFrames[f].contents.length)
{
sourcelist = app.activeDocument.pages[p].textFrames[f].findText().reverse();
while (sourcelist.length > 0)
app.selection[0].parentStory.insertionPoints[-1].contents = sourcelist.pop().contents+”r”;
for (af=0; af<app.activeDocument.pages[p].textFrames[f].textFrames.length; af++)
sourcelist = app.activeDocument.pages[p].textFrames[f].textFrames[af].findText().reverse();
while (sourcelist.length > 0)
app.selection[0].parentStory.insertionPoints[-1].contents = sourcelist.pop().contents+”r”;
}
}
For some reason findText fails with some non-obvious error message if a text frame does not contain any text (“Object contains no text for find/change”, what's that supposed to mean?) — I expected it just to find nothing — so there is an additional test to check if the frame contains anything at all (the 'contents.length' line).
Does this come close to what you had in mind?