Skip to main content
Christopher Pickering
Known Participant
March 8, 2017
Question

How to search the contents of text frame anchored to the current story

  • March 8, 2017
  • 2 replies
  • 2790 views

Hi,

I have a multipage story with many anchored text frames. I'm trying to find a way to search the contents of all the anchored objects on my current page. I've only been able to do it by selecting all text frame objects on the current page and then search them. However, this include other text frames that may have a match I don't want.

However I'm not sure how this is even working as when I run

alert(app.activeWindow.activePage.pageItems.length)

the number of items returned is all the text frames on the page except the anchored ones.. in this case, 6 instead of 21.

Any ideas you have are appreciated!

Thanks!
Christopher

This topic has been closed for replies.

2 replies

Community Expert
March 9, 2017

Hi Christopher,

if you want a search through formatted text of all anchored text frames of your main story, you could build an array of formatted text objects and search this with GREP or TEXT Find/Replace like that:

1. Select some text or a text frame of your main story

2. Loop through the parentStory.textFrames.everyItem().texts.everyItem().getElements() array.

3. Now you could do a GREP or TEXT Find/Replace action on every instance found.

var sel = app.selection[0];

var mainStory = sel.parentStory;

var fomattedTextsOfAnchoredFrames = mainStory.textFrames.everyItem().texts.everyItem().getElements();

for(var n=0;n<fomattedTextsOfAnchoredFrames.length;n++)

{

doSomethingWithFormatted(fomattedTextsOfAnchoredFrames);

};

// Build a function that will do something with the formatted text of a text frame

function doSomethingWithFormatted(text)

{

// Do something reasonable here:

// … A GREP Find/Replace action perhaps …

// Not very reasonable, but maybe enlightening.

// Write the contents of the anchored text frames to the JavaScript Console of the ESTK

$.writeln(text.contents)

}

Regards,
Uwe

Trevor:
Legend
March 9, 2017

Hi Uwe,

Nice one,

That's normally going to be a better approach than using the Grep.

If one doesn't need to loop through the individual frames one could just do find / change on the whole collection.

var sel = app.selection[0]; 

var mainStory = sel.parentStory; 

var fomattedTextsOfAnchoredFrames = mainStory.textFrames.everyItem().texts.everyItem(); 

fomattedTextsOfAnchoredFrames.fillColor = app.activeDocument.swatches[5];

app.findTextPreferences.findWhat = 'd';

app.changeTextPreferences.changeTo = 'D';

fomattedTextsOfAnchoredFrames.changeText();

Regards

Trevor

Obi-wan Kenobi
Legend
March 9, 2017

Hi Uwe & Trevor,

Why mention the "main" story"?

Is this not enough?

var myDoc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = "~a";

myFound = myDoc.findGrep();

var F = myFound.length;

for ( var f = 0; f < F; f++ ) if ( myFound.textFrames[0].isValid ) {

    // Do something!

    app.findChangeTextOptions.includeMasterPages = false;

    app.findGrepPreferences = app.changeGrepPreferences = null;

    //…………………

    }

app.findGrepPreferences = app.changeGrepPreferences = null;

(^/) 

Trevor:
Legend
March 9, 2017

Grep with "~a"

Christopher Pickering
Known Participant
March 9, 2017

Thanks, that will find the anchor point in the main story, but how can I get to the contents of the anchored text frame?

Trevor:
Legend
March 9, 2017

You can actually us find text and use "^a"

The use find = myFinds.textFrames[0] then contents = find.isValid && find.contents