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.
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.
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?
The problem is that a user-selected layer must survive a master switch. Georg posted the solution above:
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.
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.