Clipboard Snippets



  • Lately I've been doing this a lot:

    • need some text based on some font data
    • write some code to print some text based on some font data
    • run the code
    • select the text in the output
    • copy the text
    • paste the text where I need the text

    I got tired of this, so I added some startup code* to RoboREPL that establishes some always-available functions to ease my laziness:

    from AppKit import NSPasteboard, NSPasteboardTypeString
    
    def toclipboard(text):
        pasteboard = NSPasteboard.generalPasteboard()
        pasteboard.declareTypes_owner_([NSPasteboardTypeString], None)
        pasteboard.setString_forType_(text, NSPasteboardTypeString)
    
    def fromclipboard():
        pasteboard = NSPasteboard.generalPasteboard()
        text = pasteboard.stringForType_(NSPasteboardTypeString)
        return text
    

    Usage:

    >>> toclipboard("\n".join(CF().selection))
    

    Then, paste it with ⌘V.

    Maybe something like this could be injected into the namespace in which all scripts are executed and/or added to mojo†?


    * >>> settings.editStartupCode()
    † …if something like this isn't already in mojo.


Log in to reply