Skip to main content
Bedazzled532
Inspiring
December 26, 2022
Answered

Find and replace Object with text using GREP

  • December 26, 2022
  • 1 reply
  • 1736 views

Hi
I am working on Al-Quran which has many anchors to the text (red color text frame with text in the attached image)

I now want to replace the red frame with some other box. Say the blue box (shown in the image).

How do I do that with grep. I tried find and replace with ~a and ~c but that is deleting all the text inside the red text box.

I want the text to remain and change the box only. What would be the process using grep?

Thanks

This topic has been closed for replies.
Correct answer Peter Kahrel

Thanks for the effort.

 

I am getting an error message while running the script.  "Object is Invalid"

I have attached the screenshot.


The problem is that the background frame is anchored. Sorry, I should have mentioned that it should be floating, as it were, not anchored. You can simply copy and paste the frame that you placed, then it should work.

1 reply

Braniac
December 26, 2022

GREP finds text, not frames. Well, it can frames but only via text. But the solution is simple: change the object style that's applied to those boxes. If no specific object style was used, now's the time to create a style and apply the style.  You can't do that in the interface, but a small script can do it for you.

First, create an object style for the blue box (if you don't have it already). The script assumes blue box, you can use something else but then you need to change the name in the script.

Here's the script:

 

ostyle = app.documents[0].objectStyles.item('blue box');
app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '~a';
frames = app.documents[0].findGrep();
for (i = 0; i < frames.length; i++) {
  frames[i].pageItems[0].appliedObjectStyle = ostyle;
}

 

 

P.

Bedazzled532
Inspiring
December 27, 2022

Thank you so much Peter. 

This worked partly. Its my mistake, I should have mentioned it in my question.

The code is replacing other anchors also who doesnot have any object style applied to them or some other object style is applied.

In short. I want to replace all the RedsBox object style Text Frame with Blue Box. 
Thanks

Peter KahrelCorrect answer
Braniac
December 27, 2022

Thanks for the effort.

 

I am getting an error message while running the script.  "Object is Invalid"

I have attached the screenshot.


The problem is that the background frame is anchored. Sorry, I should have mentioned that it should be floating, as it were, not anchored. You can simply copy and paste the frame that you placed, then it should work.