Picture this: on a page (page i) in a document in InDesign is this 2x2 inch rectangle with the applescript label “IMG01” (aka lItemLabel). After placing a .TIF image in the rectangle I want to resize it within the 2x2 inch bounds but in a way NOT allowed via the ‘fit’ command.
To explain, what I want - much like the ‘fit given proportionally’ I want the image to be resized proportionally… just NOT so the longer dimension of the image fits within the 2x2 inch rectangle, so the shorter dimension does. So if I had a 3.5x5 inch photograph I would want to have the 3.5 inch dimension fit the 2 inch rectangle and the 5 inch dimension would be centered and cropped.
on sImageResizer(lImagePlacement, lItemLabel, i)
tell application "InDesign CS"
tell active document
tell page i
if lImagePlacement = "" or lImagePlacement = "p" then
fit rectangle lItemLabel given proportionally -- Shrinks the image down proportionally then
fit rectangle lItemLabel given frame to content -- Reduces the size of the rectangle to fit it
else if lImagePlacement = "c" then
set {RTb, RLb, RBb, RRb} to get geometric bounds of rectangle lItemLabel -- Get dimensions of rectangle
select rectangle lItemLabel
set {ITb, ILb, IBb, IRb} to get geometric bounds of graphic 1 of selection -- Get dimensions of Image
set lRectHeight to (RBb - RTb) -- Height of rectangle
set lRectWidth to (RRb - RLb) -- Width of rectangle
set lImageHeight to (IBb - ITb) -- Height of image
set lImageWidth to (IRb - ILb) -- Width of image
--display dialog ("Rectangle dimensions - H:" & lRectHeight & " W:" & lRectWidth & return & "Image dimensions - H:" & lImageHeight & " W:" & lImageWidth)
if lImageHeight > lImageWidth then -- Get the smaller of the two dimensions
set lMultiplier to (lRectWidth / lImageWidth)
else if lImageWidth > lImageHeight then -- Get the smaller of the two dimensions
set lMultiplier to (lRectHeight / lImageHeight)
else if lImageWidth = lImageHeight then -- If the two dimensions are equal
set lMultiplier to (lRectHeight / lImageHeight) -- Either width OR height would have worked in this instance
end if
--display dialog "Multiplier - " & lMultiplier
set IBb to (IBb * lMultiplier) -- Reduce/enlarge lMultiplier %
set IRb to (IRb * lMultiplier) -- Reduce/enlarge lMultiplier %
set geometric bounds of text frame lItemLabel to {ITb, ILb, IBb, IRb} -- Sets the new Image dimensions
fit rectangle lItemLabel given center content -- Center the image cropping the remainder height or width
end if
end tell
end tell
end tell
end sImageResizer
Below is the error I get with this iteration of the script. I have tried several versions and gotten several errors, but this one made the most sense.
It will be a couple days before I have access to ID again, so I can’t test your code or my hunches, but I see things that I think are potential problems. I believe line 11 needs an article “ “the selection”, and line 7 (from the bottom) is referencing a different object class than the rest of the code.
Son of a … Okay, I changed the object class ‘text frame’ to ‘rectangle’ (how did I not see that?!?) but changing line 11 to ‘the selection’ or even substituting ‘lItemLabel’ for 'selection doesn’t seem to get me past the error. In fact I get the error
The newest error message that was returned seems to show that it’s at least trying, but the class is other than expected. One of these constructions might work:
set {ITb, ILb, IBb, IRb} to get geometric bounds of graphic 1 of rectangle (the selection)
set {ITb, ILb, IBb, IRb} to get geometric bounds of item 1 of the selection's all graphics
Hi,
If I understand you correctly I think this should do the trick:
set thefile to choose file
tell application "Adobe InDesign CS2"
activate
set myDocument to make document
tell document 1
set myRectangle to make rectangle with properties {geometric bounds:{10, 10, 60, 60}, stroke weight:0}
set boxwidth to (item 4 of geometric bounds of myRectangle) - (item 2 of geometric bounds of myRectangle)
set boxheight to (item 3 of geometric bounds of myRectangle) - (item 1 of geometric bounds of myRectangle)
tell myRectangle
set thisgraphic to place thefile
end tell
set imageBounds to geometric bounds of thisgraphic
set imageHeight to (item 3 of imageBounds) - (item 1 of imageBounds)
set imageWidth to (item 4 of imageBounds) - (item 2 of imageBounds)
if imageHeight > boxheight then
display dialog "image height is > box height"
set geometric bounds of thisgraphic to {(item 1 of geometric bounds of myRectangle), (item 2 of imageBounds), (item 3 of geometric bounds of myRectangle), (item 4 of imageBounds)}
end if
set Vertical_scale to vertical scale of thisgraphic
set horizontal scale of thisgraphic to Vertical_scale
tell myRectangle
fit thisgraphic given center content
end tell
end tell
end tell
If the height of the image you place is larger than the height of the box it will reduce the height of the image to the height of the box and then take the percentage of the scale and apply it to the width of the image, keeping the image proportional.
Thanks,
Nik
I’m not home to check this yet, but if I read this correctly:
it seems to do the same as ‘fit given proportionally’ in that it reduces the LONGER dimension to fit in the rectangle. I want the SHORTER dimension to fit and the longer dimension to be cropped. What you have given me can probably be reworked to do just that so when I get home I will make the changes I think will be necessary and let you know if it works.
Nik beat me to it with his ready-made solution, but I was able to check your code in InDesign this morning, and the issue wasn’t the line of code itself, but the placement. The selection’s object reference already contains a page ID, so you can’t tell the selection of the page to do something, directly; you either have to nix the “tell page” block or redirect the command.
tell application "InDesign CS"
tell active document
set theTarget to rectangle lItemLabel
tell page 1
set {RTb, RLb, RBb, RRb} to get geometric bounds of rectangle lItemLabel
--select rectangle lItemLabel (* optional *)
tell theTarget to set {ITb, ILb, IBb, IRb} to item 1's graphic 1's geometric bounds
--insert rest o' stuff
end tell
end tell
end tell
So even if I have a document with 100 pages, each with a rectangle named “IMG01”, the document knows which one belongs to any given page? Sweet! You don’t know just how much you have helped me understand how InDesign handles these objects.
Well, here’s what I have and it works perfectly thanks to Marc Anthony and blend 3.
on sImageResizer(lImagePlacement, lItemLabel, i, lScriptsFolder)
tell application "InDesign CS"
tell active document
set lImageTarget to rectangle lItemLabel
tell page i
if lImagePlacement = "" or lImagePlacement = "p" then
fit rectangle lItemLabel given proportionally -- Shrinks the image down proportionally then
fit rectangle lItemLabel given frame to content -- Reduces the size of the rectangle to fit it
else if lImagePlacement = "c" then
tell lImageTarget to set {ITb, ILb, IBb, IRb} to item 1's graphic 1's geometric bounds -- Get dimensions of Image
set lImageHeight to (IBb - ITb) -- Height of image
set lImageWidth to (IRb - ILb) -- Width of image
set {RTb, RLb, RBb, RRb} to get geometric bounds of rectangle lItemLabel -- Get dimensions of rectangle
set lRectHeight to (RBb - RTb) -- Height of rectangle
set lRectWidth to (RRb - RLb) -- Width of rectangle
if lImageHeight > lImageWidth then -- Get the smaller of the two dimensions
set lMultiplier to (lRectWidth / lImageWidth) -- Determine percentage as decimal
set ITb to RTb -- Place top of image at top of rectangle
set ILb to RLb -- Place left side of image at left side of rectangle
set IRb to RRb
set IBb to (ITb + (lImageHeight * lMultiplier))
else if lImageWidth > lImageHeight then -- Get the smaller of the two dimensions
set lMultiplier to (lRectHeight / lImageHeight) -- Determine percentage as decimal
set ITb to RTb -- Place top of image at top of rectangle
set ILb to RLb -- Place left side of image at left side of rectangle
set IBb to RBb -- Fixed a typo here
set IRb to (ILb + (lImageWidth * lMultiplier))
else if lImageWidth = lImageHeight then -- If the two dimensions are equal
set ITb to RTb -- Place top of image at top of rectangle
set ILb to RLb -- Place left side of image at left side of rectangle
set IBb to RBb -- Place bottom of image at top of rectangle
set IRb to RRb -- Place right side of image at left side of rectangle
end if
set item 1's graphic 1's geometric bounds of lImageTarget to {ITb, ILb, IBb, IRb} -- Sets the new Image dimensions
fit rectangle lItemLabel given center content -- Center the image cropping the remainer height or width
end if
end tell
end tell
end tell
end sImageResizer
Came back to fix a typo THINKING that it was why the images on every page after page 1 fail to resize the image at all, but I was incorrect in this assumption. There is a problem with this script (at least on my Mac) and I will try to remember to come back and update this when I figure out what it is.
Okay, figured out the problem and tested this with fantastic results (told you I’d come back with the answer…eventually ).
on sImageResizer(lCustomerGraphic, lImagePlacement, lItemLabel, i, lProjectDocument)
tell application "InDesign CS"
tell lProjectDocument
tell page i
--> Resize proportionally to the longest dimension
if ((lImagePlacement = "") or (lImagePlacement contains "P") or (lImagePlacement contains "p")) then
place alias lCustomerGraphic on page item lItemLabel -- Insert the graphic item
fit rectangle lItemLabel given proportionally -- Shrinks the image down proportionally then
fit rectangle lItemLabel given frame to content -- Reduces the size of the rectangle to fit it
end if
--> Resize proportionally to the shortest dimension and center
if ((lImagePlacement contains "C") or (lImagePlacement contains "c")) then
set {RTb, RLb, RBb, RRb} to geometric bounds of rectangle lItemLabel -- Get dimensions of rectangle
set lRectHeight to (RBb - RTb) -- Height of rectangle
set lRectWidth to (RRb - RLb) -- Width of rectangle
place alias lCustomerGraphic on page item lItemLabel -- Insert the graphic item
tell rectangle lItemLabel to set {ITb, ILb, IBb, IRb} to item 1's graphic 1's geometric bounds -- Get dimensions of Image
set lImageHeight to (IBb - ITb) -- Height of image
set lImageWidth to (IRb - ILb) -- Width of image
if lImageHeight < lImageWidth then -- Get the smaller of the two dimensions
set lMultiplier to (lRectHeight / lImageHeight) -- Determine percentage as decimal
set ITb to RTb -- Place top of image at top of rectangle
set ILb to RLb -- Place left side of image at left side of rectangle
set IRb to (ILb + (lImageWidth * lMultiplier))
set IBb to RBb -- Place bottom of image at bottom of rectangle
else if lImageHeight > lImageWidth then -- Get the smaller of the two dimensions
set lMultiplier to (lRectWidth / lImageWidth) -- Determine percentage as decimal
set ITb to RTb -- Place top of image at top of rectangle
set ILb to RLb -- Place left side of image at left side of rectangle
set IRb to RRb -- Place right side of image at right side of rectangle
set IBb to (ITb + (lImageHeight * lMultiplier))
else if lImageHeight = lImageWidth then -- If the two dimensions are equal
set ITb to RTb -- Place top of image at top of rectangle
set ILb to RLb -- Place left side of image at left side of rectangle
set IRb to RRb -- Place right side of image at left side of rectangle
set IBb to RBb -- Place bottom of image at top of rectangle
end if
tell rectangle lItemLabel to set item 1's graphic 1's geometric bounds to {ITb, ILb, IBb, IRb}
fit rectangle lItemLabel given center content -- Center the image cropping the remainer height or width -- rectangle lItemLabel
end if
end tell
end tell
end tell
end sImageResizer
Yes, every polygon in ID has its own unique ID number. And every polygon belongs to a page, and every page belongs to a spread. So you have to talk to it like this:
tell polygon id 234 of page 1 of spread 1 of document 1
--do something
end tell