Python Path in Macro Panel

I am going through some bizarre behavior in the macro panel with regards to path.

f = Glyphs.font
ROOT_PATH = '/Users/yourusername/fonts/export/'
print(ROOT_PATH) # '/Users/yourusername/fonts/export/'
for instance in f.instances:
	if instance.active:
		instance.generate(
			Format=OTF,
			FontPath=ROOT_PATH
		)

Works great.

Now, if I just make the following change, it doesn’t export to the ‘otf’ folder. After all, ROOT_PATH is a string! Why doesn’t this work!? I think I am going crazy.

import os

f = Glyphs.font
ROOT_PATH = '/Users/yourusername/fonts/export/'
OTF_PATH = os.path.join(ROOT_PATH, 'otf')
print(OTF_PATH) # '/Users/yourusername/fonts/export/otf'

for instance in f.instances:
	if instance.active:
		instance.generate(
			Format=OTF,
			FontPath=OTF_PATH
		)

The fonts still go to '/Users/yourusername/fonts/export/'!

Any ideas :slight_smile: I think I spent way too much time on this with no luck.

I also tried ROOT_PATH = '/Users/yourusername/fonts/export/' + 'otf' and surely this is a string object in python. It still goes to the export folder and never makes a new otf folder.

Doesn’t work: ROOT_PATH = '/Users/yourusername/fonts/export/' + 'otf'
Works: ROOT_PATH = '/Users/yourusername/fonts/export/otf'

My goal is to automate exports of various font formats in their own folder.

The otf folder needs to exist. Create the otf folder in Finder or with Python. Then, running your code will work.

Are you sure? If the second line works, the first must also work.

1 Like

That works! I was sorta confused because it would make the export folder automatically. If the path ended with export/otf, it would make both for me.

It is not really a Python string but an NSString. Which means you can either convert it to a str with the str() method. Or you can take advantage of PyObjC methods, e.g.:

(Scroll down for more methods.)