GUI Script with Pixelmator Pro

Early test of Shape Builder Script to use GUI Scripting to extend the function
in Pixelmator. I use layer children (elements) to select, position, rotate and set operator. Its means I could edit any shape anytime (make editable, add point…)
manually do rest in Automation.

One thing I do miss are to edit/move points with Script.

-- Select the children
my selectLayerChildByName:"Rect_1" inParent:"Rectangle"
my setPositionInX:"40" Y:"100"
my setOperatorTo:"Substract"
my selectLayerChildByName:"Rect_3" inParent:"Rectangle"
my setPositionInX:"0" Y:"50"
my setRotationTo:"15"

(**
* Set the selected layer child rotation
* setRotationTo:arg1 (degree 0 - 360)
*)

on setRotationTo:_theDegree
	tell application "System Events" to tell application process "Pixelmator Pro"
		set frontmost to true
		tell window 1 to tell group 1 to tell group 1 to tell scroll area 1
			set focused of text field 5 to true
			set value of text field 5 to _theDegree
			keystroke return
		end tell
	end tell
end setRotationTo:

(**
* Set the selected layer children operator 
* "Unite", "Substract", "Intersect", "Exclude"
* setOperatorTo:arg1 (string) 
*)
on setOperatorTo:_theString -- "Unite", "Substract", "Intersect", "Exclude"
	tell application "System Events" to tell application process "Pixelmator Pro"
		set frontmost to true
		tell window 1 to tell group 1 to tell group 1 to tell scroll area 1
			tell radio group 1
				set theOperatorList to {"Unite", "Substract", "Intersect", "Exclude"}
				click radio button (my list_position(_theString, theOperatorList))
			end tell
		end tell
	end tell
end setOperatorTo:

(**
* Set the selected layer childrens position in X and Y. 
*)
on setPositionInX:_xPx Y:_yPx
	tell application "System Events" to tell application process "Pixelmator Pro"
		set frontmost to true
		tell window 1 to tell group 1 to tell group 1 to tell scroll area 1
			set focused of text field 3 to true
			set value of text field 3 to _xPx
			keystroke return
			set focused of text field 4 to true
			set value of text field 4 to _yPx
			keystroke return
		end tell
	end tell
end setPositionInX:Y:

(**
* Select layer child by name in parent layer.
*. selectLayerChildByName:arg1 (string) inParent:arg1 (string)
*)
on selectLayerChildByName:_theChild inParent:_theParent
	-- First we select the Parent layer name with _theParent variable.
	tell application "Pixelmator Pro" to tell front document
		set theLayerList to its name of every layer
		set theIndex to my list_position(_theParent, theLayerList)
		select layer theIndex
	end tell
	
	-- Build a list so we could select corrent child.
	tell application "System Events" to tell application process "Pixelmator Pro"
		set frontmost to true
		tell window 1 to tell scroll area 2 to tell outline 1
			tell UI element of every row
				set theChildren to its name
				-- Make a list of children.
				tell UI element of every row
					set theChildrenList to {}
					repeat with i in theChildren
						repeat with j in i
							copy the contents of j to the end of theChildrenList
						end repeat
					end repeat
				end tell
			end tell
			
			-- Debug log.
			log theChildrenList
			
			-- Reveal the childrens if they are hidden.
			if (its value of checkbox 3 of UI element 1 of row (theIndex as integer) is 0) then
				click checkbox 3 of UI element 1 of row (theIndex as integer)
			end if
			
			delay 1
			
			-- Children are visible and we make a new list
			tell UI element of every row
				set theChildren to its name
				-- Make a list of children.
				tell UI element of every row
					set theChildrenList to {} -- reset the list
					repeat with i in theChildren
						repeat with j in i
							copy the contents of j to the end of theChildrenList
						end repeat
					end repeat
				end tell
			end tell
			
			-- Select the children.
			select row (my list_position(_theChild, theChildrenList))
		end tell
	end tell
end selectLayerChildByName:inParent:


--~~~~~~~~~ [Supported handlers] 

on list_position(this_item, this_list)
	repeat with i from 1 to the count of this_list
		if item i of this_list contains this_item then return i
	end repeat
	return 0
end list_position