Try changing this line:
d = app.activeDocument;
to:
d = app.selection[0];
Then, just select some text in the anchored object and run the script.
Really it could be made simpler, where you just select the anchored object and run a script without needing the GREP business. When I get a chance I might post that code (or see if you can figure it out yourself!)