Horizontal flipping script butchery

Hey everyone.
I put together a script for flipping horizontally, and although this works pretty well with an assigned shortcut (I use “⌘§”), I’m sure there’s a better way to do it. So if you have any suggestions I’d love to hear them, because I’m new to all of this and I don’t really know what I’m doing.

TL;DR: trying to assign a keyboard shortcut to the horizontal flip button in the Transformations palette and this is the best I could do.

Maybe it is better not to depend on the structure and presence of a palette (which may change in the future), but rather to calculate the transformation and apply it:

import math

def transform(shiftX=0.0, shiftY=0.0, rotate=0.0, skew=0.0, scale=1.0):
	"""
	Returns an NSAffineTransform object for transforming layers.
	Apply an NSAffineTransform t object like this:
		Layer.transform_checkForSelection_doComponents_(t,False,True)
	Access its transformation matrix like this:
		tMatrix = t.transformStruct() # returns the 6-float tuple
	Apply the matrix tuple like this:
		Layer.applyTransform(tMatrix)
		Component.applyTransform(tMatrix)
		Path.applyTransform(tMatrix)
	Chain multiple NSAffineTransform objects t1, t2 like this:
		t1.appendTransform_(t2)
	"""
	myTransform = NSAffineTransform.transform()
	if rotate:
		myTransform.rotateByDegrees_(rotate)
	if scale != 1.0:
		myTransform.scaleBy_(scale)
	if not (shiftX == 0.0 and shiftY == 0.0):
		myTransform.translateXBy_yBy_(shiftX,shiftY)
	if skew:
		skewStruct = NSAffineTransformStruct()
		skewStruct.m11 = 1.0
		skewStruct.m22 = 1.0
		skewStruct.m21 = math.tan(math.radians(skew))
		skewTransform = NSAffineTransform.transform()
		skewTransform.setTransformStruct_(skewStruct)
		myTransform.appendTransform_(skewTransform)
	return myTransform

This is a code snippet, available from the Python Snippets, linked at the bottom of https://glyphsapp.com/extend: transform+tab in TextMate or SublimeText.

1 Like

You’re absolutely right it shouldn’t depend on the palette. I rewrote it using applyTransform, so further updates (hopefully) shouldn’t cause any major problems. Also thanks for the heads-up, cause I didn’t really understand what the code I copied from your comment was doing, but now I see why using the palette wasn’t such a good idea – so thank you!