Peek Inside Windows Clipboard, Part 2 (Intermediate AutoHotkey OnClipboardChange Tip)

An AutoHotkey OnClipboardChange Script for Running the Most Appropriate of Two Different Clipboard Peeking Apps

Last time, I discussed viewing the contents of Windows Clipboard using either the AutoHotkey Clipboard variable or an old Microsoft Clipbook Viewer (clipbrd.exe). While the AutoHotkey MsgBox command script offers superior results whenever copied files sit in the clipboard (e.g. a text list of the paths to each file), the ClipBook Viewer better displays bitmap image files. Rather than setting up different Hotkeys for each approach, wouldn’t a script which picks the best approach for the specific data type found in the clipboard help optimize viewing process? Ideally, the AutoHotkey app would use the Clipboard variable whenever the Windows Clipboard contains text and open the ClipBook Viewer whenever the clipboard holds an image. First, we must identify the type of data found in the clipboard.

ComputorEdge AutoHotkey E-Books*         *          *

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

*         *          *

Identifying  Clipboard Data Type

AutoHotkey includes a built-in label (subroutine) called OnClipboardChange which fires when copying new data to the Windows Clipboard—whether through the keyboard or an AutoHotkey script. This label allows AutoHotkey to take action whenever clipboard contents change, as well as, vary output based upon the type of data in the clipboard.

Whenever OnClipboardChange triggers, the built-in variable A_EventInfo saves one of three numeric values correlating to the clipboard data type:

  • 0 — the clipboard is empty.
  • 1 — the clipboard contains data which can be expressed as text—including a list of copied files or spreadsheet data.
  • 2 — the clipboard contains non-text binary data—often an image.

By identifying the clipboard data type with A_EventInfo, we know which viewing approach to use. Whenever A_EventInfo equals 1, the clipboard contains text (or can be expressed as text) and can be displayed with the AutoHotkey Clipboard variable and MsgBox command:

MsgBox, The clipboard contains text or file(s):`n%clipboard%
ClipboardFiles
Files copied from Windows (File) Explorer to the Windows Clipboard appear as a list of the file paths when displayed with the AutoHotkey MsgBox command and the Clipboard variable.

This includes files which, when copied to the clipboard, appear as a text list when displayed in a MsgBox (as shown in the image at right).

Note: The AutoHotkey MsgBox command as shown above also displays copied spreadsheet data, such as that found in Microsoft Excel, as text—even though it contains hidden binary formatting.

Whenever A_EventInfo equals 2, the clipboard contains binary (non-text) data—most commonly an image bitmap. In that case, the ClipBook Viewer provides a look at the image:

Run, clipbrd

This line of code opens the Clipbook Viewer displaying any image in the clipboard.

Note: The Microsoft utility program clipbrd.exe (available at the ComputorEdge AutoHotkey download page) must be available on the computer and appear in the system path per previous discussions.

Note: More advanced techniques may be used as shown below for identifying bitmap data, rather than merely non-text data (example copied from the archived AutoHotkey forum):

if DllCall("IsClipboardFormatAvailable", "Uint", 2)	;2:CF_BITMAP
	msgbox it's bitmap
else
	msgbox something else

This is useful for specifically detecting images, but it gets into the weeds of using the AutoHotkey DllCall() function and isn’t necessary here. You can find possible values for IsClipboardFormatAvailable at this Microsoft support page.

Note: The snippet above identifies copied spreadsheet data as a bitmap, although AutoHotkey returns the data type as text (A_EventInfo = 1). Both the AutoHotkey MsgBox line and ClipBook Viewer (in default mode) display the spreadsheet data as text. (In the View/Locale mode, ClipBook identifies spreadsheet data as undisplayable binary while showing the text in all other modes.)

If A_EventInfo equals 0, then the clipboard contains nothing and returns only an empty clipboard message:

MsgBox, The clipboard is empty!

Capturing Data Type

Simply include this OnClipboardChange label (subroutine) in any AutoHotkey script to save the current clipboard data type:

OnClipboardChange:
  ClipType := A_EventInfo
Return

When first loaded, the OnClipboardChange routine automatically runs once saving the current data type to the variable ClipType. From then on ClipType updates every time a change occurs in the clipboard. Now we only need to write a script loading the appropriate viewer (or displaying a “clipboard empty” message) for the clipboard data:

^F8::
  If ClipType = 1  ; Text, (including files or spreadsheet)
    MsgBox, The clipboard contains text or file(s):`n%clipboard%
  Else If ClipType = 2 ; Non-text (commonly images)
    Run, clipbrd
  Else If ClipType = 0
    MsgBox, The clipboard is empty!
Return

The Hotkey combination CTRL+F8 (^F8) activates conditionals (If…Else If) which select the appropriate response based upon the data type found in the clipboard. Depending upon the value of ClipType, AutoHotkey either runs the MsgBox command with the Clipboard variable (ClipType = 1), the ClipBook Viewer (ClipType = 2), or sends a “clipboard empty” message (ClipType = 0).

The Else If structure ensures that the routine stops checking once it finds a matching condition.

While a little more complicated than last time’s examples, using the OnClipboardChange subroutine allows us to combine the optimization of app selecting into one Hotkey.

 

 

 

Advertisements

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