• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to find next/find previous with select text

New Here ,
Oct 08, 2024 Oct 08, 2024

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

560

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2024 Oct 09, 2024

Copy link to clipboard

Copied

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. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2024 Oct 09, 2024

Copy link to clipboard

Copied

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 09, 2024 Oct 09, 2024

Copy link to clipboard

Copied

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

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2024 Oct 09, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2024 Oct 10, 2024

Copy link to clipboard

Copied

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?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 10, 2024 Oct 10, 2024

Copy link to clipboard

Copied

Hi Peter, 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2024 Oct 10, 2024

Copy link to clipboard

Copied

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 10, 2024 Oct 10, 2024

Copy link to clipboard

Copied

Hi Uwe

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

Thank you

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 10, 2024 Oct 10, 2024

Copy link to clipboard

Copied

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:

 

Screen Shot 22.pngScreen Shot 23.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 10, 2024 Oct 10, 2024

Copy link to clipboard

Copied

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.

螢幕擷取畫面 2024-10-11 100303.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 11, 2024 Oct 11, 2024

Copy link to clipboard

Copied

@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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 13, 2024 Oct 13, 2024

Copy link to clipboard

Copied

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!!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 11, 2024 Oct 11, 2024

Copy link to clipboard

Copied

@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. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 13, 2024 Oct 13, 2024

Copy link to clipboard

Copied

Hi Robert,

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

THX!

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 13, 2024 Oct 13, 2024

Copy link to clipboard

Copied

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
}

 

 

Screen Shot 13.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 14, 2024 Oct 14, 2024

Copy link to clipboard

Copied

Hi @rob day 
The problem still occur in table, when the cursor go in the table. The script cannot search. 

 

image.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2024 Oct 14, 2024

Copy link to clipboard

Copied

quote

Hi @rob day 
The problem still occur in table, when the cursor go in the table. The script cannot search. 


By @ai8153

 

Because you can't select part of the table and text outside of the table. 

 

Also, you can't select parts of different cells - even if they're neighbors. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2024 Oct 14, 2024

Copy link to clipboard

Copied

Right, the number of different variables makes this difficult. The function works if the text is in a text frame pasted in the cell, but not with text directly in the cell. It would take a lot of work to sort through all of the possibilities.

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2024 Oct 14, 2024

Copy link to clipboard

Copied

quote

Right, the number of different variables makes this difficult. The function works if the text is in a text frame pasted in the cell, but not with text directly in the cell. It would take a lot of work to sort through all of the possibilities.


By @rob day

 

Not a problem with my ID-Tasker. 

 

Unless there are 100s of results to process - even free version can speed up the job quite significantly. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 18, 2024 Nov 18, 2024

Copy link to clipboard

Copied

LATEST

Hi all, 
Thanks for your comments!
I try to solve the the problem in the cell, is there any problem with my direction? Could you give me some advice?

 

var sel = app.activeDocument.selection;

if (sel.length > 0) {
    var insertionPoint = sel[0].insertionPoints[0];

    if (insertionPoint.hasOwnProperty("parentStory")) {
        if (insertionPoint.parent instanceof Cell) {
            findNextFromCell(insertionPoint.parent, insertionPoint.index);
        } else {
            findNextInStory(insertionPoint);
        }
    } else {
        alert("Selection is not within a text story or table.");
    }
} else {
    alert("No Text Selected");
}

function findNextInStory(insertionPoint) {
    var ps = insertionPoint.parentStory;
    var si = insertionPoint.index + 1;

    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findGrepPreferences.findWhat = "\\[|\\]";

    var myFound = ps.insertionPoints.itemByRange(si, ps.insertionPoints[-1].index).findGrep()[0];

    if (myFound.length > 0) {
        myFound[0].select();
    } else {
        alert("No more '[' ']' found in the text story.");
    }
}

function findNextFromCell(cell, startIndex) {
    var si = startIndex + 1;

    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findGrepPreferences.findWhat = "\\[|\\]";

    var myFound = cell.insertionPoints.itemByRange(si, cell.insertionPoints[-1].index).findGrep()[0];

    if (myFound.length > 0) {
        myFound[0].select();
    } else {
        var table = cell.parent;
        var nextCellIndex = cell.index + 1;

        if (nextCellIndex < table.cells.length) {
            findNextFromCell(table.cells[nextCellIndex], -1);
        } else {
            var nextParagraphStart = table.parent.insertionPoints[-1].index + 1;
            findNextInStory(table.parentStory.insertionPoints[nextParagraphStart]);
        }
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines