找到下一个并找到以前的indesign javascript

时间:2017-01-24 09:33:55

标签: javascript adobe-indesign

我想在Indesign中找到并选择活动光标点的下一个结果。

到目前为止我所拥有的是它,但是它从文档的开头找到并选择了第一个结果。

如果可能的话,我还想从光标中找到以前的结果。

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

var myResults = myDoc.findGrep();  
app.select(myResults[0]);  

2 个答案:

答案 0 :(得分:1)

controller

答案 1 :(得分: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" );