Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build a Shared Clipboard Utility in Python

A shared clipboard lets you copy and paste data seamlessly across machines—it's the perfect productivity tool if you work with multiple machines in parallel.


advertisement
his article presents the Python shared clipboard—a little utility that allows different machines connected over a LAN to share the contents of their clipboards. While building it, you might learn something about cross-platform development in Python: how to access the clipboard on different platforms through various APIs, and some sound software design. Here's a quick scenario that illustrates the basics. Suppose you're running both a Mac and a Windows machine. You select and copy the text "The shared clipboard is awesome!" from within a text editor on the Mac (see Figure 1). Having done that, you can now paste that exact same text into an application on a separate Windows machine (see Figure 2). The shared clipboard works in the background, making clipboard interactions between machines as simple as copying and pasting between applications on the same machine.

 
Figure 1. Shared Clipboard on the Mac: After copying the text "The shared clipboard is awesome!" into the Mac's clipboard, the shared clipboard automatically wrote it to the shared file.
 
Figure 2. Shared Clipboard on Windows: On the Windows machine, the SharedClipboard utility monitored the shared file and copied the text "The shared clipboard is awesome!" (originally copied on the Mac) into the Windows clipboard. Here, it's been pasted into a text editor.

A shared clipboard is particularly useful for people that work on multiple machines at the same time. For example, I typically work with between two and three physical machines running Windows XP, Vista, Linux, and Mac OS X Leopard; some of those OSs run in virtual machines (VMs). I often look up something on the web or in a file and need to paste it in some application on another machine. Before I developed the shared clipboard I had several inconvenient options: I could switch to the target machine and look up the content again; I could copy the content on one machine and send an IM or email to the other machine, open the email or IM client, copy the content, and paste it in its final destination; or I could create a file containing the content on a shared network drive, save it, and then open the file from the other machine. All these options take extra time, but using the shared clipboard to copy and paste across machines takes essentially no more time than a copy and paste operation on a single machine.

It important to understand that SharedClipboard is designed to help a single user copy and paste between multiple machines, not to share information between users. Different users will want different content in their clipboards. Sharing the clipboard will cause them to overwrite each other's clipboard contents, perhaps causing people to lose data before they had a chance to paste it. How Does It Work?
The shared clipboard is based on a very simple idea: you keep a little program called SharedClipboard running on each physical machine from which you wish to share a clipboard. This program monitors the machine's clipboard and a special shared file. For example, suppose you had three machines: A, B, and C. Whenever you copy some text into the clipboard of machine A, the SharedClipboard instance running on that machine overwrites the contents of the shared file. Immediately, the SharedClipboard instances on the other two machines (B and C) that monitor the shared file will detect that its content has changed—and will copy the file's contents into their own local clipboards, so it's ready to be pasted.



To prevent infinite loops that repeatedly write the same content to the clipboard and the file, SharedClipboard checks whether the clipboard contents are identical to the contents of the shared file, before modifying it; if they are, it doesn't alter the file. Race conditions are ignored. If you were to simultaneously copy some data into the clipboard of more than one machine (you need to be quick!), one of the machines will grab the shared file first, modify it, and then the other machine will open and modify the file. It may not be clear exactly which machine's clipboard content will end up in the shared file and available to all machines, but such contention won't break the SharedClipboard itself. In practice, such contention is all but impossible for a single user. SharedClipboard and Virtual Machines
The shared clipboard works through a single instance when you're running virtual machines, because VMs typically let you share the clipboard of the host OS. In other words, even if you're running multiple VMs with different operating systems, they can all share the physical machine's clipboard. Therefore, running SharedClipboard on any one of the virtual machines or on the host OS lets all benefit from it.

SharedClipboard in a Multiuser Environment
As described so far, the shared clipboard is designed to be used by a single user. But what if you have multiple users on a single LAN, each controlling several machines, and they all wish (naturally) to use the shared clipboard? No problem. The shared file that each instance of SharedClipboard uses is a command-line argument to the program. Each user should launch a SharedClipboard instance on each of their physical machines, passing the same shared filename to each instance. They won't be able to share data with each other through their SharedClipboard instances, but as long as different users use different shared files (typically, that file would reside on one of their machines or a network share) they will not overwrite each other's clipboards. The size of the shared file for each user is always the size of the last copied content, so it doesn't grow over time and won't clog up the drive.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap