So, now that we’ve understood the object model, we can go on to extend our Scripts menu with our own scripts.
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 an investment later, you can go with the free TextWrangler, SubEthaEdit, or Atom, which has become very popular lately.
Whichever editor you choose, create a new document by hitting Cmd-N, and save it to this folder:
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 Shaker.py 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 # -*- coding: utf-8 -*- __doc__=""" 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.
The second line is also a comment, but this time, it is Python that reads something out of it, namely the encoding of the
.py file. Unless you have a better idea, and you know what you are doing, always pick UTF-8.
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 uses 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 random.seed()
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
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
Now you can open your least favorite 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 doc string.
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.