SOLVED Access clipboard content in BaseEventTool on paste



  • Hi,

    I want to make sure when I paste some anchors while my tool is active, their names don't already exist in the glyph. Is it possible to access clipboard content in BaseEventTool on the paste method?

    Thanks,
    Bahman


  • admin

    this is pretty cool:

    import AppKit
    import defcon
    from fontTools.ufoLib.glifLib import writeGlyphToString, readGlyphFromString
    
    # get the paste board
    pasteboard = AppKit.NSPasteboard.generalPasteboard()
    # get the data
    possibleGlifXML = pasteboard.stringForType_(AppKit.NSPasteboardTypeString)
    # maybe its glif xml, could also just be normal text
    print(possibleGlifXML)
    # get a dummy glyph
    dummyGlyph = RGlyph()
    succes = False
    try:
        # try to read the xml
        readGlyphFromString(possibleGlifXML, dummyGlyph, dummyGlyph.getPointPen())
        succes = True
    except Exception as e:
        # if not valid fail
        print(f"Not valid glif xml!\nerror: {e}\ndata: \"{possibleGlifXML}\"")
    
    if succes:
        # do something with the glyph
        dummyGlyph.moveBy((50, 50))
        # get the glif xml
        glifXML = writeGlyphToString(dummyGlyph.name, dummyGlyph, dummyGlyph.drawPoints)
        print(glifXML)
        # clear the past board
        pasteboard.clearContents()
        # write it back in to the paste board
        pasteboard.setString_forType_(glifXML, AppKit.NSPasteboardTypeString)
    

Log in to reply