Copy selected segments

There is the path method copy() but I wonder if there is something quick and easy to copy only the selection (e.g. selected segments).

I tried to build something with saving the data from a source path, then initializing a new GSPath and adding points to it via insertNode_atIndex_() but this doesn’t give reliable results all the time.

What I simply want to achieve is the same behaviour as when you manually select a segment, hit cmd+c and cmd+v. … this pasted segment is what I want.

Does the method layer.selectedObjects() help?

After a quick sketch: this looks really good. I think that’s exactly what I need. Thanks so much :slight_smile:

I found a bug with this method. To replicate, please do the following steps:

  • Draw a rect with the rect tool. The starting point should be the Top-Left Corner, direction CCW.
  • Select exactly these corners as seen in the image.
  • Run this bit of code:
for path in Glyphs.font.selectedLayers[0].selectedObjects()["paths"]:
    print path

Any other selections will print some path(s) properly, while this particular selection returns an Error:

Traceback (most recent call last):
  File "<string>", line 36, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/objc/", line 162, in __getitem__objectForKey_
    return container_unwrap(res, KeyError, key)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC/objc/", line 659, in container_unwrap
    raise exc_type(*exc_args)
KeyError: 'paths'

PS: The make-code-button of this forum’s editor seems to be broken

Fixed it.