append to clipboard

2. Help
batman42ca
2014-01-29
2014-02-09
  • batman42ca
    batman42ca
    2014-01-29

    I thought I saw somewhere that it's possible to select some text and append it to the clipboard instead of just replacing what's in the clipboard.

    Did I imagine that or is there some command or plugin that makes append to clipboard possible.

     
  • batman42ca
    batman42ca
    2014-01-29

    A lot of google searching before I originally posted here did not turn up any clues, but I finally stumbled upon the place where I previously saw a hint that it was possible to append to the clipboard on this Wiki page (after a day of searching I was beginning to think I was simply dreaming of this feature):

    https://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Clipboard_Uses

    So the secret is to install the TextFx plugin.

    Once that is done, from the Notepad++ main menu, it is also worth selecting

    TextFx > TextFx Viz Settings > W: +Vix Paste / Append Binary
    

    ... otherwise appending to the clipboard doesn't preserve carriage returns and pasting puts everything on the same line.

     
    Last edit: batman42ca 2014-01-29
  • batman42ca
    batman42ca
    2014-02-04

    It seems I was a little wrong about TextFx. It will append to the clipboard but it strips carriage returns and the Append Binary setting doesn't fix it. It seems I was mistaken about that.

    MultiClipboard almost does what I want but it looks like I can't assign keyboard shortcuts to enough of the commands. Ideally, I'd like Ctrl-C or Ctrl-Ins to copy to the clipboard (erasing whatever else may have been there as usual). Then Ctrl-Num-+ appends to the clipboard. Pasting everything from the clipboard would be the same as usual Ctrl-V or Ctrl-Shift-Ins.

    At the very least I'd like to assign keys to "Delete All Items" and "Paste All Items" because I always want to paste all items.

     
  • batman42ca
    batman42ca
    2014-02-09

    I hope this helps someone else. I spent a LONG time searching and experimenting to finally find a way to make this work. Here is how to get a working "append to clipboard" in Notepad++

    1) install Python Script using the plugin manager
    2) using the Plugins > Python Script > New Script, create a new script (I called it appendClipboard)
    3) copy and paste the code below into the script
    4) save the script
    5) using Plugins > Python Script > Configure, select the script you just created from the User Scripts, then click the "Add" button above the "Menu Items" box
    6) Click OK, then restart Notepad++ (can't set a shortcut key to your new script until you reset Notepad++)
    7) Select Macro > Modify Shortcut/Delete Macro...
    8) Click the "Plugin commands" button and find the python script you created in step 4
    9) Assign a key to it (I chose Ctrl + Num+).

    The way to use this is select some text as usual and copy to the clipboard as usual. select some other text, then press the append to clipboard shortcut key created in step 9 (Ctrl + Num+ for me) and the current selection will be appended to what was already in the clipboard. Keep selecting more text and keep appending to the clipboard. When you are done, just paste from the clipboard as normal.

    Now here is the Python script to do this (no external Python libraries required, tested and working with Notepad++ 6.5.3 and Python Script 0.9.2.0):

    import ctypes
    
    # this is adapted from what I found here
    # http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python/4203897#4203897
    
    #Get required functions, strcpy..
    strcpy = ctypes.cdll.msvcrt.strcpy
    user = ctypes.windll.user32
    kernel = ctypes.windll.kernel32
    
    GMEM_DDESHARE = 0x2000
    CF_TEXT = 1
    
    # -------------------------------
    # read from the clipboard
    # -------------------------------
    def readClipboard( ):
      user.OpenClipboard(None) # Open Clip, Default task
    
      # get clipboard data
      pcontents = user.GetClipboardData(CF_TEXT)
      data = ctypes.c_char_p(pcontents).value
      kernel.GlobalUnlock(pcontents)
      user.CloseClipboard()
      return data
    
    # -------------------------------
    # write to the clipboard
    # -------------------------------
    def writeClipboard( data ):
      user.OpenClipboard(None) # Open Clipboard, Default task
      user.EmptyClipboard()
      hCd = kernel.GlobalAlloc( GMEM_DDESHARE, len( bytes(data) )+1 )
      pchData = kernel.GlobalLock(hCd)
      strcpy(ctypes.c_char_p(pchData),bytes(data))
      kernel.GlobalUnlock(hCd)
      user.SetClipboardData(1,hCd)
      user.CloseClipboard()
    
    # get the clipboard contents
    clipboardText = readClipboard()
    
    # get the selected text
    selectedText = editor.getSelText()
    
    # set the clipboard to the appended text
    writeClipboard(clipboardText + selectedText)
    

    NOTE: to use Ctrl + Num+ as the keyboard shortcut, I had to clear that shortcut from the zoom in function. Also, I needed to add the following code to the startup.py file in plugins\PythonScript\scripts to fix a path issue (running a python script via a keyboard short cut wasn't working)

    Here's the code to add to startup.py, just above "import site"

    # Fix for some sys.path conflicts when user has an existing python 2.7
    # installation.
    # from here: http://sourceforge.net/p/npppythonscript/discussion/1188886/thread/3b0e506b
    newPath = []
    for pth in sys.path:
        if (pth.count(notepad.getNppDir()) > 0) or (pth.count(notepad.getPluginConfigDir()) > 0):
            newPath.append(pth)
    sys.path = newPath
    
     
    Last edit: batman42ca 2014-02-09