Guides added with script behave strangely

I’m trying to write a script that would copy selected guides to all masters:

font = Glyphs.font
mylayer = font.selectedLayers[0]
glyph = mylayer.parent

for layer in glyph.layers:
	print(layer.name, layer.guides)

for selectedGuide in mylayer.guides:
	if selectedGuide.selected == True:
		newGuide = GSGuide()
		newGuide.position = selectedGuide.position
		newGuide.angle = selectedGuide.angle
		for layer in glyph.layers:
			layer.guides.append(newGuide)

It does copy them, but these guides lag when dragging them (normal guides follow the pointer, these jump to pointer position when movement stops) and I can’t delete them. Also they seem to be interconnected through masters (moving them in one master moves them in the others). What am I doing wrong? Beside adding an extra guide on top of the selected one, I’m aware of that.

You have to make a copy of the guide for each layer. Move the white line above where you instantiate the new guide (and change the indentation).

font = Glyphs.font
mylayer = font.selectedLayers[0]
glyph = mylayer.parent

for layer in glyph.layers:
	print(layer.name, layer.guides)

for selectedGuide in mylayer.guides:
	if selectedGuide.selected == True:
		for layer in glyph.layers:
			if layer == myLayer:
				continue
			newGuide = selectedGuidecopy()
			layer.guides.append(newGuide)

Thanks, I got it to work. Now I’m trying to do the same for global guides, I can print it looping over font.selectedFontMaster.guides, but .selected is always false. How can I check if they’re selected?

if guide in layer.selection:

That works for both, the local and globals. The .selected property is doing the same but needs to get the layer might be slower.