After spending a full 6 hours trying different formulas, different approaches and different google phrases I’m ready to a admit I need help.
I need a script that can change the scale of an embedded image to match the width of the document it’s in and I just cannot figure out what I need to do to convert the pixel width of the document into a percentage to set my layer. Nothing I’ve tried has even got it close.
I’d have thought this was a really common operation but I haven’t been able to find a single comparable case
Here’s as far as I’ve got with my code, the part with question marks is where I’ve been desperately trying different formulas all day.
set type units of settings to pixel units
--set variable for layer width
set LW to (item 3 of bounds of layer "LOGOvec" of current document)
--set variable to document width
set DocW to (width of current document)
--set variable to final percentage to apply to layer
set UseScale to ???????
scale (layer "LOGOvec") of current document ¬
horizontal scale UseScale ¬
vertical scale UseScale ¬
anchor position (middle center)
Had to write this really fast, barely had any time to test it, but seems to be working.
I’m assuming you always want it:
scaled proportionally
centered on the canvas
tell application "Adobe Photoshop CC 2019"
set docRef to the current document
tell docRef
-- gather data
set embeddedLayerName to "Test Logo"
set embedBounds to the bounds of layer embeddedLayerName
tell embedBounds to set {leftEdge, topEdge, embedWidth, embedHeight} to {item 1, item 2, (item 3) - (item 1), (item 4) - (item 2)}
set docWidth to the width
set docHeight to the height
-- center embedded image on canvas
set {embedXcenter, embedYcenter} to {(embedWidth / 2 + leftEdge), (embedHeight / 2 + topEdge)}
set {docXcenter, docYcenter} to {docWidth / 2, docHeight / 2}
set {translateX, translateY} to {docXcenter - embedXcenter, docYcenter - embedYcenter}
translate layer embeddedLayerName delta x translateX delta y translateY
-- test aspect ratio to decide to scale by width or height
if embedWidth / embedHeight > docWidth / docHeight then
set scaleFactor to docWidth / embedWidth * 100
else
set scaleFactor to docHeight / embedHeight * 100
end if
--scale
scale layer embeddedLayerName horizontal scale scaleFactor vertical scale scaleFactor
end tell
end tell
That works absolutely beautifully, thank you so much!
The frustrating thing is I think I did have the right scaling formula at one point, but I’d also misunderstood how getting the bounds of at object worked