Scripting Glyphs, part 3

by Rafał Buchner & Rainer Scheichelbauer
en zh

18 August 2022 Published on 17 July 2012

So, now that we’ve understood the object model, we can go on to extend our Scripts menu with our own scripts.

This tutorial assumes you have read Scripting Glyphs, part 1 and Scripting Glyphs, part 2 first.

You may have noticed the Scripts menu in the Glyphs menu bar. We’re going to write a script and place it into that menu. First, we need a good text editor. Personally, I like TextMate (USD 60) and SublimeText (USD 80) because those two have special features that make coding really easy and fun. Mac veterans also like BBEdit (USD 50), Smultron (USD 15), and Coda (USD 100). Chances are you already paid a license for one of those, and if that is the case, you can easily continue using that editor for Python coding. But if you are on a budget, or just want to try some coding now and decide on investment later, you can go with the free TextWrangler, SubEthaEdit, Atom, or Visual Studio Code which has become very popular lately.

Whichever editor you choose, create a new document by hitting Cmd-N, and save it to this folder:

~/Library/Application Support/Glyphs/Scripts/

The tilde (~) represents your home folder. Hint: in the save dialog, you can hit Cmd-Shift-G to activate the Go to the folder function and paste the line above. Give your script a name like Glyph because we’re going to turn our Python code from last time into a full-fledged script. Yes, the .py ending is for Python scripts. But now, to the coding …

First things first. We start by entering the menu title, declare the encoding of the file, and add a short explanation:

#MenuTitle: Glyph Shaker
Goes through all selected glyphs and slaps each of their nodes around a bit.

The first line is actually a comment since in Python, comments start with a pound sign (#). However, Glyphs interprets a comment that starts with #MenuTitle: as the menu name for the script. If your script lacks such a line, Glyphs will use the file name instead. Our script, however, will appear in the Script menu as Glyph Shaker.

In the following lines, we declare a special variable called __doc__. This stores the so-called doc string, a short documentation text about your Python object, or, in this case, your script. Triple quotes denote a multi-line string, so you can add an explanation that spans several paragraphs if you feel like it. Glyphs use the doc string for displaying a tool tip when your mouse pointer hovers over the name of the script in the Script pull-down menu.

Then, like last time, we need to get the randomizer going:

import random

Then we get all the currently selected layers and place it in the variable selectedLayers. (We don’t want to mingle with all layers/masters of all letters, only the ones we actually see.) Here we go:

selectedLayers = Glyphs.font.selectedLayers

Now, we can do stuff with selectedLayers, like step through each node of each path of each layer, and move it by a random amount between -50 and 50:

for thisLayer in selectedLayers:
    for thisPath in thisLayer.paths:
        for thisNode in thisPath.nodes:
            thisNode.x += random.randint( -50, 50 )

If you’ve done everything right, this is what it should approximately look like:

That’s it. Save your script, switch back to Glyphs and take a look into the Scripts menu. What?! Our Glyph Shaker script is not there! That’s because we first have to tell Glyphs to re-scan its Scripts folder. Here’s how: Hold down your Option key while opening the menu and the Open Scripts Folder item will turn into Reload Scripts. Afterwards, your script will show up with the name you specified after #MenuTitle:.

Now you can open your least favourite font, select the letters you hate the most, and finally give them the beating they deserve by choosing Glyph Shaker from the Scripts menu. Repeat the slapping over and over again with the Cmd-Opt-R shortcut. Take this, Helvetica!

Now take a well-deserved coffee and ice cream break. And when you feel ready for the next step, go on to read part 4 of this Python intro.

Update 2014-10-04: added encoding line, updated explanation of docstring.
Update 2016-12-08: fixed screenshot (thx Friedrich Althausen)
Update 2017-05-24: added reference to part 4.
Update 2018-12-26: added links to more text editors.
Update 2020-12-02: updated for Python3.
Update 2021-02-17: erased part about encoding (not relevant for Python 3)
Update 2022-18-08: updated title, related articles, minor formatting changes.