Align to left/right edge of flip horizontally with duplicate.

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

2020-10-28 08:51:02

Here is example of AppleScript of a function that I think Pixelmator Pro should have as feature.

To be able to flip/mirror a shape align to left to right. In other words mirror a shape clone to left or right
It means it would be possible to draw a shape and mirror the copy and merge together.

The way flip horizontally or vertically works in current version of Pixelmator Pro is not so useful because the position is always wrong.

The problem with my script will be when flip horizontally make width to (negative number). Because now its not possible to
use width or height to position a shape on the screen because the value will be wrong.

The great thing with clone... its possible to edit 1 layer shape to effect the children (clones)

That feature is not possible in Pixelmator Pro.
property alignRightEdge : true
property alignLeftEdge : false

tell application "Pixelmator Pro"
	tell front document to tell current layer
		set theName to its name
		set {theWidth, theHeight} to {width, height}
		set {xPos, yPos} to position
	end tell
	(**
	* Align to left edge of flip horizontally with duplicate.
	*)
	if (alignLeftEdge is true) then
		set alignLeftEdge to {xPos - theWidth, yPos}
		tell front document to duplicate layer theName
		tell front document to tell current layer
			flip horizontally
			set position to alignLeftEdge
		end tell
	end if
	(**
	* Align to right edge of flip horizontally with duplicate.
	*)
	if (alignRightEdge is true) then
		set alignRightEdge to {xPos + theWidth, yPos}
		tell front document to duplicate layer theName
		tell front document to tell current layer
			flip horizontally
			set position to alignRightEdge
		end tell
	end if
end tell
User avatar

2020-10-28 10:35:43

I was able to use abs from JavaScriptCore to make a negative width value to positive.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "JavaScriptCore"
use scripting additions

repeat 7 times
	my alignDuplicate:"right"
end repeat

on alignDuplicate:_align
	tell application "Pixelmator Pro"
		tell front document to tell current layer
			set theName to its name
			set {theWidth, theHeight} to {width, height}
			set {xPos, yPos} to position
		end tell
		
		-- Align to left edge of flip horizontally with duplicate.
		if (_align = "left") then
			set alignLeftEdge to {xPos - (my doJsMath("abs", theWidth)), yPos}
			tell front document to duplicate layer theName
			tell front document to tell current layer
				flip horizontally
				set position to alignLeftEdge
			end tell
		end if
		
		-- Align to right edge of flip horizontally with duplicate.
		if (_align = "right") then
			set alignRightEdge to {xPos + (my doJsMath("abs", theWidth)), yPos}
			tell front document to duplicate layer theName
			tell front document to tell current layer
				flip horizontally
				set position to alignRightEdge
			end tell
		end if
		
		-- Align to top edge of flip horizontally with duplicate.
		if (_align = "top") then
			set alignRightEdge to {xPos, yPos - (my doJsMath("abs", theHeight))}
			tell front document to duplicate layer theName
			tell front document to tell current layer
				flip horizontally
				set position to alignRightEdge
			end tell
		end if
		
		-- Align to bottom edge of flip horizontally with duplicate.
		if (_align = "bottom") then
			set alignRightEdge to {xPos, yPos + (my doJsMath("abs", theHeight))}
			tell front document to duplicate layer theName
			tell front document to tell current layer
				flip horizontally
				set position to alignRightEdge
			end tell
		end if
	end tell
end alignDuplicate:

on doJsMath(theMethod, argsList)
	if class of argsList is not list then set argsList to {argsList}
	set theContext to current application's JSContext's new()
	set theMathObject to theContext's objectForKeyedSubscript:"Math"
	return (theMathObject's invokeMethod:theMethod withArguments:argsList)'s toDouble()
end doJsMath
User avatar

2020-10-28 11:52:46

Hey Fredrik, I guess right now the correct mathematical value of the (internal) transformation is preserved but you're right, it can most probably be considered a bug. Good call using the JS Math.abs() function, I certainly can't think of a better way.

P.S. I've passed your feedback on to the team!