Scripting Boolean Ops: NameError: global name 'GSPathOperator' is not defined

I’ve been trying to script the boolean operations as if to automate the clicking the transformations buttons. I’m interested mostly in returning the paths of intersections and subtractions.

I’ve found this example from @mekkablue:

but I get the error:
NameError: global name 'GSPathOperator' is not defined

I also noticed this example is used in many scripts on github and I get the same error when running such scripts (pasting into macro panel and running)

Is this method of scripting deprecated?
Is there an alternative?

It’s also troubling that the only ‘documentation’ for this method is in the forum, why can’t I find any mention of scripting the boolean operations in docu.glyphsapp.com?

Hmm. It works for me. Which app version are you running?

I added these paths to a layer:

… then ran this script in the Macro window:

pathOp = GSPathOperator.alloc().init()
p1 = Layer.paths[0]
p2 = Layer.paths[1]
p3 = Layer.paths[2]
thePaths = [p1,p2]
otherPaths = [p3]
pathOp.intersectPaths_from_error_( otherPaths, thePaths, None )
Layer.paths = None
Layer.paths.append(thePaths)

… and this is what I got:

Thank you @mekkablue for the quick response!

When I check for updates I get:

Glyphs 2.4.1 is currently the newest version available.

I replicated your test and tried the code with no luck. Same error.

What happens if you add that line:

GSPathOperator = objc.lookUpClass("GSPathOperator")

to the beginning of your script?

That seems to work, thank you.
I successfully replicated @mekkablue’s test.

Question now is why is that necessary for me?

That line is included in the wrapper of the cutting edge version. And, if you do from Foundation import * the python bridge will load all classes. But that takes a while so should be avoided.

1 Like

Just FYI I ran into some trouble with the intersection method, perhaps it’s a known problem.

I found that if the edges of the two paths you want to intersect line up, it returns no paths.

Eg:
Take two squares one smaller than the other, align them to their bottom left corners.
The intersection should be equal to the small square, but it returns nothing.

I know all these literal edge cases are hard to account for, just a heads up if you don’t know it’s behaving in this unexpected way.

Thanks for the report. I’ll have a look.

Hi,
Has something changed in the API since this ?
I’m trying to replicate Rainer’s example using his exact code (btw I’m on 2.6.2 1264 and still had to add GSPathOperator = objc.lookUpClass(“GSPathOperator”) to avoid a NameError) but every time I run the script it raises a ValueError:

Traceback (most recent call last):
File “”, line 10, in
File “GlyphsApp/GlyphsApp/init.py”, line 2429, in append
ValueError

It does the same if I try to use removeOverlapPaths_error_ or subtractPaths_from_error_

The last line of the script needs to be:

Layer.paths.extend(thePaths)

It’s working. Thanks Georg! :ok_hand:

Has this been changed again?
I cannot get it working. See here.