[AppleScript] Apply an effect to one layer in a series of images

Discuss Pixelmator Pro tutorials and share useful resources.
User avatar

2020-10-16 16:47:29

Here's a quick script to answer this question on Twitter:
tell application "Pixelmator Pro"
	set imagesToProcess to choose file with prompt "Please select the images to process:" of type {"public.image"} with multiple selections allowed
	set exportLocation to choose folder with prompt "Please select where you'd like export the edited images:"
	repeat with a from 1 to number of imagesToProcess
		set currentImage to open item a of imagesToProcess
		tell currentImage
			duplicate layer 1
			set opacity of layer 1 to 50
			make new gaussian effect at the beginning of effects of layer 2 with properties {radius:10}
		end tell
		export currentImage to (exportLocation as text) & name of currentImage & "-edited.png" as PNG
		close currentImage without saving
	end repeat
end tell
User avatar

2020-10-16 21:00:38

This is great! I've updated based on the twitter conversation to use the Duo effect preset as noted below. Is there a way to add a step that resizes the image to a set dimension without distorting the image? So, say I wanted everything to be cropped to 1500x500 with a center alignment? Is this possible?
tell application "Pixelmator Pro"
	set imagesToProcess to choose file with prompt "Please select the images to process:" of type {"public.image"} with multiple selections allowed
	set exportLocation to choose folder with prompt "Please select where you'd like export the edited images:"
	repeat with a from 1 to number of imagesToProcess
		set currentImage to open item a of imagesToProcess
		tell currentImage
			duplicate layer 1
			set opacity of layer 1 to 50
			apply effects preset layer 2 name "Duo"
		end tell
		export currentImage to (exportLocation as text) & name of currentImage & "-edited.png" as PNG
		close currentImage without saving
	end repeat
end tell
User avatar

2020-10-16 21:32:53

by Justin Cox 2020-10-16 18:01:46 This is great! I've updated based on the twitter conversation to use the Duo effect preset as noted below. Is there a way to add a step that resizes the image to a set dimension without distorting the image? So, say I wanted everything to be cropped to 1500x500 with a center alignment? Is this possible?
Sure, I think the best option would be to resize the canvas using the resize canvas command. BUT... I just remembered there's currently a bug with that command — when setting the width/height, the first value is used for both the width and height. We've fixed this internally but the update hasn't yet been released. If everything was working correctly, you'd be able to add this line at the end of the currentImage tell block:
resize canvas width 1500 height 500 anchor position middle center
However, this will currently get you a 1500x1500 square image. Luckily, when "relative" resizing is used, the bug doesn't rear its head, so you can just add this line after the one above to take away 1000 pixels from the height of the image:
resize canvas width 0 height -1000 anchor position middle center relative yes
Oh, and if your images are all the same size, you could use just the command above. Say, if they're 2000x1000, you could set the canvas size width to -500 and the height to -500. But if they're different sizes, here's what the full script would look like:
tell application "Pixelmator Pro"
	set imagesToProcess to choose file with prompt "Please select the images to process:" of type {"public.image"} with multiple selections allowed
	set exportLocation to choose folder with prompt "Please select where you'd like export the edited images:"
	repeat with a from 1 to number of imagesToProcess
		set currentImage to open item a of imagesToProcess
		tell currentImage
			duplicate layer 1
			set opacity of layer 1 to 50
			apply effects preset layer 2 name "Duo"
			resize canvas width 1500 height 1500 anchor position middle center
			resize canvas width 0 height -1000 anchor position middle center relative yes
		end tell
		export currentImage to (exportLocation as text) & name of currentImage & "-edited.png" as PNG
		close currentImage without saving
	end repeat
end tell
Hope that helps!
User avatar

2020-10-16 23:12:15

This is great help, though instead of resizing the canvas, I also what to resize the image. So I changed the string to this:
tell application "Pixelmator Pro"
	set imagesToProcess to choose file with prompt "Please select the images to process:" of type {"public.image"} with multiple selections allowed
	set exportLocation to choose folder with prompt "Please select where you'd like export the edited images:"
	repeat with a from 1 to number of imagesToProcess
		set currentImage to open item a of imagesToProcess
		tell currentImage
			duplicate layer 1
			set opacity of layer 1 to 50
			apply effects preset layer 2 name "Duo"
			resize image width 2048 height 1080 resolution 72 algorithm lanczos
		end tell
		export currentImage to (exportLocation as text) & name of currentImage & "-edited.png" as PNG
		close currentImage without saving
	end repeat
end tell
Which is almost perfect, but it stretches the image if the ratios are off. If I do the resize canvas command, it just crops the image to the middle, which isn't what I'm going for. I want to resize the canvas to the dimensions and resize the image to match without stretching the dimensions of the image. Does that make sense?
User avatar

2020-10-17 10:48:10

I think I understand what you mean – you'd like to resize the image to be 1500 pixels wide and at this point, presumably, it can be any amount of pixels tall. So then you'd like to crop away the top and bottom part of the image, leaving it 1500 pixels wide by 500 pixels tall? If so, here's what the resizing part of the script would be:
resize image width 1500 algorithm lanczos
resize canvas width 1500 height 1500 anchor position middle center
resize canvas width 0 height -1000 anchor position middle center relative yes
Because I didn't provide a height to the resize image command, it's resized in proportion to the width. Then the other part does the cropping bit.

Of course, if the resize canvas bug wasn't there, you could just say:
resize image width 1500 algorithm lanczos
resize canvas width 1500 height 500 anchor position middle center
However, the first version should keep working once we fix the bug, so you shouldn't have to go and manually update it. Hope that helps!
User avatar

2020-10-17 10:50:13

Oh, and I can see that you're using a different width/height in your most recent script but the general technique would remain the same. Resize to the width/height you need, then resize the canvas to crop away the parts that don't fit in the new aspect ratio.
User avatar

2020-10-17 12:42:04

Brilliant! That works! I was chaining them wrong. THANK YOU! Will the canvas bug be fixed with a future update of Pixelmator or is an update to Script Editor?
User avatar

2020-10-19 09:28:05

The bug will be fixed with a future update to Pixelmator Pro, but, as I mentioned, your script will keep working even after that update because it's designed to work in both circumstances.