Subscribe to Windows IT Pro
June 12, 2006 12:00 AM

Capture the Clipboard’s Contents

The Win32::Clipboard extension makes it easy
Windows IT Pro
InstantDoc ID #50376
Rating: (0)
Downloads
50376.zip

As callout A in Listing 1 shows, the script starts by creating a Win32::Clipboard object ($Clip). If the creation fails, the script terminates. You can use this object for method calls by using code such as

$Clip->Get() 

Alternatively, if you don't create a Clipboard object, you can use the full namespace in method calls by using code such as

Win32::Clipboard::Get() 

Because there might already be content on the Clipboard, the code at callout A clears any pending change notifications by calling the WaitForChange() method. Notice that a timeout value of 100 milliseconds is passed to this method. A timeout value is included in case no change notification is pending. Otherwise, the script might wait indefinitely and never enter the main loop.

Callout A ends with the beginning of the main loop. This large while() loop uses the WaitForChange() method to wait for Clipboard changes, but this time the method isn't passed a timeout value. Therefore, the script could wait forever. However, a change will most likely be detected. When a change occurs, the main loop processes it.

The code at callout B performs two important tasks. First, the code calls the IsText() method to see whether the Clipboard contains text data. If this method returns TRUE (1), the code calls the Get() method to retrieve that data and assigns it to the $Data variable. Second, the code uses regular expressions to look for URLs, email addresses, and paths in the text that the $Data variable contains. If the code finds any of these elements, it stores them under respective keys in the %List hash.

Using a hash is a neat trick to quickly weed out duplicates. For example, if the Clipboard contains two identical URLs, they'll both be stored in a hash using the same key. This will cause the second entry to overwrite the first one, which essentially removes the duplicate entry.

The code at callout C iterates through each of the entries in the %List hash and groups them by category (i.e., URL, email addresses, and file paths). Each entry in each category is appended to the $Clipboard-Buffer variable.

It's worth noting that the script treats carriage returns (\n) as if they don't imply linefeeds (\r\n). Windows' file I/O routines automatically convert between \n and \r\n as appropriate when retrieving data from or storing data to text files. However, Windows doesn't perform such conversions when retrieving data from or storing data to the Clipboard. Therefore, the code at callout D and callout G converts all occurrences of \n to \r\n. This treatment is done solely for the sake of testing with Notepad. If Perl stores carriage returns on the Clipboard and the data is later pasted into Notepad, all the text will display on one long line. This is a limitation of Notepad and not Win32::Clipboard.

The data in the $ClipboardBuffer variable is displayed and copied back to the Clipboard. As callout D shows, the script uses the print() method to display the sorted data and the Set() method to copy the data back to the Clipboard. Anything that was previously on the Clipboard is overwritten.

Next, the script checks to see whether the Clipboard contains graphic data. The code at callout E first calls the IsBitmap() method to determine whether the Clipboard contains graphic data. If so, the code saves the graphic to a file with a unique filename. Because the graphic data is binary and must not be interpreted as text, the created file is switched from text mode to binary mode. The code then uses the Get() method to retrieve the graphic from the Clipboard and uses the print() method to write it to the file. The file's path is copied back to the Clipboard. Thus, you can paste the path from the Clipboard to an application, such as Windows Explorer.

The final part of this script processes any list of files and directories on the Clipboard. This occurs when the user selects objects on the desktop or in Windows Explorer, then selects the Copy option on the Edit menu. As callout F shows, the script calls the IsFiles() method. If paths are found on the Clipboard, the script checks each path to see whether it's a file or directory path. All file and directory paths are stored in the %List hash to remove any duplicates.

Just like the code at Callout C, the code at callout G sorts the contents of the %List hash and stores the sorted data in the $ClipboardBuffer variable. If any paths were found, the script displays and copies them to the Clipboard.

A Great Tool
Using the Clipboard isn't for everyone. For some people, writing Perl scripts that interact with the Clipboard might seem like a useless endeavor. But for me, reading from and writing to the Clipboard is part of my everyday work life. The Clipboard can be a great tool that you can leverage with minimal effort.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.