Adobe InDesign图片帧移动脚本异常:每次运行移动y轴553像素以下帧
问题分析与修复
问题原因
你的脚本出现不符合预期的移动行为,核心有两个错误:
判断条件基于修改后的位置:
你先修改了帧的geometricBounds,之后才用graphic.parent.geometricBounds[0] > 553判断是否移动Y轴。此时的geometricBounds[0]已经是修改后的顶部坐标,而非原始位置,导致原本不需要移动的帧被错误触发移动逻辑。move方法误用:
InDesign脚本中,frame.move([dx, dy])的作用是相对当前位置偏移dx和dy,而非移动到[dx, dy]绝对坐标。你原本想把帧移动到指定绝对位置,但实际执行的是偏移操作,导致位置完全混乱。
修复后的代码
#target indesign var currentDocument = app.activeDocument; var targetImagePath = "path\\to\\picture"; var newWidth = 22.408; var newHeight = 26; var positionX1 = 274; var positionX2 = 1655.408; var positionY = 950; var allGraphics = currentDocument.allGraphics; for (var i = 0; i < allGraphics.length; i++) { var graphic = allGraphics[i]; if (graphic.itemLink && graphic.itemLink.filePath === targetImagePath) { var selectedFrame = graphic.parent; // 提前保存原始位置的坐标 var originalTop = selectedFrame.geometricBounds[0]; var originalLeft = selectedFrame.geometricBounds[1]; // 修改帧尺寸,保留原始顶部和左侧位置 selectedFrame.geometricBounds = [ originalTop, originalLeft, originalTop + newHeight, originalLeft + newWidth ]; graphic.fit(FitOptions.PROPORTIONALLY); // 基于原始顶部坐标判断是否移动Y轴 if (originalTop > 553) { // 直接设置几何边界实现绝对定位 selectedFrame.geometricBounds = [ positionY, selectedFrame.geometricBounds[1], positionY + newHeight, selectedFrame.geometricBounds[3] ]; } // 基于原始左侧坐标调整X轴位置 if (originalLeft <= 320) { selectedFrame.geometricBounds = [ selectedFrame.geometricBounds[0], positionX1, selectedFrame.geometricBounds[2], positionX1 + newWidth ]; } else if (originalLeft > 1496) { selectedFrame.geometricBounds = [ selectedFrame.geometricBounds[0], positionX2, selectedFrame.geometricBounds[2], positionX2 + newWidth ]; } } }
关键修改说明
- 提前保存原始的
originalTop和originalLeft,所有判断逻辑基于原始位置,避免修改尺寸后干扰条件判断; - 放弃
move方法,直接通过设置geometricBounds实现绝对定位(InDesign的geometricBounds格式为[顶部, 左侧, 底部, 右侧]),更直观且不易出错; - 修改尺寸时保留原始顶部和左侧位置,确保后续位置调整的基准正确。
内容的提问来源于stack exchange,提问作者Ivan
相关产品推荐
相关产品推荐

