Script assistance to extract/export all text layers

What features would you like to see in Pixelmator Pro?
User avatar

2021-02-23 17:11:46

So I think AppleScript is the best route here but if anyone has any other suggestions I am all ears. A lot of the time I am building posters 36x24 or 72x36. Usually I have a half dozen text layers mixed within graphics. Most of the time it is nothing too fancy, just in house advertisement for vehicles.
I am looking for a way to extract all the text layers and export them into a single word or text document. Ideally the text would export in the order it is displayed in the Pixelmator file, starting at top left to bottom right. Anyone have any suggestions?


Sample image
Image
User avatar

2021-02-24 09:57:48

If formatting does not matter, you can try the following:
* select all text layers in Pixelmator (use layer filter on bottom of layers panel)
* copy
* paste or "paste and match style" into a plain text editor of your choice.

The order will be taken from layers panel, though, not position on screen.
User avatar

2021-02-24 10:44:55

Hi Jason, if you're looking to move all that text from multiple files to, for example, a Pages doc automatically, AppleScript would almost certainly be your best bet. If the order of your layers in the Layers sidebar isn't the order you'd like to export the text, you could write the script to export layers based on their position on the canvas.

I've written a quick example script that simply takes all the top-level text layers, sorts them according to their vertical position on the canvas, then goes over the list again and sorts layers according to their horizontal position for any layers that have the exact same vertical (y axis) position. I'm not sure what your documents look like – I have a feeling they probably have group layers and you'd like to take that into account, so this might not work, but it's a good start and something that can be built on. I haven't written the Pages part of the script, but if you could share a sample document, I could find some time to look into this for you. I've also shared the basic script below.

Disclaimer: I'm not a programmer (more an enthusiastic amateur) and I know bubble sort is horribly inefficient and I'm using too many variables in the handler (with the goal of, hopefully, making the script easier to read and understand), but hopefully my less than efficient and overly verbose method might inspire someone to chime in with a better way. :wink:

Anyway, here's that basic script that gets all the top-level text layers, sorts them according to their position, then puts all the text content into a list.
tell application "Pixelmator Pro"
	tell the front document
		set sortedTextLayers to my sortLayersByPosition(every text layer)
		set allTextContent to {}
		repeat with currentLayer in sortedTextLayers
			copy text content of currentLayer to end of allTextContent
		end repeat
		return allTextContent
	end tell
end tell

on sortLayersByPosition(myLayers)
	using terms from application "Pixelmator Pro"
		repeat with i from 1 to (count of myLayers) - 1
			repeat with j from i + 1 to count of myLayers
				set {jLayerXPosition, jLayerYPosition} to position of item j of myLayers
				set {iLayerXPosition, iLayerYPosition} to position of item i of myLayers
				if jLayerYPosition < iLayerYPosition then
					set temp to item i of myLayers
					set item i of myLayers to item j of myLayers
					set item j of myLayers to temp
				end if
			end repeat
		end repeat
		repeat with i from 1 to (count of myLayers) - 1
			repeat with j from i + 1 to count of myLayers
				set {jLayerXPosition, jLayerYPosition} to position of item j of myLayers
				set {iLayerXPosition, iLayerYPosition} to position of item i of myLayers
				if jLayerYPosition = iLayerYPosition then
					if jLayerXPosition < iLayerXPosition then
						set temp to item i of myLayers
						set item i of myLayers to item j of myLayers
						set item j of myLayers to temp
					end if
				end if
			end repeat
		end repeat
	end using terms from
	return myLayers
end sortLayersByPosition
User avatar

2021-02-24 17:41:55

Thank you both for these great suggestions! I am indeed using groups so the script is not pulling everything I need but I think I can work a combo of both suggestions to come to what I am looking for. Again thank you both so much and I will reply back when I have something that works for me.
:thumbs_up: