Copy link to clipboard
Copied
It isn't that complicated. You don't need `getGraphics` function if you already have the graphic some other way. You can move the `intersection` logic inside the `intersectionOfGraphicFrame` function. The whole thing—minus the checking and getting—boils down to this one operation:
graphic.parent.geometricBounds = [
graphic.geometricBounds[0] > graphic.parent.geometricBounds[0] ? graphic.geometricBounds[0] : graphic.parent.geometricBounds[0],
graphic.geometricBounds[1] > graphic.parent.g
...
Copy link to clipboard
Copied
Hi @dublove, like this ...
/**
* @file Intersect Graphic Frame.js
*
* Example, setting the selected graphics' frames size to match the intersection of their bounds.
*
* @author m1b
* @version 2025-09-20
* @discussion https://community.adobe.com/t5/indesign-discussions/i-only-want-to-shrink-the-image-frame-without-affecting-the-cropped-portion/m-p/15512777
*/
function main() {
var doc = app.activeDocument;
var graphics = getGraphics(doc.selection);
for (var i = 0; i < graphics.length; i++)
intersectionOfGraphicFrame(graphics[i]);
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Intersect Graphic Frames');
/**
* Returns graphics derived from `items`.
* @author m1b
* @version 2025-08-20
* @param {Array|PageItem} items - array or collection or page item.
*/
function getGraphics(items) {
var graphics = [];
if ('Array' === items.constructor.name) {
for (var i = 0; i < items.length; i++)
graphics = graphics.concat(getGraphics(items[i]));
}
else if (
items.hasOwnProperty('allGraphics')
&& items.allGraphics.length > 0
)
graphics = graphics.concat(items.allGraphics);
else if (
items.hasOwnProperty('allPageItems')
&& items.allPageItems.length > 0
)
graphics = graphics.concat(getGraphics(items.allPageItems));
else if ('Image' === items.constructor.name) {
graphics.push(items);
}
return graphics;
};
/**
* Sets the graphic's container's size to match the intersection of their bounds.
* @author m1b
* @version 2025-09-20
* @param {PageItem} graphic - a page item contained in another page item.
*/
function intersectionOfGraphicFrame(graphic) {
if (
!graphic
|| !graphic.parent
|| !graphic.parent.geometricBounds
)
return;
graphic.parent.geometricBounds = intersection(graphic.geometricBounds, graphic.parent.geometricBounds);
};
/**
* Returns the intersecting bounds of b0 and b1.
* @param {Array<Number>} b0 - a bounds array.
* @param {Array<Number>} b1 - a bounds array.
*/
function intersection(b0, b1) {
return [
b0[0] > b1[0] ? b0[0] : b1[0],
b0[1] > b1[1] ? b0[1] : b1[1],
b0[2] < b1[2] ? b0[2] : b1[2],
b0[3] < b1[3] ? b0[3] : b1[3],
];
};
Copy link to clipboard
Copied
Hi m1b.
Thank you very much.
I didn't expect it to be so complicated to implement.
I'm still experimenting, and it works best when combined with:
s.fit(FitOptions.FILL_PROPORTIONALLY);
s.fit(FitOptions.FRAME_TO_CONTENT);
If not handled properly, they will clash.
It might take some getting used to.
Copy link to clipboard
Copied
It isn't that complicated. You don't need `getGraphics` function if you already have the graphic some other way. You can move the `intersection` logic inside the `intersectionOfGraphicFrame` function. The whole thing—minus the checking and getting—boils down to this one operation:
graphic.parent.geometricBounds = [
graphic.geometricBounds[0] > graphic.parent.geometricBounds[0] ? graphic.geometricBounds[0] : graphic.parent.geometricBounds[0],
graphic.geometricBounds[1] > graphic.parent.geometricBounds[1] ? graphic.geometricBounds[1] : graphic.parent.geometricBounds[1],
graphic.geometricBounds[2] < graphic.parent.geometricBounds[2] ? graphic.geometricBounds[2] : graphic.parent.geometricBounds[2],
graphic.geometricBounds[3] < graphic.parent.geometricBounds[3] ? graphic.geometricBounds[3] : graphic.parent.geometricBounds[3],
];
The larger issue is you don't always do a very good job of giving us examples that cover all your needs. The code I wrote works perfectly on your example document. But now you are talking about the `fit` method—which has nothing to do with your example. I know it can be hard., but try to supply examples that cover all your cases.
- Mark
Copy link to clipboard
Copied
This is easier to understand.
It must be quite useful.
I just can't use too many features right now, or it'll get cluttered.
Copy link to clipboard
Copied
Okay, that's good. Using single-purpose functions—as many as you need—is normal programming. Just include them in an external file and forget about them. There is no clutter to your main script file.
- Mark
Copy link to clipboard
Copied
I've taken many detours.
I'll revise all my old code when I have time.
For instance, I often conflate the current selection with the final target of an operation, interspersing various transformations in between.
From now on, I'll strictly separate them into two distinct categories:
'item' represents the current selection, while 'sel' denotes the final target of the operation. 'sel' must be an object.
Copy link to clipboard
Copied
Find more inspiration, events, and resources on the new Adobe Community
Explore Now