Automator / Export for Web

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

2021-11-12 01:13:51

Good day!

I'm trying to create a Quick Action with Automator (macOS 11.6.1, PixelMator Pro 2.2 from AppStore) for processing multiple selected images, but no luck so far :sweat_smile:

What I'm trying to achieve:
- Select multiple images in Finder (manually);
- Send them via Quick Action menu to 'Export for Web';
- Save in same folder, overwriting original files.

I always sort and duplicate files before editing, so a prompt for a save location or overwrite confirmation is really not necessary.

After going over Google and various topics on this board, I kind of managed to stich together some AppleScript code, but it doesn't really work:
tell application "Pixelmator Pro"
	set exportLocation to choose folder with prompt "Please choose where you'd like to export the images:"
	tell the front document
		export for web format(PNG)
		export for web options {compression factor:100, advanced compression:true, reduce colors:false, keep transparency:true, convert to sRGB:false, scale:1}
	end tell
end tell
I don't know how to set location to current folder, so I've just copied that part from another script I've found here.

My Automator settings are:
Workflow receives: Image files in Finder

The first thing on my Automator routine is Get Selected Finder Items, followed by Run AppleScript, as above.

I'm always getting error "Document 1 doesn't understand format message".

Any help or examples would be highly appreciated :pray: :red_heart:
User avatar

2021-11-12 09:05:45

I can´t find an action "Export for Web" in Automator->Library->Photos available through "pixelmator.com".
These are the only ones related to PixelmatorPro I can determine:
Image
User avatar

2021-11-12 10:53:40

Your explanation of what you'd like to achieve is very clear, you just need the tools to be able to turn that into code. Essentially, you need to:

– Get the name/location of every currently selected file in the Finder
– Export those files to PNG, overwriting the original files
– Rename the files

The script for that would look like this:
tell application "Finder"
	-- Finder has a "selection" property that gets you every selected item
	set filesToProcess to selection
	-- I also get the location here to make it easier to rename the file later on
	set fileLocation to (container of item 1 of filesToProcess) as text
end tell

tell application "Pixelmator Pro"
	repeat with currentFile in filesToProcess
		
		tell application "Finder"
			set extensionWasHidden to extension hidden of currentFile
		end tell
		
		-- We need to use "currentFile as alias" because document file objects 
		-- (which is what is returned by the "selection" property) get opened 
		-- in Preview by default, not too sure why, though
		open (currentFile as alias)
		-- Before, you had "scale: 1" and "compression factor: 100" – scale: 
		-- 1 is 1% of the original size and "compression factor" only applies 
		-- to JPEGs, HEIFs, and other lossy formats, so I fixed all that up
		tell the front document to export for web to (currentFile as alias) as PNG with properties {advanced compression:true, reduce colors:false, keep transparency:true, convert to sRGB:false, scale:100}
		close the front document without saving
		
		-- This bit just handles file names and extensions – the file might have 
		-- been any format before, so we need to update the extension to "png". 
		-- Also, exporting to the same file somehow makes the extension visible, 
		-- so before opening the file, I check if the extension is hidden, then make 
		-- a decision based on that here
		tell application "Finder"
			set extension hidden of currentFile to true
			set filename to (displayed name of currentFile) & ".png"
			set name of currentFile to filename
			set extension hidden of ((fileLocation & filename) as alias) to extensionWasHidden
		end tell
		
	end repeat
end tell
To make this work with a Quick Action, you'd need to just create one that runs one action "Run AppleScript" with this script. It's possible to integrate AppleScripts within Automator and pass the data from the script to Automator actions, but for this, I think it's simpler to keep everything in the script.

One final note – the script is almost certainly not the most optimal implementation of this workflow (the file name and extension handling can probably be optimized), but it took a little while already and I really want to share it now. :pray: