Need some help with Applescript layer resize

Talk about Pixelmator Pro, share tips & tricks, tutorials, and other resources.
User avatar

2023-12-11 11:56:21

Hello, I *love* Pixelmator Pro and am so happy I switched from that "other" program. I am working on an Applescript (which I'll execute through Keyboard Maestro, a tool every Mac user should be using) that takes the current image layer I'm working on and reduces it in size a little (around 5% to 15% depending on the macro). I managed to get a script that works for me, which is
set resizeType to 1 -- resize 5%
--set resizeType to 2 -- resize 15%

tell application "Pixelmator Pro"
tell document 1
set thisLayer to current layer
set theWidth to width of thisLayer
set theHeight to height of thisLayer
if resizeType = 1 then
set width of thisLayer to theWidth * 0.92 -- reduce it a little
end if
if resizeType = 2 then
set width of thisLayer to theWidth * 0.85 -- reduce it a bit more
end if
end tell
end tell
This works, but it makes the layer shrink as if the anchor point were in the upper left corner. Is there any more elegant way to do this, so that it shrinks from the center? Or, how would I set the X/Y point of a layer if I wanted to move the layer over in the script?
User avatar

2023-12-11 16:56:01

chatGPT was of little help. All the methods it suggested failed for one reason or another. I am not any kind of applescript user.
User avatar

2023-12-14 22:04:15

Try this tweak to use the midpoint as the anchor instead of the upper left corner.
set resizeType to 1 -- resize 5%
--set resizeType to 2 -- resize 15%

tell application "Pixelmator Pro"
	tell document 1
		set thisLayer to current layer
		set theWidth to width of thisLayer
		set theHeight to height of thisLayer
		set {itemX, itemY} to position of thisLayer
		
		if resizeType = 1 then
			set width of thisLayer to theWidth * 0.92 -- reduce it a little
		end if
		if resizeType = 2 then
			set width of thisLayer to theWidth * 0.85 -- reduce it a bit more
		end if
		
		set newWidth to width of thisLayer
		set newHeight to height of thisLayer
		
		tell thisLayer to set position to {itemX + ((theWidth - newWidth) / 2), itemY + (theHeight - newHeight) / 2}
		
	end tell
end tell
User avatar

2023-12-14 22:39:28

I don't think this changes the origin - Just saying. Moves the origin to a different location based on the calculated midpoint. It would be VERY good if PixPro would permit changing the origin of a layer/shape or group, etc.
This is a good work around that shortcoming.