I’ve searched extensively but haven’t found a solution. Does anyone have a script that retrieves properties, such as height and width, of the page item directly below the currently selected object? Ideally, the script would use geometric bounds to determine which object is below, focusing on those whose center point is directly beneath the selected rectangle.
I understand this is a complex request, so no worries if no one has a solution.
In case anybody needs it, this seems to work well. It returns true if the nearest object is less that 101 pts wide/tall and exists beneath the selected page item:
tell application id "com.adobe.InDesign"
activate
set myFoundFrame to false
tell active document
set sel to selection
--get the stacking order of the Selection
set mySel to object reference of selection
set allPageItems to all page items
repeat with i from 1 to count of items of allPageItems
if item i of allPageItems is mySel then
set mySelFrameStackingOrder to i
exit repeat
end if
end repeat
display dialog mySelFrameStackingOrder
if (count of sel) > 0 then
set currentItem to item 1 of sel
set parentPage to parent of currentItem
set allItems to every page item of parentPage
-- Get the bounds of the selected item
set {leftSel, topSel, rightSel, bottomSel} to geometric bounds of currentItem
-- Initialize variables to track the closest item behind
set closestItem to missing value
set closestDistance to 9999999 -- A large number to start with
-- Check all items on the page in reverse order
repeat with i from (count of allItems) to 1 by -1
set nextItem to item i of allItems
-- Skip the current item
if nextItem is not currentItem then
-- Get bounds of the next item
set {leftNext, topNext, rightNext, bottomNext} to geometric bounds of nextItem
-- Check for intersection with the selected item
if not ((rightNext < leftSel) or (leftNext > rightSel) or (bottomNext < topSel) or (topNext > bottomSel)) then
-- Calculate the distance squared
set centerSelX to (leftSel + rightSel) / 2
set centerSelY to (topSel + bottomSel) / 2
set distanceSquared to ((centerSelX - ((leftNext + rightNext) / 2)) ^ 2) + ((centerSelY - ((topNext + bottomNext) / 2)) ^ 2)
-- Check if this item is the closest one behind
if distanceSquared < closestDistance then
set closestDistance to distanceSquared
set closestItem to nextItem
end if
end if
end if
end repeat
-- Return width and height of the closest item if found
if closestItem is not missing value then
set {leftClosest, topClosest, rightClosest, bottomClosest} to geometric bounds of closestItem
set widthClosest to rightClosest - leftClosest
set heightClosest to bottomClosest - topClosest
display dialog "Width: " & widthClosest & ", Height: " & heightClosest
if (widthClosest is less than or equal to 101) and (heightClosest is less than or equal to 101) then
--get the stacking order
--get the stacking order of the Selection
try
set mySel to object reference of closestItem
set allPageItems to all page items
repeat with i from 1 to count of items of allPageItems
if item i of allPageItems is closestItem then
set frameBeneathStackingOrder to i
exit repeat
end if
end repeat
end try
if frameBeneathStackingOrder is greater than mySelFrameStackingOrder then
display dialog frameBeneathStackingOrder
set myFoundFrame to true
end if
end if
else
--display dialog "No intersecting items behind the selected item."
end if
else
--display dialog "No items selected."
end if
end tell
display dialog myFoundFrame
end tell
tell application id "InDn"
invoke menu action id 11288
set theObject to item 1 of selection
set {theY, theX, theB, theR} to geometric bounds of theObject
set {theWidth, theHeight} to {theR - theX, theB - theY}
end tell