Are the graphics anchored (either in line or not) objects?
If yes then you can change your Grep to include the anchored object marker ~a.
For example if you wanted to collect the next graphic within the same paragraph as the number 999 you could use something like.
app.findGrepPreferences = app.changeGrepPreferences = null; app.findGrepPreferences.findWhat = "999[^\r]*~a"; !app.properties.activeDocument && exit(); var doc = app.activeDocument, finds = doc.findGrep(true), l = finds.length, graphs = , c = 0; while (l--) graphs[c++] = finds[l].characters[-1].allGraphics;
The graphs array will contain all the relevant graphics.
@cschweden – additionally to that what Trevor said and posted:
You can look at every frame that is anchored in two different ways:
1. You can select it with the Text Tool. So consider it as a single character; a special character of course.
2. You can select the frame with the Selection Tool or the Direct Selection Tool. So now it is something like a rectangle, oval, polygon, text frame, that is anchored in the text.
If we do a GREP search, we look at the text level. So #1 will apply here. And happily GREP is providing a special notation for anchored objects (equal what they are): ~a
If one of these objects is found, a single character, it is indeed containing the fully anchored object.
You can easily find that out, if you do the following:
Have some text with an anchored object in it.
Select the anchored object with the text tool. That means select the special character that represents this object in the Story.
Now run the following code from the ESTK (ExtendScript Toolkit):
var myAnchoredThing = app.selection.texts.pageItems.getElements(); $.writeln(myAnchoredThing);
The result would be [object Rectangle] , if you anchored a rectangle.
In case you want to add an hyperlink to the container object of an image itself, you're done. You found the object you have to deal with. In case you want dig deeper and working with the image or the images, or the graphic or the graphics inside that container, you have to go a step further.
You now do a loop through the allGraphics array of that object. In case this rectangle is itself holding a group of images pasted inside, there may be more than one images or graphics inside you want to hyperlink:
Let me add: if the graphic is not part ot the same story where your code is found, you will have hard times to find the right graphic. Then you need a list of codes together with the names of the graphics to have a good starting point. Or if the the text frame that holds the code is grouped together with the graphic. Some unique rules of relationship between the code and the graphic are needed to write a reliable script.