问题
is there anyway in InDesign using Javascript to search for any text frames in a document that overlaps? I've been looking through a document with all the properties of TextFrame and can't find anything that might tell whether there's any overlapping boxes or not.
I don't know Javascript but do know Ruby so i understand bits of it.
回答1:
Each Page object has got a read only attribute textFrames
with alle textFrames on the page.
Each textFrame has got a method visibleBounds
which will return an array in the form of [x1,y1,x2,y2]
, so you can get the geometric bounds of this text frame.
So you need to iterate over all the textFrames of the page, get the bounds of each textFrame and then you must check if any of them overlaps wich each other.
回答2:
Here is a simple code to find text frames that overlap. It loops through all spreads in an active document and pauses to report an overlap. Check it out.
app.activeDocument.viewPreferences.rulerOrigin= RulerOrigin.SPREAD_ORIGIN;
//If you are going to work with pages, not spreads, change the line above to PAGE_ORIGIN;
for (a = 0; a < app.activeDocument.spreads.length; a ++) {
var pg = app.activeDocument.spreads [a];
for (b = 0 ; b < pg.textFrames.length; b ++) {
var r1 = pg.textFrames [b];
for (c = 0 ; c < pg.textFrames.length; c ++) {
var r2 = pg.textFrames [c];
var gb1 = r1.geometricBounds;
var gb2 = r2.geometricBounds;
if ((r1 != r2) &&
(gb1 [0] > gb2 [0] && gb1 [0] < gb2 [2] && gb1 [1] > gb2 [1] && gb1 [1] < gb2 [3]) ||
(gb1 [2] > gb2 [0] && gb1 [2] < gb2 [2] && gb1 [1] > gb2 [1] && gb1 [1] < gb2 [3]) ||
(gb1 [0] > gb2 [0] && gb1 [0] < gb2 [2] && gb1 [3] > gb2 [1] && gb1 [3] < gb2 [3]) ||
(gb1 [2] > gb2 [0] && gb1 [2] < gb2 [2] && gb1 [3] > gb2 [1] && gb1 [3] < gb2 [3])) {
r1.select ();
var cnf = confirm ("Text frames overlap. Continue searching?", true, "Overlapping text frames");
if (!cnf)
exit ();
}
}
}
}
来源:https://stackoverflow.com/questions/33231516/indesign-javascript-to-find-overlapping-textframes