Invert selection in Font View Script

I’ve tried to write a simple script to do this but it doesn’t work, I adapted something that works for Robofont. I’d be grateful if anyone would point out why it’s not working?

(Also is there a way to script in Glyphs App with a terminal that I can ‘print’ results with?)

#MenuTitle: Invert Selection
""“Invert selection.”""

import GlyphsApp
import robofab

font = Glyphs.font
for g in font:
if not g.selected:
g.selected = True
else:
g.selected = False

Your code should work, but it would be very slow. You don’t need the “import robofab”

Try this (it is not pretty but should work)

font = Glyphs.font
ArrayController = font.parent.windowController().glyphsController()
SelectionIndexes = ArrayController.selectionIndexes()
NewSelectionIndexes = NSMutableIndexSet.indexSet()
for i in range(len(font.glyphs)):
(tav)if not SelectionIndexes.containsIndex_(i):
(tav)(tav)NewSelectionIndexes.addIndex_(i)
ArrayController.setSelectionIndexes_(NewSelectionIndexes)

If you print to standard out, it will appear in Window > Macro Panel (Cmd-Opt-M). You can invoke the Macro Panel through code as well. Put this in your script:

Glyphs.clearLog() #clears the lower half of the Macro Panel
Glyphs.showMacroWindow() #opens the Macro Panel and brings it in front
print “Output!”

Thank you mekkablue!!

Georg Seifert

It didn’t work when I tried running this:

returns the following error: and even trying to run returns

For RoboFab script, it is better to import the RoboFab wrapper from https://github.com/schriftgestalt/Glyphs-Scripts
Put the objectsGS.py in the root folder of your scripts and instead of robofab, import objectsGS at the beginning of your .py.

But, seriously, why RoboFab? It makes no sense if you try to call an app function of Glyphs. Actually, Glyphs has its own Python API: http://docu.glyphsapp.com

According to this, you can rewrite your script like this:
https://gist.github.com/mekkablue/835bbb27db5b989a7a35

The script that Georg posted above also works without RoboFab. The code may look a little weird to a Python scripter, because it directly uses app functions through the Python ObjectiveC bridge. This makes the script very fast.

This is not true. You need to from robofab.world import CurrentFont

That loads the Glyphs wrapper.

The original script did not work because it was mixing RoboFab and the internal API and that would not work.

Thanks for the feedback.

Why RoboFab? I just took a class in Python around Robofab/Robofont scripting with Joancarles Casasín and Gustavo Ferreira. They explained that if a font editor supports Robofab, then it is possible to run scripts written for Robofab (but I understand that scripts for Robofont can’t be simply copy-pasted into Glyphs and be expected to work!).

I Would’ve loved to taken one for Glyphs though…!

Most scripts written for roboFab still use some native API for things that are not covered by roboFab (like multiple master in Glyphs). So you usually need to adjust a few little things. And sometime you run into the situation where the app does not implement something properly.