[Tutorial] Advanced automation and scripting with AppleScript

Discuss and share Pixelmator Pro resources such as templates, mockups, and tutorials.
User avatar

2022-03-04 14:37:29

hello sorry evry time i used to run this code
tell application "Pixelmator Pro"
	display alert "Hello, world!"
end tell
in apple script i will have this error
Syntax Error
Internal table overflow.
what should i do
User avatar

2022-03-04 15:12:18

by hossein 2022-03-04 11:38:35 hello sorry evry time i used to run this code

in apple script i will have this error
Syntax Error
Internal table overflow.
what should i do
I think there was an issue in an older version of Pixelmator Pro with AppleScript generally, but everything should be working in the latest one – which version of Pixelmator Pro are you using? Also, which version of macOS are you on?
User avatar

2022-03-12 19:56:55

Remove horizontal slice of an image and fill its void by the surrounding areas from above and/or below

I inquired how to do this as a batch process at StackExchange and already got an answer how to do this with a Photoshop action. Would this be possible in Pixelmator with an AppleScript? If so, could you provide such a script or at least the rough directions?

Image
User avatar

2022-03-17 08:45:08

by abitofmind 2022-03-12 17:56:55 Remove horizontal slice of an image and fill its void by the surrounding areas from above and/or below

I inquired how to do this as a batch process at StackExchange and already got an answer how to do this with a Photoshop action. Would this be possible in Pixelmator with an AppleScript? If so, could you provide such a script or at least the rough directions?

<p><img src="https://i.stack.imgur.com/DUMqU.png" class="postimage is-shown is-visible" alt="Image" width="1160" height="540"></p>

Hey there! This script here should work for this crop:
tell application "Pixelmator Pro"
	activate
	tell the front document
		draw selection bounds {0, 140, 10000, 10000}
		cut
		paste
		deselect
		set position of the current layer to {0, 50}
		trim canvas mode transparency
		merge all
	end tell
end tell
You can further tweak the script to include or ask for a document save location, or create a Quick Action out of it — all depending on what you need for your workflow. Hope that helps!
User avatar

2022-03-18 16:21:52

@aurelija thanks for this! Reading the code already gives me an imagination of what will run in the GUI. Pretty intuitive how it's done! Thanks!
User avatar

2022-03-18 16:29:57

Setting a very high number of 10000 effectively achieves the selection to contain the remaining width/height in your script. Are there also variables like canvas.height, canvas.width, layer.height, layer.width, etc? (to include them in expressions/calculations)
User avatar

2022-03-22 11:22:13

by abitofmind 2022-03-18 14:29:57 Setting a very high number of 10000 effectively achieves the selection to contain the remaining width/height in your script. Are there also variables like canvas.height, canvas.width, layer.height, layer.width, etc? (to include them in expressions/calculations)
Ah, yes — if you'd like to define the exact boundaries of a layer in your selection, you can assign the layer width and layer height values like so:
tell application "Pixelmator Pro"
	activate
	tell the front document
		set layerWidth to width of the current layer
		set layerHeight to (height of the current layer) - 140
		draw selection bounds {0, 140, layerWidth, layerHeight}
		cut
		paste
		deselect
		set position of the current layer to {0, 50}
		trim canvas mode transparency
		merge all
	end tell
end tell
User avatar

2022-09-19 18:58:08

Can someone help me with changing the color of a newly inserted text layer. My first attempt is commented out below, and my second attempt was going to be via a gradient fill shape. I've got a start below, but now stuck on the coloring.
#
# Create a layer with text
#
set myColor to {10000, 12543, 23808}
set myColor1 to {10000, 12543, 23808}
set myColor2 to {14400, 7643,  9808}

tell application "Pixelmator Pro"
	activate
	tell the front document
		set myAlpha to {"Test1", "Test2"}
		repeat with myLetter in myAlpha
			
			set myText to make new text layer at the beginning of layers with properties ¬
				{text content:myLetter}
			tell the text content of myText
				--set its color to myColor # Can't get this to work?
				set its size to random number from 300 to 1000
				set its font to "Kai"
			end tell
                        set position of myText to {random number from 200 to 2800, random number from 100 to 1400}

                        convert into shape myText
                        --set its fill to gradient myColor1 to myColor2# Not sure this is possible? 
		end repeat
	end tell

end tell

Thanks - huge fan of Pixelmator pro, and just getting started with Apple Scripting.
User avatar

2022-09-20 08:42:17

Hi there, the fact that the color isn't changed looks to be a bug that probably appeared after the latest round of improvements to text layers. We'll do our best to fix it ASAP, but as a workaround, you can also change the color of text using layer styles:
tell the styles of myText
	set its fill color to {0, 0, 0}
end tell
Unfortunately, it isn't possible to use gradient fills, but that's something we can look into in the future!
User avatar

2022-09-20 16:50:36

Thanks for the workaround "fill color"! Glad to see this until the other bug is repaired.

Another question on the automation front. Is there any way to automate the create of new shape based on a custom design. I see the following in the tools, but no way to do it for a custom shape:
- rectangle shape layer
- rounded rectangle shape layer
- ellipse shape layer
- polygon shape layer
- star shape layer
- line shape layer

I would like to automate creation and placement of a custom shape
* myCustom shape layer

Thanks! Having fun learning to automate with applescript.
User avatar

2022-09-23 03:48:00

Follow up question - is there any way to mirror a layer or object (flip horizontal/vertical) via Apple Script? But not the whole document (flip horizontally document)?
User avatar

2022-10-23 15:29:40

by ShaTy Follow up question - is there any way to mirror a layer or object (flip horizontal/vertical) via Apple Script? But not the whole document (flip horizontally document)?
From the AppleScript Dictionary:

The following classes respond to the flip horizontally command:
document layer rounded rectangle shape layer star shape layer
shape layer group layer ellipse shape layer line shape layer
image layer rectangle shape layer polygon shape layer text layer

So you simply tell any of these objects to flip horizontally (or vertically).
User avatar

2022-11-12 02:13:07

Thanks blacknwhite.

Is there anyway to resize a layer. I haven't had any luck performing a resize of a single layer, and have been opening a new document - pasting a layer - and then using [resize image width]. Then copying the smaller image back to the original document.

Any suggestions to improve this?
	tell newDoc
	    select newGroup
	     cut
	end tell

	set smallDoc to make new document ¬
		with properties {width:2000, height:2000}
	tell smallDoc
	        paste
		delay 0.1
		delete layer 2
		resize image width (2000 * myScalingFactor)
		select layer 1
		copy
		close saving no
	end tell

	tell newDoc
		paste
	end tell

User avatar

2022-11-12 07:50:02

by ShaTy Thanks blacknwhite.

Is there anyway to resize a layer. I haven't had any luck performing a resize of a single layer, and have been opening a new document - pasting a layer - and then using [resize image width]. Then copying the smaller image back to the original document.

Any suggestions to improve this?
OK, ShaTy. Several things:

What kind of error do you get? What do you expect to happen? What does not happen?

Did you realize that you can resize an image or resize a canvas?
Did you realize that you can tell an image layer to set the width?

Both of the following extremely trivial scripts work, with very different effects of course (just drag an image on PM Pro and run one of them to see what happens):
tell application "Pixelmator Pro"
	tell document 1
		resize image width 2000
	end tell
end tell

tell application "Pixelmator Pro"
	tell document 1
		tell image layer 1
			set width to 2000
		end tell
	end tell
end tell
At the moment, I do not understand why you do the cut, paste, copy, paste steps. Is this something you have to do because of your workflow?

Could you please send me your original .pxd sample file (or files) by mail to the following adress:

xxx@xxx.xx

Please make sure to pack the file(s) as a zip-archive (right click and choose compress).


Thanks!
User avatar

2022-11-13 02:40:39

Oh nice! I'm a rookie at Applescript, and automation with Pixelmator.

I was hitting errors trying to resize a layer using the wrong keyword "resize". As you pointed out I just needed to use "set width" on the layer instead. That works great and is exactly what I needed to avoid those annoying steps of opening a new document, resize the whole document, then copy back the layer to the original to resize a single layer. Much cleaner.

Thanks blacknwhite! My script is simply converting a text layer to a shape, applying a pattern fill with a clipping-mask, and then resizing it by myScalingFactor. Easy stuff, but helpful to have an automation utility for me.

Thanks!
User avatar

2022-11-13 06:59:25

Great! I'm happy that my tiny tidbits were of help.

AppleScript is rather awkward but immensely helpful. It was designed to create a natural programming language for inter application communication, but it has many quite unnatural oddities, very often due to bad implementation within the applications who support it. PM Pro's implementation is quite good, however.

In principle AppleScript newbies should keep in mind:

• Read the AppleScript dictionaries of the applications you want to script carefully.

• Conceptually, AppleScript "talks" to objects, i.e. tell application "X", and within this frame tell window "X" or tell document "X", and again within that tell layer "X" (in PM Pro) or tell folder "X" or tell file "X" [in the Finder] and so on.

• The respective AS dictionaries tell you about (a) what to talk to [classes] (b) the hierarchies [of classes] and (c) what to tell [commands] in great detail.

• If something does not work, try to isolate the problem in a new script window. A good approach is to start using minimal tell statements for what you intend to do and use "get properties" and study the result to find out all the information you can get from the object you are working with. The output is the practical image of the theoretical description in the AS dictionary. "get properties" gives you a summary of all the askable properties of an object and their actual values. Like this:
tell application "Pixelmator Pro"
	tell document 1
		get properties
	end tell
end tell
or this
tell application "Pixelmator Pro"
	tell document 1
		tell image layer 1
			get properties
		end tell
	end tell
end tell
• If you really want to do work with AS go and get Scipt Debugger by Late Night Software. It's worth every cent.

• There are many resources on the web (some of them pitifully out of date, e. g. scripting additions (= osax files) no longer work in modern Apple OSes).

• This site is very helpful:
https://developer.apple.com/library/arc ... index.html
Simply ignore the passages about JavaScript if you want to focus on AS.
by ShaTy Oh nice! I'm a rookie at Applescript, and automation with Pixelmator.

I was hitting errors trying to resize a layer using the wrong keyword "resize". As you pointed out I just needed to use "set width" on the layer instead. That works great and is exactly what I needed to avoid those annoying steps of opening a new document, resize the whole document, then copy back the layer to the original to resize a single layer. Much cleaner.

Thanks blacknwhite! My script is simply converting a text layer to a shape, applying a pattern fill with a clipping-mask, and then resizing it by myScalingFactor. Easy stuff, but helpful to have an automation utility for me.

Thanks!
User avatar

2023-08-30 07:00:30

Wow, such a useful thread!

I'm trying to move to Pixelmator Pro full-time, and one script I need is something that will reduce the selected layer in size by 10% (or 30%), since I am so often shrinking down a layer to move it somewhere useful in the image. I've played with the dictionary but can't find any commands for re-scaling a layer. Can anyone chime in with assistance?
User avatar

2023-08-30 14:57:15

Unverfied pseudo code:

tell the layer in question
to get its width and height into respective variables
calculate the 10 or 30% you want to shrink for each of them
set the layers width resp. height to the respective results of your calculation

Sorry, I am in a bit of hurry, but this should be easy to accomplish.
I hope this will do what you intend to do.
There is, in fact, no command to shrink a layer.

Please google for "Pixelmator Pro Library" to find a script library full of Apple scripting examples on a (public) LateNightSoftware user forum.
by ppayne Wow, such a useful thread!

I'm trying to move to Pixelmator Pro full-time, and one script I need is something that will reduce the selected layer in size by 10% (or 30%), since I am so often shrinking down a layer to move it somewhere useful in the image. I've played with the dictionary but can't find any commands for re-scaling a layer. Can anyone chime in with assistance?
User avatar

2023-10-24 05:05:37

I think I might have uncovered a logical bug somehow. I love that I can draw a selection on my image, which I need for automation of products images, but this (I think) should draw a box 500 px wide and 1000 px tall in my image...but for some reason the right edge is all the way to the right of the image (likely even farther if the image were that size).

The script:

tell application "Pixelmator Pro"
set theDoc to front document
tell theDoc
resize image height 1200 resolution 96 algorithm ml super resolution
set theWidth to width
set theHeight to height
set ourWidth to theWidth / 2
set ourStart to ourWidth - 250
set ourEnd to ourStart + 500
draw selection bounds {ourStart, 100, ourEnd, 1000}
end tell
end tell

The after-running-the-script image:

Image

Arent the numbers just X,Y of the left upper corner, and the lower right corner?
User avatar

2023-10-24 07:59:08

by ppayne
Arent the numbers just X,Y of the left upper corner, and the lower right corner?
Nope, one is the upper left corner, the two other ones are the vertical and horizontal distance (lengths of the sides of the frame) relative to the new upper left corner. I guess. Just don't have the time to check it, so play around with this info, you'll surely find out.
User avatar

2023-10-25 19:07:14

Hello! it is posible a script to select image folder
and then Scale and move image (repeat with all images) to fit selected area inside guides

100px left
50px right
40px up
80px down

and select a folder to save all the images?
User avatar

2024-01-12 19:18:09

I'm using Pixelmator Pro Version 3.5.3 (Flare)

I need to use the "High Pass" Effect in Applescript automation and it doesn't seem to be supported.
I also need to use the "Threshold" Effect in Applescript and it doesn't seem to be supported either.

Is there any way to use these 2 Effects in Applescript today?
Any reason they aren't already there?

From the Applescript Dictionary I see support for many many effects. Just a few:
* gaussian effect n [inh. effect > item] : A gaussian blur effect.
* box effect n [inh. effect > item] : A box blur effect.
* disc effect n [inh. effect > item] : A disc blur effect.
...
* image fill effect n [inh. effect > item] : An image fill effect.
* pattern fill effect n [inh. effect > item] : A pattern fill effect.

Question:

Can you add these missing ones to the Applescript dictionary in the next release?
* high pass effect n [inh. effect > item] : A high pass effect
* threshold effect n [inh. effect > item] : A threshold effect
- any any other missing effects?

Thanks!
User avatar

2024-01-16 12:31:15

@ShaTy I am trying to reproduce the effects described in their tutorial video https://www.youtube.com/watch?v=qZI3j-cOh0E so need to have access to the displacement map and color controls effects as well as the High Pass one that you want to use.

Does anyone know if they are currently available or are coming soon?