SOLVED Clipboard content for multiple glyphs



  • 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()
    
    


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



  • hello @bahman,

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

    thanks!


  • 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!!



  • @frederik Yes, it works, I know! And I'm wondering how RF gets the clipboard content for these multiple glyphs because I'm trying to make a paste special script that only would paste anchors or contours when I use it on selected glyphs. The current behavior replaces the whole glyphs on paste. I guess I'm just gonna wait for those notifications you mentioned. Thank you for your patience.


  • admin

    copy multiple glyphs and pasting in an font overview with the same amount of selected glyphs, already works :) see https://robofont.com/documentation/workspace/font-overview/#actions

    Or am I misreading your request...



  • Thank you, those notifications will help. I wanted to avoid redoing what RF already does. I thought RF already copies multiple glyph contents to the clipboard. Please consider doing this:

    1- Open a font which contains glyphs with outlines.
    2- Select some glyphs and hit CMD-C.
    3- Open an empty font, select similar glyphs (same glyph names) and hit CMD-V.

    Now the glyph contents are also pasted on multiple glyphs. If this data exists and can be accessed easily then I can avoid making it myself.


  • admin

    in the upcoming release and public beta there is a notification when copy pasting in a collection view:

    fontOverviewPaste and fontOverviewCopy which has a key glyphs a list of glyphs.

    would this work?



  • Currently, RF has the following behaviors:

    • cmd-c with single glyph selected: whole glyph (XML) is copied
    • cmd-c with multiple glyphs selected: literal characters are copied (based on Unicode value, if they exist. Otherwise it’s glyph names)
    • cmd-option-c with multiple glyphs selected: glyph names are copied

    Probably you’d need to re-think step 1 of your workflow, to copy RGlyphs (or XMLs) for the current selection. You could assign something like cmd-shift-c?



  • Thank you Frank. It's for a different purpose. I want to make a paste special for multiple glyphs. For example, I have selected multiple glyphs, then hit copy. Then selected another font then hit paste. Now I want to show a window where the user can choose what type of data is pasted in the new font. Anchors, contours, etc. And I want to do it for multiple glyphs not just one glyph.



  • What is the reason for using the OS clipboard in this case – why don’t you just create RGlyphs (and drop them where you need them)?
    If you really need the XML, you could use g.writeGlyphToString().