# How to activate a layer via Python?

**URL:** <https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358>\
**Category:** Scripting\
**Created:** [November 11, 2016, 8:46pm UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358 "2016-11-11T20:46:23Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [November 11, 2016, 8:46pm UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/1 "2016-11-11T20:46:23Z")

</div>

I tried to write a Python script that activates the master layer for the currently selected master (i.e. when a non-master layer is active). It should practically simulate clicking on the layers palette. How is it done?

This is what I have tried to far but the result is not perfect:

```
from GlyphsApp import *

font = Glyphs.font
thisGraphicView = font.currentTab.graphicView()

# Switch each glyph in the current tab to its master layer.
# This is exactly the desired behaviour but it does not last.
# After clicking on a different glyph, the layer is switched back to what it was before running this script.
for layer_index in range( len( font.currentTab.layers ) ):
    layer = font.currentTab.layers[layer_index]
    glyph = layer.parent
    master_layer = glyph.layers[font.selectedFontMaster.id]
    font.currentTab.layers[layer_index] = master_layer

# does not seem to have any effect
thisGraphicView.setActiveLayer_( font.glyphs['g'].layers[font.selectedFontMaster.id] )

# redraw
font.currentTab.redraw()

# Does not work. What is it supposed to do?
# font.currentTab.layersCursor = 0

# Another redraw
Glyphs.redraw()

```

After clicking on a different glyph, the layer is switched back to what it was before running this script.

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [November 11, 2016, 11:14pm UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/2 "2016-11-11T23:14:58Z")

</div>

Try to take the layer array, add the layers you like to see and the set the whole array

```python
Layers = Font.currentTab.layers

# Add/remove stuff

Font.currentTab.layers = Layers

```

That might give strange results when you have some features active.

The activeLayer property is reset internally. It is not to be used like that.

The layerCursor is read only. It returned the cursor position in the .layers array. That can be diffeent than the text cursor with active ligatures.

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [November 12, 2016, 2:16pm UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/3 "2016-11-12T14:16:50Z")

</div>

Oh, excellent! Now it works. And layerCursor is indeed very helpful, too.

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [November 12, 2016, 2:35pm UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/4 "2016-11-12T14:35:26Z")

</div>

Just noticed that this method does not work if I try to switch to a non-master layer (or to retain non-master layers).

This code …

```
Layers = Font.currentTab.layers

# don’t touch anything

Font.currentTab.layers = Layers

```

… switches all glyphs in the tab that are on non-master layers to the respective master layer (which is not necessarily the font’s selected master).

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [November 12, 2016, 3:44pm UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/5 "2016-11-12T15:44:12Z")

</div>

I fixed it.

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [November 15, 2016, 10:34am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/6 "2016-11-15T10:34:57Z")

</div>

Thanks!

Btw, this is the script I am using this code for:

> <https://github.com/justanotherfoundry/freemix-glyphsapp/blob/master/Toggle%20Backup%20Layer.py>

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [November 15, 2016, 10:52am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/7 "2016-11-15T10:52:57Z")

</div>

I just realised that after using the script, I cannot switch all the glyphs in the tab to a different master using Cmd+1, Cmd+2 etc. any more. Any way to fix this?

Btw, I always found it _very_ annoying if I have to manually switch a glyph to the respective master layer when Glyphs thinks I want to stay on the current layer. This happens a lot and causes lots of unnecessary clicks.

Can I ask that Cmd+1, Cmd+2 etc. _always_ switches _all_ glyphs in the tab to that master layer, without keeping some glyphs stuck. That would make me very, very happy.

The above script was partly an attempt to solve this problem (at least make it a little easier to switch to the master layer) but it seems it makes the situation worse.

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [November 15, 2016, 11:16am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/8 "2016-11-15T11:16:57Z")

</div>

I realise we had that discussion before:

> [@Changing Masters doesn't correspond with correct selection in Layers Palette](https://forum.glyphsapp.com/t/changing-masters-doesnt-correspond-with-correct-selection-in-layers-palette/4457/1):
>
> See this video [http://quick.as/2yG5TpPJk](http://quick.as/2yG5TpPJk) I’m clicking around the buttons at the top but it jumps around and is not correct. It’s the same if I use Alt+[0…9]

Sorry, I still don’t see a realistic scenario in which the current behaviour is better, and I see a lot of uses in which it would be be better to always make Cmd+1 switch to the first master.

Georg, can you explain why you think the current behaviour is better?

---

<div class="post-metadata">

**Author:** ![GeorgSeifert](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/georgseifert/32/259_2.png) [@GeorgSeifert](https://forum.glyphsapp.com/u/GeorgSeifert)\
**Post date:** [November 15, 2016, 11:28am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/9 "2016-11-15T11:28:32Z")

</div>

Please have a look at the setter of `tab.layers`: [https://github.com/schriftgestalt/GlyphsSDK/blob/master/ObjectWrapper/GlyphsApp/](https://github.com/schriftgestalt/GlyphsSDK/blob/master/ObjectWrapper/GlyphsApp/) **init**.py#L7132

If you construct a string (`NSMutableAttributedString`) without attributes, the layers will follow the master selection.

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [September 12, 2017, 8:29am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/10 "2017-09-12T08:29:15Z")

</div>

Thanks for your help, Georg.

Finally got this to work:

> <https://github.com/justanotherfoundry/freemix-glyphsapp/blob/master/Toggle%20Backup%20Layer.py>

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [October 10, 2017, 6:55am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/11 "2017-10-10T06:55:31Z")

</div>

Seems like after using this script, Cmd+1, Cmd+2 to activate masters does not work any more.  
@GeorgSeifert any ideas how to fix that?

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [December 2, 2017, 10:04am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/12 "2017-12-02T10:04:57Z")

</div>

@GeorgSeifert Any Ideas how to fix this?

I find tht Toggle script immensely useful but it is annoying that it breaks the switching between masters.

---

<div class="post-metadata">

**Author:** ![Realist](https://forum.glyphsapp.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Realist](https://forum.glyphsapp.com/u/Realist)\
**Post date:** [December 2, 2017, 10:24am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/13 "2017-12-02T10:24:29Z")

</div>

Agreed. Would it be possible to fix the switching, Georg?

---

<div class="post-metadata">

**Author:** ![mekkablue](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/mekkablue/32/23_2.png) [@mekkablue](https://forum.glyphsapp.com/u/mekkablue)\
**Post date:** [December 2, 2017, 12:13pm UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/14 "2017-12-02T12:13:35Z")

</div>

The problem is that a user-selected layer must survive a master switch. Georg posted the solution above:

> [@GeorgSeifert](#):
>
> If you construct a string (NSMutableAttributedString) without attributes, the layers will follow the master selection.

You can build this into the script. Every time you switch back to the master layer of a currently active master, you can reconstruct the tab string and the cursor position/selection.

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [December 16, 2017, 10:03am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/15 "2017-12-16T10:03:20Z")

</div>

Sorry but this is all very cryptic. I tried for a while but could not make sense of your advice.

How do I construct a string (NSMutableAttributedString) without attributes?  
What do I do with it after I have constructed it?

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [December 16, 2017, 10:56am UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/16 "2017-12-16T10:56:12Z")

</div>

Okay, got this to work. Constructing a string without attibutes is not the solution, though. Will post the result later.

---

<div class="post-metadata">

**Author:** ![TimAhrens](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/timahrens/32/4309_2.png) [@TimAhrens](https://forum.glyphsapp.com/u/TimAhrens)\
**Post date:** [December 16, 2017, 7:19pm UTC](https://forum.glyphsapp.com/t/how-to-activate-a-layer-via-python/5358/17 "2017-12-16T19:19:57Z")

</div>

The updated version is on Github now. It turned out that the best approach is not to interact with `currentTab.layers` directly but _only_ to use the `NSMutableAttributedString` to control the active layers. The content of that `NSMutableAttributedString` needs to be partly with attributes and partly without.

> <https://github.com/justanotherfoundry/freemix-glyphsapp/blob/master/Toggle%20Backup%20Layer.py>
