Skip to main content
Participating Frequently
October 9, 2024
Question

How to find next/find previous with select text

  • October 9, 2024
  • 20 replies
  • 1443 views

I want to find and select the result from cursor point in Indesign.

 

app.findGrepPreferences = null;
app.findChangeGrepOptions.searchBackwards = false;
app.findGrepPreferences.findWhat = "\\[|\\]";
myFound = app.activeDocument.findGrep();

 

myFound[0].select();

 

#javascript #indesign

This topic has been closed for replies.

20 replies

Robert at ID-Tasker
Legend
October 11, 2024

@ai8153

 

Are you looking for a free / one time solution - or paid?

 

My tool will let you search in the current Story, Tables, InLine / Anchored Objects from the current InsertionPoint till the end of the Document - or in the specified range of pages. You could also limit the scope to the area or Layer(s).

 

But it's not free - and it's PC only. 

 

ai8153Author
Participating Frequently
October 13, 2024

Hi Robert,

I look for free. Because I want to improve my skill more than one time solution.

THX!

 

rob day
Community Expert
Community Expert
October 13, 2024

This might not be as easy as it seems—there are a lot of variables. You could create a function with my code that returns true when the last result is selected, and from there you could loop other stories, but stories are not necessarily in reading order:

 

var sel = app.activeDocument.selection
if (sel.length>0) {
	if (sel[0].hasOwnProperty("parentStory")) {
        //the story to search, the starting insertion point, the grep
        var gs = getNextGrep(sel[0].parentStory, sel[0].insertionPoints[-1], "\\[|\\]")
        if (gs){
            alert("End of Story. Go to next story")
        }
    } else {alert("No Text Selected")}
} else {alert("No Text Selected")}




/**
* Searches for the next instance of a Grep search 
* @ param ps, the story to search 
* @ param si, the starting insertion point index 
* @ param g, the Grep pattern 
* @ returns true if the last instance is selected 
* 
*/
function getNextGrep(ps, si, g){
    var b = false
    var f = []
    var ei = ps.texts[0].insertionPoints[-2];
    var tr = ps.insertionPoints.itemByRange(si,ei).texts[0]
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findGrepPreferences.findWhat = g;
    f = tr.findGrep()[0];
    if (f.length>0) {
        f[0].select();
    } else {
        b = true
    }
    return b
}

 

 

Community Expert
October 11, 2024

@ai8153 said: "It can only find the nearest one, but it cannot search continuously."

 

Hi @ai8153 ,

I thought from your code in your inital post, that you want to find the first instance only…

And I did not think about the case that your cursor could be positioned in a table text cell when you start the script.

 

Let's see into some cases:

[ 1 ] You want to find text in a table where the table special character is part of the story. Nothing's wrong with that. But that case needs a different approach and more code.

[ 2 ] Do you also regard anchored text frames as part of the story where you want to find text?

What about text frames in groups that are anchored to the story?

What about more complex cases with nested objects of any kind that can contain text frames?

[ 3 ] How about footnote texts? Do you regard footnote texts also as part of a story where you want to find text?

 

My thinking about all this revolves around the idea of selected text frames as scope of app.selection[0].findGrep() and not of text. And methods to determine if a found instance comes before or after an insertion point that is currently selected…

 

Regards,
Uwe Laubender
( Adobe Community Expert )

ai8153Author
Participating Frequently
October 13, 2024

Hi Uwe,

 

[1] I main search [ ], bullet point or use Grep to search in the story

[2] I just consider search in the same text frame. If it can do that it more perfect.

[3] I never use footnote. So I never consider on this.

 

Thx!!

 

rob day
Community Expert
Community Expert
October 10, 2024

Hi @ai8153 , Do you want to restore your starting selection? There‘s also this which gets the text from the cursor to the end of the story and uses that for the search:

 

var sel = app.activeDocument.selection
if (sel.length>0) {
	if (sel[0].hasOwnProperty("parentStory")) {
        var myFound = []
        var ps = sel[0].parentStory;
        var si = sel[0].insertionPoints[-1]
        var ei = ps.texts[0].insertionPoints[-2];
        //gets the text from the start of the selection to the end of the story
        var tr = ps.insertionPoints.itemByRange(si,ei).texts[0]
        app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
        app.findGrepPreferences.findWhat = "\\[|\\]";
        myFound = tr.findGrep()[0];
        if (myFound.length>0) {
            myFound[0].select();
        } 
    } else {alert("No Text Selected")}
} else {alert("No Text Selected")}

 

 

Before and after:

 

ai8153Author
Participating Frequently
October 11, 2024

Hi @rob day 

It's close to the effect I want to achieve, but a problem occurs when I go to the table. I don’t know why I can’t continue.

Community Expert
October 10, 2024

Hi @ai8153 ,

With the cursor just set or with a selected range of characters, you could expand the text selection to the end of the story first and then do findGrep() on the selection, the selected text. To expand the text selection:

app.select
( 
	app.selection[0].parentStory.texts[0].insertionPoints.itemByRange
	( 
		app.selection[0].insertionPoints[0] , 
		app.selection[0].parentStory.insertionPoints[-1]
	) 
);

 

All in all this:

app.findGrepPreferences = null;
app.findChangeGrepOptions.searchBackwards = false;

app.findGrepPreferences.findWhat = "\\[|\\]";

var originalSelection = app.selection[0];

// Select text from the current insertion point to the end of the story:
app.select
( 
	app.selection[0].parentStory.texts[0].insertionPoints.itemByRange
	( 
		app.selection[0].insertionPoints[0] , 
		app.selection[0].parentStory.insertionPoints[-1]
	) 
);

// Run findGrep() on the selection:
var myFound = app.selection[0].findGrep();

// Select the first instance found:
if( myFound.length > 0 )
{
	app.select( myFound[0] );
}else
{ 
	// Restore your original selection:
	app.select( originalSelection ) 
};

That would also find text in a table that is inserted after the current position of your cursor.

Note: The scope for findGrep(), the text selection, will not see into anchored text frames.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

ai8153Author
Participating Frequently
October 11, 2024

Hi Uwe

It can only find the nearest one, but it cannot search continuously.

Thank you

Peter Kahrel
Community Expert
Community Expert
October 10, 2024

So you're looking for [ and ], then want to do something to text bracketed by [ and ]

You should probably look for "\\[.*?\\]" to capture those bracketed texts. Is there any reason why you don't do that?

 

ai8153Author
Participating Frequently
October 11, 2024

Hi Peter, 

I need to looking for "[  or ]" continually, so that why I need to use "\\[|\\].
Thank you. 

Peter Kahrel
Community Expert
Community Expert
October 9, 2024

I want to find and select the result from cursor point in Indesign.

 

from the cursor point to where? The end of the story?

ai8153Author
Participating Frequently
October 10, 2024

From the most recent cursor position. (Both of text and table)

Robert at ID-Tasker
Legend
October 9, 2024

You need to select text from cursor to end of the Story and then run search on the selection - not the app.

 

If you want to search from current page to the end of the Document - it's a bit more complicated. 

 

ai8153Author
Participating Frequently
October 10, 2024

No need from the end of story. Just the nearest result.