Menu

Accessing the clipboard

Help
2011-05-02
2016-10-16
  • Michael Brock

    Michael Brock - 2011-05-02

    After  many years of using NoteTab Pro as my main editor, I'm in the process of switching to notepad++. One feature I use often in NoteTab scripting is the ability to manipulate the contents of the clipboard.  For example, take the contents of the clipboard, add text to it (such as debugging code) , and insert that at the current caret point.   I spent quite a while looking for the equivalent ability in notepad++ with no luck.  Is it possible?

    I'm using Windows XP.

     
  • Dave Brotherstone

    You're in the right place :)

    Assuming you've installed Python Script from the plugin manager, you can paste the contents of the clipboard with editor.paste(), and add text at the caret position with editor.addText('some text here').  Getting the contents of the clipboard (if you want to change the contents, and not just add stuff before and after it).

    If you need to get the clipboard, you need (as far as I know) the win32clipboard module, from the pywin32 project (http://sourceforge.net/projects/pywin32/files/).  As the python27.dll for Notepad++ is compiled differently to the standard python27.dll, 3rd party modules don't work properly, so you have to go through a couple of steps to get it working.

    You should install a standard python 2.7 (www.python.org).  Then, copy the python27.dll from your windows\system32 directory to the c:\program files\notepad++ directory.  Then you can install the pywin32 project (get the download for python 2.7).

    Then you can do

    import win32clipboard
    win32clipboard.OpenClipboard()
    text = win32clipboard.GetClipboardData()
    

    Hope that helps,
    Dave.

     
  • Michael Brock

    Michael Brock - 2011-05-02

    Worked perfectly!   I had found the win32clipboard  module was missing the piece to get it to work with Python Script.

    Thanks!  This really opens things up.

    Michael

     
  • Sasumner

    Sasumner - 2015-05-21

    I think I found a bug with editor.paste() myself, so I'm going to switch to the above technique. What I'm trying to do is write something to paste in the contents of the clipboard and then leave the caret sitting at the beginning of the pasted text and not the end which is the default N++ behavior.

    What I found was that even though I reposition the caret, something in N++ doesn't record it correctly. When I move off the line where the paste occurs, the caret jumps back to the column of the end of the paste. After some moderately successfully (but not totally successful) techniques to compensate, I'm thinking that using the win32 clipboard functions as above would be a whole lot easier and straightforward.

     
  • Dave Brotherstone

    This is a scintilla feature. You need to call "chooseCaretX()" after your paste, in order to set the current "sticky" X position. This is reset by every manual move of the cursor, so you can only reproduce it in a script.

    The following script works-for-me :)

    pos = editor.getCurrentPos();
    editor.paste()
    editor.gotoPos(pos)
    editor.chooseCaretX()
    
     
  • Sasumner

    Sasumner - 2015-05-26

    Wow. Super-simple once how things work is known! Thanks, Dave.

    I ended up still doing my task with the win32clipboard routines, simply because it is easier to figure out the length of the clipboard text this way (needed to position the cursor at the beginning of the paste data).

    However, I'll retain the .chooseCaretX() technique for the next time it comes up.

     
  • Michael Brock

    Michael Brock - 2015-06-25

    I am in the process of moving to a new PC. This is the 3rd or 4th time I have had to move Notepad++ and it is a pain every time! I attempted to follow the same procedure above to get the win32clipboard to work but now when I move the python27.dll from /windows/system32/ to /notepad++/ I get an error:

    Load Library is failed.
    Make "Runtime Library" setting of this project as "Multi-threaded(/MT)" may cure this problem.

    This plugin is not compatible with current version of NOtepad++.

    It then uninstalls pythonscript from Notepad++.

    All of my other scripts (finally) work well before I copy python27.dll over.

    Any suggestions?

     
  • Sasumner

    Sasumner - 2016-05-18

    This appears to be a dead thread but I am now trying to get the win32clipboard technique described above going on a new PC and am having problems. Specifically, after doing what I think is the right thing, an import of win32clipboard results in it not being found.

    Reading Dave B's 2011 post above, following the directions seems simple enough, but I'm wondering what really goes on at runtime. Does copying the non-Pythonscript DLL over the Pythonscript one cause Pythonscript to somehow point back to the standard Python27 install (typically in C:\Python27) and therefore have access to the win32clipboard installed with pywin32? I don't see how that (replacing the DLL) would do it...

    Anyway, it currently doesn't work for me, which is frustrating because with the last PC I had it was really easy to get it working.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.