Click here to Skip to main content
12,702,995 members (28,815 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked

Transform Clipboard Contents Between Copy and Paste

, 26 Aug 2016 CPOL
Rate this:
Please Sign up or sign in to vote.
Alter your clipboard contents before pasting to automate tasks.


This windows desktop application, while running, monitors your clipboard, and allows you to alter the contents before you paste.  This can be quite powerful, for example, copying a range of data in Excel (unformatted) and then transforming it into a pretty table to be pasted into an email.  Or, splitting a string of numbers and getting a distinct, sorted subset for analysis on paste.  One of my favorites is copying the top row of a SQL result set from MS SQL Server Management Studio, and transforming it into a column list to make selecting and re-arranging the columns easier.  The most powerful one is taking an Excel sheet and transforming it into a SQL temp table script.  All with the push of a button.


Repetitive tasks drive me nuts.  After having to alter text to make it fit some need over and over and over again, I finally realized what I need to do is be able to programmatically alter the text in my clipboard without having to go to some intermediate tool. 

For example, without this tool, to send results from SQL Server Management Studio to my co-worker, I would copy the results with headers, launch MS Excel, paste into Excel, format in Excel, copy from Excel, paste into the email to be sent.  That's a lot of busy work just to display a table of data.  Given I was doing that many times a day, I needed a solution!  This is that solution.

Conversely, I would regularly get Excel workbooks from users asking me to import/export/explain some piece of data.  To get that into our production database on the other side of the firewall was a pain.  The Excel to SQL transformation takes the Excel input and makes it simple to paste that Excel data into a SQL #Temp table, which I can then use to import or analyze the issue. 

After using this tool for several years, and sharing with my co-workers, it seemed a proper time to share with the rest of the world.  Enjoy.

Using the Code

This posting is more about making the tool available for others to enjoy.  To use, launch the executable, copy something into your clipboard, select the transformation(s), apply and paste.  That's it.  For common transformations that you use every day, you can create a shortcut macro on your desktop (or quick launch bar) to apply the transformation with a single click.  See the About section of the application for more details.

Exploring the Code

This application is a standard WPF desktop application that, for the most part, is unremarkable.  However, if you are interested in extending the code to meet your own needs, it would be helpful to understand some of the moving parts.


The general design is a ViewModel bound WPF application.  On startup, we use reflection to find all classes that implement TextTransformerBase in the currently loaded assembly, we then add those classes to the list of transformers displayed in the application.  On transform, we capture the user's clipboard, and apply the transformations (calling Transform(ref text) on each selected transformer), then write the result back to the clipboard, finally closing the application.

As with most WPF application, we start in App.xaml.cs, in Application_Startup we parse the command line to see if we are running in UI mode (display a list of possible transformations) or command line mode (a canned list of transformers was passed in, so just transform and exit -- no UI).  In either case, the core initialization happens in TextTransformerBase.AllTransformers, where we parse assemblies looking for transformers, returning them as a collection.  They are then wrapped in ListBoxTransformerItem, which keeps track of selection sort order and other UI features, finally being added to an ObservableCollection that is rendered into the UI.

On selection of any item, we DoPreview() to display what the output of the transformation will look like.  Ultimately, if the user clicks apply, we basically just do VM.ClipboardText = VM.Transform(); and close the application.

All that said, there is quite a bit of code to make the systray bubble work.

Problem 1: Integrating into Win32 components! This code was entirely grafted from the internet (clipboard-event-c-sharp) thanks to dbkk!

Problem 2: How to display the bubble for the right amount of time after the transformation, but before the application closes (removing the bubble and systray icon)?  Most of the logic around this is in MainWindowVM.DisplayTransformComplete.  If we pass in delegate function (onDoneHandler), we wire it in to a 3 second timer to be called on done.  In most cases, this basically calls VM.Quit() to shut the app down.  In any case, no matter what apply mode we are in, as soon as the "apply" starts, we hide the main window to make it "look" like the application has shut down while we display he popup bubble.

The remaining code that is noteworthy is really the transformer code.  My hope in posting this is that developers take this code and create their own transformers!

Implementing Custom Transformers

For the most power, it may be easiest to write your own transformers depending on your needs.  I did try to write the code so you can just include another assembly (.dll) below the application root and if it contained a transformer, you could use it.  This proved somewhat problematic when it came to security, and that code is currently commented out (TextTransformerBase.AllTransformers#if false section).

There are two basic options to create your own transformers.

Option 1 - XML Transformers

On startup, MaskedTransformer.GetAllMaskedTransformers parses the application directory (and sub directories) for *.xml files that contain XML-based (serialized) transformation.  See Sample.xml for full details, but the gist is: if your tranformation just needs to do a find/replace, splitting, or calling other transformers in conjunction with either, then you can just create an XML Transformer.  The biggest advantage being no need to re-compile code, just create the XML (text) file and include it below the application root.

Option 2 - Custom .NET Transformers

To create your own custom transformer, create a class that implements the abstract base class TextTransformerBase.  The semantics are petty simple (I hope), where the constructor defines the basics (name, sort order, description), and then override Transform(ref string str) with your custom code.


public class ToUpper : TextTransformerBase
        public ToUpper()
            : base(
                GROUP_SORT_ORDER.STRING_CASE, // Group in which to include this transformer
                "To Upper", // Name to display
                "Make all characters UPPER case." // Tool tip and Help file text
            ) { }

        override public void Transform(ref string str)
            str = str.ToUpper();

There are checks in place to ensure str is never null, so you should just be able to write whatever transformations you desire.

Points of Interest

Disable Preview: While working on this post, I had cause to transform a very large SQL result set (> 100 MB).  It was SLOOOOW!  After a little debugging I realized that the issue was solely with the rendering to the UI.  As such, I have added a "Disable Preview" option that will trigger if it thinks the amount of data in your clipboard is too big.

ClipboardFormat: There are some transformers that return HTML.  HTML in the clipboard gets ... tricky.  You have to provide these index positions of where the pasted HTML lives, which is somewhat cumbersome to compute.  If you need to return an HTML result, I would recommend you use HTMLClipboardHelper.HTMLToClipboardFormattedText to provide the result.

Desktop Shortcuts: I have used this app for years, and I always just create macro shortcuts to it by hand, but that's pretty lame.  So with the help of the internet (create-shortcut-on-desktop-c-sharp) and thanks to Simon Mourier, I was able to wire in a simple button to do so.  See ShortcutHelper.CreateShortcut for the "fun" code.

Event Log: Meaningful, but non-crashing, errors are logged to the Windows Event Log.  This can be helpful when debugging XML Transformer issues when you don't have a debugger attached.

Help: I worked hard to have the help be included in-line in a seamless way, which is why TextTransformerBase has a HelpText property.  The styling and other details are defined in Help.html, which is what is ultimately rendered out to the help window.


26 Aug 2016 - (My Mother's Birthday!) - Initial Public posting.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Brad Joss
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Carsten V2.030-Aug-16 0:15
memberCarsten V2.030-Aug-16 0:15 
PraiseTransform Clipboard Contents Between Copy and Paste Pin
ArtemDaemon27-Aug-16 22:15
memberArtemDaemon27-Aug-16 22:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 26 Aug 2016
Article Copyright 2016 by Brad Joss
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid