<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML lang="en">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<link href="style.css" rel="stylesheet" type="text/css">
<title>4.6.14. Notepad++ as a Clipboard Monitor</title>
<script src="show_matches.js" type="text/javascript"></script>
</HEAD>
<BODY>
<h3>4.6.14. Notepad++ as a Clipboard Monitor</h3>

<p>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++.</p>
<p>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.</p>
<p>At first, let's download and install AutoHotKey v2 ( <code>https://www.autohotkey.com/</code> ).</p>
<p>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:</p>

<blockquote>
  <pre>#SingleInstance Force

if A_Args.Length &lt; 1
{
    MsgBox &quot;This script requires a path to an output text file as an argument!&quot;
    ExitApp
}

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

OnClipboardChange ClipboardChanged  ; clipboard callback function

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

ProcessWaitClose(&quot;notepad++.exe&quot;)  ; be active while Notepad++ is active
ExitApp</pre>
</blockquote>

<p>Let's save this script file as e.g. &quot;C:\AutoHotkeyScripts\NppClipboardMonitor.ahk&quot;.</p>
<p>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 -&gt; Plugins Admin... first). In Notepad++, select Plugins -&gt; NppExec -&gt; Execute NppExec Script... and type the following in the opened dialog:</p>

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

<p>This assumes AutoHotKey v2 was installed to &quot;C:\Program Files\AutoHotkey\v2&quot; and Notepad++'s interface uses English localization. If Notepad++'s localization is different than English, please change the line</p>
<blockquote>
  <pre>npp_menucommand View|Monitoring (tail -f)</pre>
</blockquote>
<p>to the one corresponding to your Notepad++'s localization. Alternatively, you can use</p>
<blockquote>
  <pre>npp_sendmsg NPPM_SETMENUITEMCHECK IDM_VIEW_MONITORING 1</pre>
</blockquote>
<p>to enable the monitoring, and</p>
<blockquote>
  <pre>npp_sendmsg NPPM_SETMENUITEMCHECK IDM_VIEW_MONITORING 0</pre>
</blockquote>
<p>to disable the monitoring.</p>

<p>Let's save the NppExec's script above as &quot;clipboard monitor&quot;. To do it, press the &quot;Save...&quot; button, type &quot;clipboard monitor&quot; (without the quotes) and press &quot;Save&quot;.</p>
<p>Now you may want to assign a shortcut key to this NppExec's script. To do that, select Plugins -&gt; NppExec -&gt; Advanced Options... and click the &quot;Associated script:&quot; combo-box in the bottom part of the opened dialog. Select the &quot;clipboard monitor&quot; item in the drop-down list and press &quot;Add/Modify&quot;. A new item &quot;clipboard monitor :: clipboard monitor&quot; will be added to the &quot;Menu items&quot; list. When you finally press &quot;OK&quot;, there will be a message &quot;Notepad++ must be restarted to apply some of the options&quot;. This is required to add a new menu item &quot;clipboard monitor&quot; to Notepad++.</p>
<p>When Notepad++ is restarted, a shortcut key can be assigned to the new menu item &quot;clipboard monitor&quot;. In Notepad++, select Settings -&gt; Shortcut Mapper... and click &quot;Plugin commands&quot;. Find the &quot;clipboard monitor&quot; command that corresponds to &quot;NppExec.dll&quot; and assign a desired shortcut key to it.</p>
<p>Everything is ready!</p>
<p>Now either press the shortcut key assigned to the &quot;clipboard monitor&quot; menu item or select Plugins -&gt; NppExec -&gt; Execute NppExec Script... and then select &quot;clipboard monitor&quot; in the combo-box at the bottom of the opened dialog and finally press &quot;OK&quot;. You'll see an empty file &quot;clpbrd.txt&quot; opened in Notepad++.</p>
<p>Now, copy any text to the Clipboard in any program - and that exact text should be appended to the &quot;clpbrd.txt&quot; file content shown in Notepad++.</p>
<p>Final words. If you close and/or reopen this file (the &quot;clpbrd.txt&quot; one) in Notepad++ or if you restart Notepad++, don't forget to run the &quot;clipboard monitor&quot; NppExec's script again in order to monitor the Clipboard. Otherwise the &quot;clpbrd.txt&quot; will be a simple text file. (Because in fact it <em>is</em> 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 &quot;Exit&quot; to exit AutoHotKey. In such case, you'll again have to run the &quot;clipboard monitor&quot; NppExec's script manually in order to monitor the Clipboard. No magic here, just dexterous hands :-)</p>

<blockquote>
  <pre> </pre>
</blockquote>

<p>See also: Clipboard, keystrokes and much more <a href="4.6.8.html">[4.6.8]</a>.</p>

<blockquote>
  <pre> </pre>
</blockquote>

</BODY>
</HTML>
