SOLVED Clipboard content for multiple glyphs


  • admin

    Make you own copy paste:

    copy:

    import AppKit
    import json
    
    # get the pasteboard
    pasteBoard = AppKit.NSPasteboard.generalPasteboard()
    # get the font (maybe check if there is a font)
    font = CurrentFont()
    # create your pasteboard type
    bahmanPasteBoardType = "Bahman.special.copy.paste"
    # collect data
    data = []
    for glyphName in font.selectedGlyphNames:
        glyph = font[glyphName]    
        anchorData = [dict(glyphName=glyph.name, x=anchor.x, y=anchor.y, name=anchor.name) for anchor in glyph.anchors]        
        data.append(anchorData)
    # write it to the pastboard
    pasteBoard.setString_forType_(json.dumps(data), bahmanPasteBoardType)
    

    paste:

    import AppKit
    import json
    # get the pasteboard
    pasteBoard = AppKit.NSPasteboard.generalPasteboard()
    # print out all the availabe types
    print(pasteBoard.types())
    # create your pasteboard type
    bahmanPasteBoardType = "Bahman.special.copy.paste"
    # get the paste board string for type
    data = pasteBoard.stringForType_(bahmanPasteBoardType)
    # its None when there no such paste board type
    if data:
        data = json.loads(data)
        # do something with this data    
    

    good luck!!



  • hello @bahman,

    the latest beta includes the new observers fontOverviewCopy and fontOverviewPaste, please give them a try…

    thanks!



  • Thank you this is much more useful. Now we can access the clipboard content much easier!



  • I came up with this idea to store the glyphs inside an extension key and when I run the paste script, I will retrieve the glyphs from there. There is a possible drawback here since the content of the extension key can't be saved into disk, it might cause failure and other extension keys will also not get saved. Is there a way to delete the key before RF is about to be closed? I couldn't find an observer for this. This is the observer that stores the glyphs:

    from mojo.events import addObserver, removeObserver
    from defconAppKit.windows.baseWindow import BaseWindowController
    from mojo.extensions import setExtensionDefault, getExtensionDefault, removeExtensionDefault
    
    EXTENSION_KEY = 'design.bahman.specialClipboard'
    
    class ClipboardGlyphs():
    	_glyphs = {}
    
    	def start(self):
    		addObserver(self, '_fontOverviewCopy', 'fontOverviewCopy')
    		addObserver(self, '_copy', 'copy')
    
    	def destroy(self, sender):
    		removeObserver(self, 'fontOverviewCopy')
    		removeObserver(self, 'copy')
    
    	def _fontOverviewCopy(self, info):
    		self._glyphs = {}
    		for g in info["glyphs"]:
    			self._addGlyphToClipboard(g)
    		setExtensionDefault(EXTENSION_KEY, self._glyphs)
    
    	def _copy(self, info):
    		self._glyphs = {}
    		self._addGlyphToClipboard(info["glyph"])
    		setExtensionDefault(EXTENSION_KEY, self._glyphs)
    
    	def _addGlyphToClipboard(self, g):
    		if g.unicodes != ():
    			for v in g.unicodes:
    				self._glyphs[v] = g
    		else:
    			self._glyphs[g.name] = g
    
    if __name__ == '__main__':
    	ClipboardGlyphs = ClipboardGlyphs()
    	ClipboardGlyphs.start()
    	debug=True
    	if debug:
    		from vanilla import FloatingWindow
    
    		class DebuggerWindow(BaseWindowController):
    			def __init__(self):
    				self.w = FloatingWindow((500, 500), f'ClipboardGlyphs debug!')
    				self.w.open()
    				self.w.bind("close", ClipboardGlyphs.destroy)
    		DebuggerWindow()
    
    

  • admin

    Euhm dont store it in the defaults ;). If it should only be accessible while RF is running, you can store it anywhere in one of your objects.



  • I have a script that stores the glyphs when users hits copy and it runs on startup, another script runs when the user pastes.

    If I attach the glyphs to an object, I need to access that object in the paste script. And if I import the object that has the observer, it will be executed agian, adding up to previous observers. In short I don't know how to create this object you're saying, without adding another observers every time I run the paste script.


  • admin

    You'll need a controller, which is subscribed to both notifications.

    On copy you call you copy module/code, for paste the same and you provide the controller as storage for your copy glyphs.

    good luck!



  • Thanks! Finally I made this extension. Please give it a try and see if it works correctly.

    https://gitlab.com/typoman/robofont-special-clipboard


  • admin

    the url doesnt work... looking forward!



  • Sorry, I just made it public.