Peek Inside Windows Clipboard (Beginning AutoHotkey Tip)

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?

ComputorEdge AutoHotkey E-Books*         *          *

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%

ClipboardPeekIn 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.

ClipBook Viewer
ClipBook Viewer displays the contents of the Windows Clipboard regardless of the data type—in this case a bitmap image.

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:

^F9::Run, clipbrd

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.

 

 

Advertisements

6 thoughts on “Peek Inside Windows Clipboard (Beginning AutoHotkey Tip)

  1. I’ve wracked my brain trying to create a hotkey script that allows me to paste or move a preset file onto any folder or text box currently in focus. This is what I have, but no luck so far:

    [Hotkey]:
    ClipSaved := ClipboardAll
    FileRead, Clipboard, *c C:\Users\edcastil\Pictures\File.png
    Clipboard := ClipSaved
    return

    Not sure what’s wrong.

    Like

    • Hi, Edwin,

      Other than the single colon after the Hotkey (should be a double colon), you need to use the right tool for the job.

      To move files, use the FileMove command or the appropriate variation found at the bottom of the page (FileCopy, FileCopyDir, FileMoveDir, FileDelete).

      To move the contents of a file saved to a variable, use the Send command (Send, ^v for paste). Also see ControlSend.

      Like

      • Thanks for the input Jack.

        The only reason why I didn’t use FileCopy or FileMove is because they require a defined destination…unless there’s a way I’m not aware of to paste onto whatever window I’m at.

        I don’t want to move it to a specific folder; I just want it to grab a specific file and paste it into the window currently in focus. So if I have a remote window open, I should be able to paste it in there, as well as if I want to paste it into an email message, or whatever application will allow me to.

        Like

  2. If you use Send, ^v for paste, then it will paste into the active window. You just need to include the Send command before you reset the clipboard to the original contents (i.e after the FileRead and before the Clipboard := ClipSaved).

    Like

    • For some reason I still can’t make this work; I’ve tried with different variables, played with the FileRead options, changed the file, changed the hotkey, even removed the clipboard reset functions, to no avail. Every time I run it, it either pastes the script, errors out saying the file is not found, or nothing at all. Not sure the FileRead command is doing anything for me.

      Like

  3. I should have looked a little closer rather than just pointing you toward specific commands. There are a couple of issues with your script (other than the lack of a Send command):

    [Hotkey]:
    ClipSaved := ClipboardAll
    FileRead, Clipboard, *c C:\Users\edcastil\Pictures\File.png
    Clipboard := ClipSaved
    return

    First, when you use the *c option in the FileRead command, I believe that AutoHotkey looks for a CLIP file saved with the AllClipboard variable. You should be able to remove the *c option.

    Second, there probably was not enough time for the clipboard to load. For that use the ClipWait command, but first the clipboard must be empty. Something more like the following:

    [Hotkey]:
    ClipSaved := ClipboardAll
    Clipboard := ""
    FileRead, Clipboard, C:\Users\edcastil\Pictures\File.png
    ClipWait 0 ; waits until empty clipboard loaded
    Send, ^v
    Clipboard := ClipSaved
    return

    I haven’t tested it but it should give you better results.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s