Batch export layers individually ?

Chat about the original Pixelmator.
User avatar

2020-12-11 12:21:30

I export a lot of layers, and it takes a lot of time to check and uncheck e.g. 20 layers and save them with a numerically incremented name e.g. "layer1", "layer2" (and not necessarily the name of the layer itself).

Is there a feature in the Pro version that allows batch export of layers, which can be renamed before exporting, or at least the ability to batch export each layer individually ?
User avatar

2020-12-18 16:42:32

You could absolutely use AppleScript to batch export layers in Pixelmator Pro. If you have a sample file and some instructions on how you'd like to batch export the layers, I could help you with a script.
User avatar

2021-01-04 16:55:35

Many thanks for the reply.

I do not have Pro, unfortunately I don't see a need to upgrade with the Pixelart style that I employ.

However, quality of life features like that would be universally welcome, as well as batch renaming layers and files in layers.

There are a number of GitHub repositories that handle batch renaming, as you list that some of your extensions are natively written in Swift the closest I found was a C++ cross-platform effort by Guid75 called "renamah" which is apparently "modular", which would make it easier to implement:

https://github.com/Guid75/renamah
User avatar

2021-01-05 11:13:19

by tiffin Many thanks for the reply.

I do not have Pro, unfortunately I don't see a need to upgrade with the Pixelart style that I employ.
That's totally your call. But if you haven't actually tried the app, you should definitely at least check out the free trial.
by tiffin There are a number of GitHub repositories that handle batch renaming, as you list that some of your extensions are natively written in Swift the closest I found was a C++ cross-platform effort by Guid75 called "renamah" which is apparently "modular", which would make it easier to implement:

https://github.com/Guid75/renamah
If we did decide to create a feature like this, I reckon the code part wouldn't be the problem. But thanks for the suggestion, either way!
User avatar

2021-09-15 17:18:42

Hi @andrius,

I am dealing with the same request:
- 50 headshots all nicely cropped and fitted into one file
- rather not hide/unhide/export one by one but have a batch export

Dragging to e.g. the desktop is great, but the sizing is based on the original, not the Canvas Size.

If you can write an Apple Script, I would much appreciate that offer!

Have a great day

Roland
User avatar

2021-09-15 18:08:12

Hi Roland! I've posted a script for a similar workflow in this thread here. Perhaps you'll find some use for it. I don't think you'd need much overwriting either — just remove the 50% resizing and the @0.5x.jpg extension, I guess? And, of course, choose the export file format you want. Essentially, a very basic export script would look something like this:
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
		set visible of every layer to false
		set visible of the last layer to true
		repeat with currentLayer in every layer
			set visible of currentLayer to true
			export for web to ((exportLocation as text) & name of currentLayer & ".jpg") as JPEG with properties {compression factor:100}
			set visible of currentLayer to false
		end repeat
	end tell
end tell
User avatar

2021-09-16 16:45:28

Image Hi @Aurelija,

Not much of an Automator expert... could you tell me where to start or maybe even send me the file to run it?

UPDATE: this seems to work like a charm for some files with e.g. 3 pictures, but when I have a file with 5, I only get one exported picture. No clue what is causing one file to export all three when I have three layers and only one when I have five images in another layered file. I send these PXM files via email.

Thanks,

Roland
User avatar

2021-09-17 07:42:47

You'd typically run AppleScipts using the Script Editor (just copy and paste the script into a new document and run it), but you can alternatively use Automator's 'Run AppleScript' action if you so prefer, which I can see you did.

Thanks for sending in the documents, by the way. The issue I found is that one of them features layers that all share the same name. Since you're exporting all these layers to the same folder — that causes conflict on macOS. You can't have two files with the same title in the same folder. In other words, one file overwrites the other. My quick suggestion would be to rename the layers so that they all have unique names. Any other ideas on how to potentially do this layer renaming in-script are welcome though — I'm not very pro at scripting yet.
User avatar

2021-09-17 09:39:10

I got a little carried away, bur, errr, here's one solution...
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
		set visible of every layer to false
		set visible of the last layer to true
		repeat with currentLayer in every layer
			set visible of currentLayer to true
			set fileName to name of currentLayer
			set fileExtension to ".jpg"
			if my fileExistsAt((exportLocation as text) & fileName & fileExtension) then
				set fileName to my overwriteOriginalFile(fileName, fileExtension)
			end if
			export for web to (exportLocation as text) & fileName & fileExtension as JPEG with properties {compression factor:100}
			set visible of currentLayer to false
		end repeat
		set visible of every layer to true
	end tell
end tell

on fileExistsAt(filePath)
	tell application "Finder"
		if exists file filePath then
			return true
		end if
	end tell
	return false
end fileExistsAt

on overwriteOriginalFile(fileName, fileExtension)
	tell application "Pixelmator Pro"
		set overwriteOriginal to display dialog "A file named \"" & fileName & fileExtension & "\" already exists at your export location. Would you like to overwrite it or choose a new name?" buttons {"Cancel", "Overwrite", "Rename"} with icon note default button "Rename" cancel button "Cancel"
		if button returned of overwriteOriginal is "Overwrite" then
			return fileName
		else
			set newFilename to display dialog "New filename:" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
			return text returned of newFilename
		end if
	end tell
end overwriteOriginalFile
A simpler method would look like this:
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
		set visible of every layer to false
		set visible of the last layer to true
		set a to 1
		set fileExtension to ".jpg"
		repeat with currentLayer in every layer
			set visible of currentLayer to true
			set fileName to name of currentLayer
			export for web to (exportLocation as text) & fileName & a & fileExtension as JPEG with properties {compression factor:100}
			set visible of currentLayer to false
			set a to a + 1
		end repeat
		set visible of every layer to true
	end tell
end tell
Of course, it's also possible to write a script that would do the usual macOS thing of adding the next available number to the filename, but I think I've already spent a little too much time on this and I'm not sure that's exactly what you need.
User avatar

2021-09-17 15:15:32

by Aurelija 2021-09-17 14:42:47 You'd typically run AppleScipts using the Script Editor (just copy and paste the script into a new document and run it), but you can alternatively use Automator's 'Run AppleScript' action if you so prefer, which I can see you did.

Thanks for sending in the documents, by the way. The issue I found is that one of them features layers that all share the same name. Since you're exporting all these layers to the same folder — that causes conflict on macOS. You can't have two files with the same title in the same folder. In other words, one file overwrites the other. My quick suggestion would be to rename the layers so that they all have unique names. Any other ideas on how to potentially do this layer renaming in-script are welcome though — I'm not very pro at scripting yet.
Thanks! I will look into using your method. Very dumb of me to not realize I was using similar names. 🙈
User avatar

2021-09-17 15:16:41

by Andrius 2021-09-17 16:39:10 I got a little carried away, bur, errr, here's one solution...



A simpler method would look like this:



Of course, it's also possible to write a script that would do the usual macOS thing of adding the next available number to the filename, but I think I've already spent a little too much time on this and I'm not sure that's exactly what you need.
That’ll keep me busy!

Would something like this be achievable with the new Shortcuts functionality on the next release?
User avatar

2021-09-20 10:17:32

by Roland 2021-09-17 14:16:41 Would something like this be achievable with the new Shortcuts functionality on the next release?
We currently don't have an action that would get the layers from a layered document but it does sound like it would potentially be quite useful. Maybe we should consider it!
User avatar

2021-09-20 16:29:27

by Andrius 2021-09-20 17:17:32
We currently don't have an action that would get the layers from a layered document but it does sound like it would potentially be quite useful. Maybe we should consider it!
I'll keep an 👁 on this Community/Twitter!

Thanks for all your help!