I’ve built a script that accepts images and then compiles an InDesign document, placing one image on each page. The Applescript below is the step that handles this task within a larger Automator workflow.
It seems to work well. There’s one change I’d like to make though. At present, it simply places images centrally at their actual size within the frame it creates. 90% of the time this is perfect for my purposes. Occasionally though, I will feed in an image which is larger than the frame and it will appear cropped. What I would like is for those images to be scaled down proportionally so that they fit the frame. To be clear, I don’t want any images that fit at actual size scaled up though. I want them to remain as they are.
You can probably ignore the second half of the script, which inputs the file name into the document. That’s fine as is. I don’t need it to change if the image scales or anything.
I am a complete novice. I’m enjoying learning, but my process so far has largely revolved around finding pieces of code online that seem relevant, trying to figure them out and then cobbling stuff together. It’s slow and painful, but I seem to learn best that way. Apologies for what I’m sure is horribly messy code.
tell application "Finder"
set linksLocation to ((path to desktop folder) & "RootFolder:Links") as string as alias
set filelist to (every file of the linksLocation as list)
set docFolderLocation to ((path to desktop folder) & "RootFolder:Pages") as string as alias
repeat with currentFile in filelist
set fileName to name of currentFile
set workPageTemplate to (path to me) & "Templates:WorkPageTemplate.indd" as string as alias
duplicate workPageTemplate to docFolderLocation
set workPageTemplate to ((path to desktop folder) & "RootFolder:Pages:WorkPageTemplate.indd") as string as alias
set the name of file workPageTemplate to fileName
set name extension of workPageTemplate to "indd"
open workPageTemplate
delay 1
tell application "Adobe InDesign CC 2017"
activate
set itemlink to currentFile as string as alias
set myDocument to active document --User's active document becomes document to place image
tell myDocument
set noColor to swatch "None" --same as transparent
set myRectangle to make rectangle with properties {geometric bounds:{36, 318, 675, 1160}}
place (itemlink) on myRectangle --InDesign's place function
fit rectangle 1 given center content
--- ( trim file name for insertion into document as dimensions
set fileName to ((characters 1 thru -9 of fileName) as string)
--- ) trim file name for insertion into document as dimensions
-- ( Adds file dimensions to info panel
set findWord to "dimensions"
--establish text for change
set changeWord to fileName
tell application "Adobe InDesign CC 2017"
--Clear find text preferences
set find text preferences to nothing
--Clear change text preferences
set change text preferences to nothing
--establish properties for find
set find what of find text preferences to findWord
--establish properties for change
set change to of change text preferences to changeWord
--perform change text operation
tell document 1
change text
end tell
set find text preferences to nothing
set change text preferences to nothing
end tell
-- ) Adds file dimensions to info panel
save workPageTemplate
close workPageTemplate
end tell
end tell
end repeat
end tell
Hi. I really just skimmed your code, but I saw that your choice of itemlink as a variable name has the potential to be confused or mistyped as InDesign’s own item link specifier; I would either use the latter or differentiate the former. I also recommend downloading Adobe’s AppleScript Scripting Guide for your version; it has good info for newbies and is free.
The command you want—Fit—is already part of your script, but you’ll use the parameter proportionally. You can either issue that as a second command after the center content fit, or set a centering preference outside any loop:
tell application "Adobe InDesign CS3"'s document 1 to set rectangles's frame fitting options's fitting alignment to center anchor
You can set fitting as a global preference.
tell application "Adobe InDesign CS3" to set frame fitting options's properties to {fitting on empty frame:proportionally}
Edit: I’ve made some progress since this post and posted again below, so please don’t waste your time scrutinising the terrible code in this post. Instead, I’d really appreciate you skipping down to the post below and casting an eye over the terrible code in there instead. Thanks!
Hi Marc,
Thanks for coming back to me. I have taken your advice with regards to the item ink specifier. I’ve been picking through the Scripting Guide already but need to spend more time with it.
I’m struggling to implement this. I’m using InDesign CC 2017. Could that make a difference?
If it did work though, wouldn’t it also scale images that were appearing at actual size up to fit the frame? That’s not what I’m after.
I guess an if statement could make this work. I came up with the following, which doesn’t work. It hopefully illustrates what I’m getting at though. I feel like I’m close.
tell application "Adobe InDesign CC 2017"
activate
set workFile to currentFile as string as alias
set myDocument to active document --User's active document becomes document to place image
tell myDocument
set noColor to swatch "None" --same as transparent
set myRectangle to make rectangle with properties {geometric bounds:{36, 318, 675, 1160}}
place (workFile) on myRectangle --InDesign's place function
fit rectangle 1 given center content
set hScale to (horizontal scale of workFile)
set vScale to (vertical scale of workFile)
if vScale is greater than {770} then fit rectangle 1 given content to frame
if hScale is greater than {840} then fit rectangle 1 given content to frame
I’ve got closer to achieving what I wanted since my last post. After hours of exasperation, I stepped away from the keyboard for 10 minutes and realised something obvious. Always the way! I’ve already defined the dimensions of the images I’m using in terms of x and y within my script. I could probably use that. Duh! I quickly threw this together and it almost achieves what I need. Almost.
tell application "Image Events"
-- start the Image Events application
activate
-- open the image file
set this_image to open currentFile as alias
-- extract the properties record
set the props_rec to the properties of this_image
-- purge the open image data
close this_image
-- extract the property values from the record
set the image_info to ""
copy (dimensions of props_rec) to {x, y}
set the image_info to the image_info & ¬
"" & x & "x" & y
end tell
tell application "Adobe InDesign CC 2017"
activate
set workFile to currentFile as string as alias
set myDocument to active document --User's active document becomes document to place image
tell myDocument
set noColor to swatch "None" --same as transparent
set myRectangle to make rectangle with properties {geometric bounds:{36, 318, 675, 1160}}
place (workFile) on myRectangle --InDesign's place function
if x is greater than {770} then fit rectangle 1 given content to frame
if y is greater than {840} then fit rectangle 1 given content to frame
Now, images which are larger than the frame are scaled down to fit. Those which are smaller are depicted at actual size. Awesome!
However… I now have two new issues. The larger images which are scaled down don’t scale proportionally, so they appear stretched. The smaller images no longer appear centrally within the frames. They’re anchored to the top left so they appear in the wrong place on the page.
I’m sure there are really simple fixes to these but whatever I try seems to just bring up an error. I’m so close! Can anyone push me in the right direction?
Again, thanks in advance and I appreciate your patience with my inexperience.
You said “user’s active document,” implying group use; is this for a commercial purpose? If so, I can only give you tips, rather than writing code. If you want proportionality on a case-by-case basis, both width and height must be considered, and Image Events isn’t needed for that. Determine the frame’s dimensions from your supplied bounds. Arrive at that container’s placed image’s dimensions, and then adjust both horizontal and vertical scales until it’s smaller; this requires polling for changes.
I managed to get it working eventually. Thanks for your input. No, this is being built for my own use. I’m just trying to automate a very tedious part of my workflow and hopefully get my head around this whole scripting thing in the process.