Shortcut for layer visibility

I often use a single master layer as a reference/guide along with a pasted background outline. I have the background visibility on a keyboard shortcut to quickly hide/reveal, and would like to have similar (keyboard shortcut) for the master layer visibility. I know I can mouse over to the eyewateringly tiny eye icon and click it but I find it time consuming and frustrating to keep doing this (due to the way I work, I might need to do this quite a lot). Might it be possible to have a command/shortcut that toggles the visibility of all other masters?

You can pack this into a tiny script (save it as a .py file and put it in Glyphs’s scripts folder), which you then trigger with a keyboard shortcut:

# MenuTitle: Toggle Layer Visibility
# -*- coding: utf-8 -*-

__doc__ = """
Toggle the current layer's visibility status.
"""

Layer.visible = not Layer.visible

This toggles the currently selected layer’s visibility.

thank you Sebastian, I shall give that a whirl

it works! but it only toggles the visibility of the layer I am actually working on, what I would like to do is toggle the visibility of another layer that I am using as a guide

How do you intend to indicate which layer that is?

I don’t know, I assume it’s not easy, as Glyphs has no psychic power. I only have the one other layer visible, so I thought something to toggle all other layers would do the trick

Replace the one line in my previous script with the following:

for layer in Layer.parent.layers:
    if layer == Layer:
        continue
    layer.visible = not layer.visible

this is fun, and thank you again: I could see you luring me into making scripts one day. This time it turns all layers on, but when I run it again, it just goes back to the start, with the one layer visible

is it possible to target a layer by its position in the stack, or its relationship to the active layer. Maybe “top layer” or “next layer” or “layer above”?

So your starting premise is that you have exactly two visible layers: the current (working) layer and the guide layer – no other visible layers; and you want to turn off the guide layer, but keep all other layers (including the working layer) as they are, whether visible or hidden. Is that right?

If so, this should do it:

for layer in Layer.parent.layers:
    if layer == Layer or layer.visible == False:
        continue
    layer.visible = False

That will iterate through all layers, and for each layer it will check if that layer is (a) the current working layer, or (b) already hidden; if none of those criteria is met (which should then only be the case on the guide layer), it will hide the layer.

thank you Janus. that does turn the guide layer off, but it doesn’t toggle it, so to get it back, it’s a trip to the wee stack of eyeballs. This is why I mused above about the ability above to target a layer with a known position

Yeah, if you want to toggle it, you’ll have to decide on some way to let Glyph know which layer you’re looking for, because once it’s hidden, there’s nothing to distinguish it from all the other hidden layers.

You can use its index position (zero-based, with the top layer as 0), but then if you add another layer above the guide layer, that value will change. If you’re only using these guide layers as guides, you can give them a constant, unique name and use that.

You could also store custom user data in the layer itself to indicate that it’s a layer, and then use that to find the right layer. You could have a script that toggles whether a layer is a guide layer or not, which you run whenever you make a guide layer (or turn an existing layer into a guide layer), as in the following example.

Whenever you want to convert a layer to a guide layer (or from a guide layer back to a regular layer), you would then select that layer and run this script (with a shortcut or however you want):

# MenuTitle: Toggle Layer Guidehood
# -*- coding: utf-8 -*-

__doc__ = """
Toggle whether the current layer is a guide layer or not.
"""

if Layer.userData['isGuideLayer']:
	del Layer.userData['isGuideLayer']
else:
	Layer.userData['isGuideLayer'] = True

And then the other script would look like this and would toggle the visibility of only those layers that you have converted to guide layers:

for layer in Layer.parent.layers:
    if layer.userData['isGuideLayer'] == True:
        layer.visible = not layer.visible

this is quite illuminating, thank you. I will try to get my head around that. Meanwhile, how might I adjust your script to target layer 0?

target_layer = Layer.parent.layers[0]
target_layer.visible = not target_layer.visible

you could add a color to the guide and toggle it based on that.

thank you again Sebastian, that did it!

thank you Georg: could you give me an example of how I would specify the colour in code?

I thought that you set the color manually to choose the reference layer.
But here is the docu for the layer color: Glyphs.app Python Scripting API Documentation — Glyphs.app Python Scripting API 3.2 documentation