4.6.14. Notepad++ as a Clipboard Monitor

Let's imagine you want Notepad++ to work as a Clipboard Monitor by gathering all the text copied to the Clipboard into a single file shown in Notepad++.

We may use a third-party tool such as AutoHotKey to do all the clipboard-related work, while Notepad++'s work will be to automatically refresh the file content once any text from the Clipboard has been added to this file.

At first, let's download and install AutoHotKey v2 ( https://www.autohotkey.com/ ).

Secondly, let's write an AutoHotKey v2 script that will monitor the Clipboard and append the new text data to a single file. Let's accept the path to that single file as an input argument of the script:

#SingleInstance Force

if A_Args.Length < 1
{
    MsgBox "This script requires a path to an output text file as an argument!"
    ExitApp
}

; TempDir := EnvGet("TEMP")
; ClpbrdFilePath := TempDir "\clpbrd.txt"
ClpbrdFilePath := A_Args[1]  ; reading file path as a command-line argument
FileObj := FileOpen(ClpbrdFilePath, "w")  ; creating an empty file
FileObj.Close()

OnClipboardChange ClipboardChanged  ; clipboard callback function

ClipboardChanged(DataType) {
    if DataType = 1 {  ; text
        FileObj := FileOpen(ClpbrdFilePath, "a")
        FileObj.WriteLine(A_Clipboard)  ; appending to the file
        FileObj.Close() 
        ; ToolTip "Clipboard text: '" A_Clipboard "'"
        ; Sleep 2000
        ; ToolTip  ; Turn off the tip.
    }
}

ProcessWaitClose("notepad++.exe")  ; be active while Notepad++ is active
ExitApp

Let's save this script file as e.g. "C:\AutoHotkeyScripts\NppClipboardMonitor.ahk".

Now let's write NppExec's script that will use the AutoHotKey v2 script mentioned above. (If you don't have the NppExec plugin, install it via Plugins -> Plugins Admin... first). In Notepad++, select Plugins -> NppExec -> Execute NppExec Script... and type the following in the opened dialog:

npp_console ?
set local cbfile = $(SYS.TEMP)\clpbrd.txt
set local ah2script = C:\AutoHotkeyScripts\NppClipboardMonitor.ahk
npp_run "C:\Program Files\AutoHotkey\v2\AutoHotkey32.exe" "$(ah2script)" "$(cbfile)"
npp_close "$(cbfile)"
npp_open "$(cbfile)"
npp_menucommand View|Monitoring (tail -f)

This assumes AutoHotKey v2 was installed to "C:\Program Files\AutoHotkey\v2" and Notepad++'s interface uses English localization. If Notepad++'s localization is different than English, please change the line

npp_menucommand View|Monitoring (tail -f)

to the one corresponding to your Notepad++'s localization. Alternatively, you can use

npp_sendmsg NPPM_SETMENUITEMCHECK IDM_VIEW_MONITORING 1

to enable the monitoring, and

npp_sendmsg NPPM_SETMENUITEMCHECK IDM_VIEW_MONITORING 0

to disable the monitoring.

Let's save the NppExec's script above as "clipboard monitor". To do it, press the "Save..." button, type "clipboard monitor" (without the quotes) and press "Save".

Now you may want to assign a shortcut key to this NppExec's script. To do that, select Plugins -> NppExec -> Advanced Options... and click the "Associated script:" combo-box in the bottom part of the opened dialog. Select the "clipboard monitor" item in the drop-down list and press "Add/Modify". A new item "clipboard monitor :: clipboard monitor" will be added to the "Menu items" list. When you finally press "OK", there will be a message "Notepad++ must be restarted to apply some of the options". This is required to add a new menu item "clipboard monitor" to Notepad++.

When Notepad++ is restarted, a shortcut key can be assigned to the new menu item "clipboard monitor". In Notepad++, select Settings -> Shortcut Mapper... and click "Plugin commands". Find the "clipboard monitor" command that corresponds to "NppExec.dll" and assign a desired shortcut key to it.

Everything is ready!

Now either press the shortcut key assigned to the "clipboard monitor" menu item or select Plugins -> NppExec -> Execute NppExec Script... and then select "clipboard monitor" in the combo-box at the bottom of the opened dialog and finally press "OK". You'll see an empty file "clpbrd.txt" opened in Notepad++.

Now, copy any text to the Clipboard in any program - and that exact text should be appended to the "clpbrd.txt" file content shown in Notepad++.

Final words. If you close and/or reopen this file (the "clpbrd.txt" one) in Notepad++ or if you restart Notepad++, don't forget to run the "clipboard monitor" NppExec's script again in order to monitor the Clipboard. Otherwise the "clpbrd.txt" will be a simple text file. (Because in fact it is a simple text file). As we discussed above, all the clipboard monitoring work is done by AutoHotKey. Correspondingly, when the AutoHotKey v2 script is running, you may find the AutoHotKey's icon in the TaskBar, as well as right-click that icon and select "Exit" to exit AutoHotKey. In such case, you'll again have to run the "clipboard monitor" NppExec's script manually in order to monitor the Clipboard. No magic here, just dexterous hands :-)

 

See also: Clipboard, keystrokes and much more [4.6.8]; NppExec's Console as a Log Viewer [4.6.18].