A Quick, Easy Way to View Windows Clipboard Contents
There are some great AutoHotkey Clipboard scripts. In particular, the free ClipJump, written in AutoHotkey and maintained by Avi Aryan, represents a major upgrade in practical Clipboard-like apps. People now use ClipJump to replace no longer supported third-party programs. But not everyone needs that much clipboard. What if you only want to take a peek at your clipboard’s contents?
New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”
* * *
Clipboard and ClipboardAll
AutoHotkey includes two built-in variables which contain the contents of the Windows Clipboard: Clipboard and ClipboardAll. While they both serve an important function, understanding the difference between the two makes certain tasks easier.
Clipboard only contains and supports text saved in the Windows Clipboard. If the clipboard contains other data types such as images or files copied from Windows File Explorer, then the Clipboard variable either returns nothing or, as in the case for copied files, the path to the file(s). Fortunately, the most common use for the AutoHotkey variable Clipboard occurs in text manipulation scripts.
The Clipboard variable works like any other AutoHotkey variable when used in commands and expressions making it much more flexible than ClipboardAll—which is one-way. Assigning a value, text, or an image to Clipboard immediate places the data into Windows Clipboard—whereas assigning Clipboard to a variable only saves text.
ClipboardAll contains any type of data saved in the Windows Clipboard—whether text, image bitmaps or files. However, unlike Clipboard, its use is limited. Other than assigning the contents of the Windows Clipboard to a variable or saving it to a file, ClipboardAll cannot be used in commands or expressions. Users commonly use ClipboardAll to save the current clipboard contents to a new variable (ClipSaved := ClipboardAll). Later they restore the data to the clipboard using the Clipboard variable (Clipboard := ClipSaved). That way images and other binary data remain intact after running the AutoHotkey routine.
Perhaps the best way to view ClipboardAll is as a one-way read-only variable. While you can assign its value to a variable or save it to a file, you cannot write to ClipboardAll. For storing data directly to the clipboard in AutoHotkey, you must save to the Clipboard variable. Curiously, any type of data can be assigned directly to the Clipboard variable (and thus placed in the Windows Clipboard), but only text can be retrieved with it.
AutoHotkey Best Practice: I’ve written numerous scripts which save the original clipboard data to a variable before copying and manipulating new data with the clipboard. The script later restores the data from that variable leaving the original clipboard contents untouched. In the examples in both articles and books, I’ve often saved the clipboard using the text-only Clipboard variable. However, for temporarily saving clipboard data, as a rule, you should use the ClipboardAll variable. This prevents the loss of data when the original clipboard content includes bitmaps or files. As an example from online AutoHotkey documentation:
ClipSaved := ClipboardAll ; Save the entire clipboard ; ... here make temporary use of the clipboard Clipboard := ClipSaved ; Restore the original clipboard ClipSaved = ; Free the memory when clip is very large.
While I don’t remember ever encountering a problem when using Clipboard, when I update the books for newer editions, I’ll make of point of using ClipboardAll for saving clipboard contents.
The contents of the ClipboardAll variable may also be saved to a file (in an AutoHotkey clipboard format) using the FileAppend command…then later restored with FileRead. However, since the binary file uses a special clipboard file format, it’s readable only by the clipboard and requires the use of the *c option when restoring the file to a variable with FileRead. Changing the clip file’s filename with a new extension won’t recover the file contents for any other format (i.e. filename.clip to filename.jpg won’t work—even if a JPG file was originally copied to and saved-to-file from the clipboard). The file extension is meaningless to an AutoHotkey clip file.
Viewing Windows Clipboard Contents
There are times when we might like to see what’s in the Clipboard. If it only contains text, then there is a simple one-line solution:
^F8::MsgBox, The clipboard contains:`n%clipboard%
In conjunction with the Clipboard variable (%clipboard%), the MsgBox command displays any text saved in the Windows Clipboard. If you copy a file (or set of files) into the clipboard from Windows File Explorer, then the above snippet returns a text list of path(s) to the files. Since it doesn’t operate in commands, inserting the ClipboardAll variable into the script returns nothing.
The Easiest Way to See Clipboard Contents
The above method for looking at clipboard contents works great for text, but not much else. We need a way to peek inside the clipboard—especially to see graphics. I looked around, but didn’t find any easy way to do it in AutoHotkey. Often we get so involved in using AutoHotkey that we forget that there may already be a simpler way to do things. That’s certainly true for peeking inside the clipboard.
Windows XP included a utility file called ClipBook Viewer (clipbrd.exe). It disappeared from later version of Windows. I’m not sure why Microsoft dropped it—I always found it pretty useful. For that reason I’ve included clipbrd.exe as download for restoring it to newer Windows computers. Add the EXE file (extract clipbrd.exe from the clipbrd.zip file) to the C:\Windows folder or any other folder in the system path. (Run ⇒ cmd, type path, then enter into the new command window to view folders in the systems path.)
Setup the following AutoHotkey Hotkey:
After loading the above Hotkey, the ClipBook Viewer utility opens (or, if already open, pops to the top) every time you press CTRL+F9.
As the contents of the clipboard changes, the latest data displays in the viewer— whether text or graphics. If the clipboard contains both graphics and text, the viewer displays the text. Unlike the first AutoHotkey snippet found above which provides a list of file paths, copying files to the clipboard returns a single abbreviated path to the file or group of files in the viewer. For copied files I like the AutoHotkey MsgBox script better since I can see a complete list of the files.
With the exception of viewing copied file(s), the ClipBook Viewer provides a better look at the clipboard contents. You can actually see any copied images. It provides an excellent way to quickly look at clipboard data.
While the ClipBook Viewer does save to and restore from a CLIP file, the format is not compatible with the AutoHotkey clipboard file format. Use the FileAppend/FileRead technique discussed above for saving and recovering clipboard contents in AutoHotkey.
Each approach (the AutoHotkey MsgBox command and the ClipBook Viewer) has its advantages. Next time, I’ll look at a way to get the best of both with one Hotkey.