About copy glyph by script

The script copies one glyph from one Glyphs file to another. If the masterId of the two glyphs is different, the replication will be problematic. If the shortcut key Command+C is used directly, Command+V is possible.
Which method should be used to copy glyphs from one file to another?

If you write a script, you have more control over which layer will be which layer in the receiving font. Copy/paste will do it by order of masters, first to first, second to second, etc.

With a script, you need to map the masterIDs yourself. So replacing the old with the new IDs.

That is to say, there is no direct method to use it. I need to match each layer of the new glyph with the current master.

You have to roll it yourself.

Which way should I replace the old layer’s associatedMasterId, use layer. associatedMasterId = MasterID It doesn’t seem to work, it just changes in seemingly memory, and it doesn’t change in the actual file.