AppleScript: Duplicating current 'document', trim, resize and export?

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

2022-03-21 16:33:41

I've been looking around for a solution to this but could not find one, after been using Pixelmator for over 10 years I finally joined the community... I hope i posted in the right forum... :baby:

A very common workflow I have is to trim the current image, resize it and export it for web. My problem is that 1) I don't like to undo the trim and resize every time I do this by hand and 2) I would like to automate this using AppleScript.

What do you think is the best way of doing this?

My first idea was to create a duplicate image with the visible layers of the original image, trim it, resize it and finally export it to the Desktop (using the same name as the original image plus some suitable suffix. Problem is that I cannot find a good way of duplicate the image, anyone that knows how to do that? If I have an image (or document in AppleScript) that contains four layers – e.g. A, B, C, and D – but one of them (C) is not visible. I then want the duplicate to show A, B and D (C is allowed to be duplicated as long as it is not visible in neither the duplicate document nor exported image). The duplicate will be discarded after the image has been exported for web, I have no further use for it.

I think I've figured out the other steps, but creating the duplicate is stopping me. Any ideas?

(In case anyone is curious: the script will be triggered by StreamDeck and Keyboard Maestro.)
User avatar

2022-03-22 15:44:26

Hey Govvamuorra! Do you necessarily need to duplicate the document? I think you could undo as many actions as you want in the original file once it's exported. For example, to undo 5 actions you can type the following:
repeat 5 times
undo
end repeat
I hope I'm understanding your workflow correctly. :)
User avatar

2022-03-24 17:15:44

You ar right, I do not need to duplicate the document. It was more of a safety procedure to not have the destructive actions taken on the original. I was thinking of such an approach, but I've written enough code through the years to not trust such solutions... 😏
I will experiment a little further with this.
User avatar

2022-03-26 14:22:59

Thanks, I now have a working solution. ☺️
tell application "Pixelmator Pro"
	set theDoc to the front document
	set aspectratio to ((get the width of theDoc) / (get the height of theDoc))
	if theNewWidth = 0 and theNewHeight = 0 then
		set theNewHeight to (get the height of theDoc)
		set theNewWidth to (get the width of theDoc)
	end if
	if theNewWidth = 0 then
		set theNewWidth to (theNewHeight * aspectratio) as integer
	end if
	if theNewHeight = 0 then
		set theNewHeight to (theNewWidth / aspectratio) as integer
	end if
	
	tell theDoc
		trim canvas mode transparency
		resize image width theNewWidth height theNewHeight algorithm lanczos
		set desktopPath to (path to desktop as text)
		set theFile to desktopPath & the name & ".png"
		export for web to theFile as PNG
	end tell

	tell theDoc
		undo -- resize
		undo -- trim
	end tell
end tell
User avatar

2022-03-28 14:23:21

Sweet! Glad to hear you got it all working. :raised_hands: