Clipboard Munger
For a while, I’ve had a simple VBScript file which is able to read and write to the clipboard using a small ActiveX object I wrote in a DLL. The Clipboard Munger, as I called it, was just my swiss army knife of functions for transforming text a certain way. It is designed to easily evolve: as I discover a short, repetitive text munging task for which there was no easy-to-find tool, I would just add a section of code to do it, and add an entry into the list of things it can do.
Over time, the list of munging functions grew to quick a large list. One of its functions is in the download section as a separate script: VBScript beautifier.
I became aware of a scripting technique in dotNET, but VBScript was just faster–so I stuck with it. But alas, the ActiveX object for interfacing to the clipboard succumbed to the 64-bit world, leaving Vista users in the cold. So finally, I decided it was time to migrate the code to dotNET, even if it was just an exercise in learning how to use the scripting technique in dotNET. So here is the Clipboard Munger project.
The project consists of a solution with one WinForm project, which uses a tray icon when you’re not working on the scripts themselves. The scripts implement a simple interface definition named IClipboardMunger. The interface contains one method which takes a string as input (the clipboard content), and returns a string which becomes the new clipboard content.
There are several working sample scripts already written. Scripts can be written in C# or VB.Net. They can also reference assemblies in the GAC, and external assemblies.
This is a fully working version, which really only lacks syntax highlighting and Intellisense-like help in the script editor (which is currently only a text box). I may have a solution for that as well shortly.
There are two dotNET techniques demonstrated here.
- Runtime compilation and instantiation of a class implementing an interface.
- XML Serialization / Deserialization of an object, which allows the method, its tests and descriptions to be saved and read in a text file as XML.
The source code (built with VS2008) is available on the download page. You can also just download the MSI installer package to use the existing scripts. The project only needs dotNET Framework 2.0 installed. Visual Studio is not needed to build and compile scripts in this application.
There are several default scripts to get you started, including a shell which implements the interface and just returns the clipboard content back to itself. I’ve also included the VBScript beautifier as an example, to show how useful scripts can really get.
How to use it
Upon startup, the WinForm looks like this:
If you do not see a list of scripts in the left pane, click on the Change Scripts Folder and navigate to the folder containing the scripts (called, Scripts). You can store the files anywhere. Once a script is selected on the left, clicking Run Script will execute that procedure against the clipboard contents.
Clipboard contents shows the actual text clipboard contents, if any.
The Developer tab is where the script is written and/or maintained. The compile button actually instantiates a dotNET compiler, which is part of the dotNET framework, so this compiler will work on workstations without Visual Studio installed. Very nice!
The “GAC references” tab allows inclusion of any dotNET GAC Assembly that the script needs. Note: the code for this is unique to Framework 2.0: it will produce unpredictable results in other Framework versions. Click on the check box to move an assembly to the opposite panel. Only assemblies in the Include panel are included in the build.
Clicking on the checkbox next to an assembly will move it to the opposite panel.
The “other reference” tab allows inclusion of assemblies outside of the GAC. Use the add button to add a non-GAC assembly to the list. The assembly must have its checkbox marked to be included in the build.
The Test tab contains an area for static test text (saved with the script). Run Test will run the script against the text in the upper textbox–not the actual clipboard contents. So this is the testing sandbox.
Store/Update is used when a script is created or modified. This will actually store the script in its XML file container. The file name is created by prefixing the title with “cm_” and adding the extension “.xml”
The application will create a tray icon (swiss army knife), which can be used for launching the scripts when the form is minimized.
Right-clicking the icon will display options for immediately executing a script against the clipboard. Double-clicking the icon will restore the form.
This version warns about unsaved scripts before switching scripts.
Clipboard Munger is available on the download page.
Leave a Comment
You must be logged in to post a comment.