0

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

What I have so far is this, but it finds and selects the first result from the beginning of document..

I also want to find the previous result from cursor, if it's possible.

var myDoc = app.activeDocument;  
app.findGrepPreferences = null;  
app.findGrepPreferences.findWhat="text";

var myResults = myDoc.findGrep();  
app.select(myResults[0]);  
ali haydar
  • 125
  • 10

2 Answers2

1
var main = function() {
    var doc = app.properties.activeDocument,
    selText, st, ftp = app.findTextPreferences.properties, found, n, currIndex, foundIndex, nextText;
    if ( !doc ) return;

    if ( 
        app.selection.length!=1 
        || typeof ( app.selection[0].properties.baselineShift)=="undefined"
        || (app.selection[0] instanceof TextFrame )
    ) {
        alert("Please select text then run the script again" );
        return;
    }

    app.findTextPreferences = null;


    selText = app.selection[0];
    st = selText.parentStory;

    app.findTextPreferences.properties = {
        findWhat : "BLA"
    };

    found = st.findText();
    n = found.length;

    if ( !n ) {
        alert("No results found, sorry." );
        return;
    }

    currIndex = app.selection[0].index;
    while ( n-- ) {
        foundIndex = found[n].index;
        if ( !nextText && foundIndex>currIndex ) {
            nextText = found[n];
        }
        else if (foundIndex>currIndex && foundIndex<nextText.index)  {
             nextText = found[n];
        }
        else {
            break;
        }
    }

    app.findTextPreferences.properties = ftp;

    if ( !nextText ) {
        alert("No further results, sorry." );
        return;
    }

    if ( nextText.parentTextFrames.length && nextText.parentTextFrames[0].parentPage ) {
        var c1 = nextText.characters[0].words[0].characters[0],
        cn = nextText.characters[-1].words[0].characters[-1];

        var data = 
        {
            geometricBounds:[c1.baseline, c1.horizontalOffset/1.25, c1.baseline-c1.pointSize, cn.endHorizontalOffset*1.25],
        };
        var rect = nextText.parentTextFrames[0].parentPage.rectangles.add(data);
        zoomObject ( rect );
        rect.remove();
    }
    app.select( nextText );
}

//Snippet designed by Dave Saunders at http://jsid.blogspot.fr/2006/01/zoom-in-on-object.html
function zoomObject(theObj) {
 try {
  var objBounds = theObj.geometricBounds;
 } catch (e) {
  throw "Object doesn't have bounds."
 }
 var ObjHeight = objBounds[2] - objBounds[0];
 var ObjWidth = objBounds[3] - objBounds[1];
 var myWindow = app.activeWindow;
 var pageBounds = myWindow.activePage.bounds;
 var PgeHeight = pageBounds[2] - pageBounds[0];
 var PgeWidth = pageBounds[3] - pageBounds[1];
 var hRatio = PgeHeight/ObjHeight;
 var wRatio = PgeWidth/ObjWidth;
 var zoomRatio = Math.min(hRatio, wRatio);
 app.select(theObj); // to make active the page that holds theObj
 myWindow.zoom(ZoomOptions.fitPage);
 myWindow.zoomPercentage = myWindow.zoomPercentage * zoomRatio;
 //exit() // Because there's no point in doing this if you don't exit to let the user see
}

var u;

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Jongware
  • 22,200
  • 8
  • 54
  • 100
Loic
  • 2,173
  • 10
  • 13
  • Thank you! It works. But if there are footnotes then not as precise.. for example, if I search a word, it first finds the string in a footnote on page 300, even though there is the same word on page 2 body text.. – ali haydar Jan 24 '17 at 11:43
  • The underlaying concept is that the script will look for the closer match after the selection starting point. So if you selected text on page 2 when there are two results, one in page 1, the other in page 300. Then the page 300 result will be shown instead of page 1. – Loic Jan 24 '17 at 13:23
  • thats correct, but if the page 300 result is in a footnote then it is shown even I select a text before the page 1 result.. – ali haydar Jan 24 '17 at 13:46
  • oh ok. Well I guess this is related to how InDesign deals with found text array. – Loic Jan 24 '17 at 13:52
  • is it possible to find the previous result with this script? – ali haydar Jan 25 '17 at 07:43
1

That would be…

var main = function() {
    var doc = app.properties.activeDocument,
    selText, st, ftp = app.findTextPreferences.properties, found, n, currIndex, foundIndex, nextText;
    if ( !doc ) return;

    if ( 
        app.selection.length!=1 
        || typeof ( app.selection[0].properties.baselineShift)=="undefined"
        || (app.selection[0] instanceof TextFrame )
    ) {
        alert("Please select text then run the script again" );
        return;
    }

    app.findTextPreferences = null;


    selText = app.selection[0];
    st = selText.parentStory;

    app.findTextPreferences.properties = {
        findWhat : "BLA"
    };

    found = st.findText();
    n = found.length;

    if ( !n ) {
        alert("No results found, sorry." );
        return;
    }

    currIndex = app.selection[0].index;
 
    while ( n-- ) {
        foundIndex = found[n].index;
  
        if ( !nextText && foundIndex<currIndex ) {
            nextText = found[n];
        }
        else if (foundIndex<currIndex && foundIndex>nextText.index)  {
             nextText = found[n];
        }
        else {
            break;
        }
    }

    app.findTextPreferences.properties = ftp;

    if ( !nextText ) {
        alert("No further results, sorry." );
        return;
    }

    if ( nextText.parentTextFrames.length && nextText.parentTextFrames[0].parentPage ) {

        var c1 = nextText.characters[0].words[0].characters[0],
        cn = nextText.characters[-1].words[0].characters[-1];

        var data = 
        {
            geometricBounds:[c1.baseline, c1.horizontalOffset/1.25, c1.baseline-c1.pointSize, cn.endHorizontalOffset*1.25],
        };
        var rect = nextText.parentTextFrames[0].parentPage.rectangles.add(data);
        zoomObject ( rect );
        rect.remove();
    }
    app.select( nextText );
}

//Snippet designed by Dave Saunders at http://jsid.blogspot.fr/2006/01/zoom-in-on-object.html
function zoomObject(theObj) {
 try {
  var objBounds = theObj.geometricBounds;
 } catch (e) {
  throw "Object doesn't have bounds."
 }
 var ObjHeight = objBounds[2] - objBounds[0];
 var ObjWidth = objBounds[3] - objBounds[1];
 var myWindow = app.activeWindow;
 var pageBounds = myWindow.activePage.bounds;
 var PgeHeight = pageBounds[2] - pageBounds[0];
 var PgeWidth = pageBounds[3] - pageBounds[1];
 var hRatio = PgeHeight/ObjHeight;
 var wRatio = PgeWidth/ObjWidth;
 var zoomRatio = Math.min(hRatio, wRatio);
 app.select(theObj); // to make active the page that holds theObj
 myWindow.zoom(ZoomOptions.fitPage);
 myWindow.zoomPercentage = myWindow.zoomPercentage * zoomRatio;
 //exit() // Because there's no point in doing this if you don't exit to let the user see
}

var u;

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );
Loic
  • 2,173
  • 10
  • 13
  • Thank you.. I write three BLA's, put my cursor at the end and run the script, it finds the last BLA. But if I run the script again, it doesn't find the previous one, says no further results.. – ali haydar Jan 25 '17 at 14:26