gpt4 book ai didi

javascript - 使用 indesign javascript 查找下一个和上一个

转载 作者:行者123 更新时间:2023-12-01 04:05:55 24 4
gpt4 key购买 nike

我想从 Indesign 中的事件光标点查找并选择下一个结果。

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

如果可能的话,我还想从光标处找到上一个结果。

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

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

最佳答案

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" );

关于javascript - 使用 indesign javascript 查找下一个和上一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41824622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com