Re: Automate action using AppleScript

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

2023-12-29 11:28:58

Hello everyone!

I'm trying to automate a routine in my Pixelmator Pro document.

I have quite a few layers that I need to export for web in different formats, I've tried to write an AppleScript to do this but I cannot get It to work (I've never used AppleScript before, honestly). That's what I have right now..
tell application "Pixelmator Pro"
	tell its front document
		set numberOfLayers to count layers
		set selectedLayer to 1
		repeat until selectedLayer = numberOfLayers
			select layer selectedLayer
			set lName to layer name
			set lName to lName + ".jpg"
			export for web layer to file ((path to desktop as text) & lName) as JPEG with properties {compression factor:90}
			set lName to lName + ".png"
			export for web layer to file ((path to desktop as text) & lName) as PNG with properties {compression factor:90, advanced compression:true, keep transparency:true}
			set lName to lName + ".webp"
			export for web layer to file ((path to desktop as text) & lName) as WebP with properties {compression factor:90, keep transparency:true}
			set selectedLayer to selectedLayer + 1
		end repeat
	end tell
end tell
Script compiles correctly but if I try to run it, it stops at line "set lName to layer name" with error "Unable to get layer "siteImages" of document 1."

Does anyone have an idea what's the correct way to do the task?

Thanks.
User avatar

2024-01-02 09:46:07

After quite a lot of trying I've written the script like this:
tell application "Pixelmator Pro"
	set theDocument to front document
	tell theDocument
		set numberOfLayers to count layers
		set selectedLayer to 1
		repeat until selectedLayer = numberOfLayers
			set theCurrentLayer to layer selectedLayer
			tell theDocument to tell theCurrentLayer
				set visible to true
			end tell
			set theCurrentLayerName to name of theCurrentLayer
			set theCurrentLayerName to theCurrentLayerName & ".jpg"
			export for web theDocument to file ((path to desktop as text) & theCurrentLayerName) as JPEG with properties {compression factor:90}
			set theCurrentLayerName to theCurrentLayerName & ".png"
			export for web theCurrentLayer to file ((path to desktop as text) & theCurrentLayerName) as PNG with properties {advanced compression:true, keep transparency:true}
			set theCurrentLayerName to theCurrentLayerName & ". webp"
			export for web theCurrentLayer to file ((path to desktop as text) & theCurrentLayerName) as WebP with properties {compression factor:90}
			tell theDocument to tell theCurrentLayer
				set visible to false
			end tell
			set selectedLayer to selectedLayer + 1
		end repeat
	end tell
end tell
problem now is on the line
export for web theDocument to file ((path to desktop as text) & theCurrentLayerName) as JPEG with properties {compression factor:90}
Where I get the error -10002, I really don't know why as it's basically a direct copy of the line I've found in the Pixelamtor Pro script dictionary. Does anyone have an hint?

Thanks!