Huddled Masses
You can do more than breathe for free...
Browse: Home / Clip.exe and the missing Paste.exe

Clip.exe and the missing Paste.exe

By Joel 'Jaykul' Bennett on 14-Nov-2008

One of the guys on #PowerShell in IRC was asking about how to get the contents of the clipboard in PowerShell, and after we had tried several different scripts, we realized that in Version 1, because PowerShell runs in an MTA thread, you cannot access the clipboard without creating a new thread (in STA mode). After I got done ranting, and pointing out that everyone needs to use PowerShell v2 (yeah, it’s still in CTP, so I was mostly kidding) in -STA mode … I whipped together the C# one liner and stuck it into a class with instructions for compiling, and figured I might as well share.

The code is below. There are two files: Clip.cs and Paste.cs … you probably only need Paste.cs, because Clip.exe is already included in Windows after Windows XP, but for the sake of completeness (and because it was just a few lines), I wrote them both.

To use them, you must compile them using Csc.exe, which is included as part of the .Net Framework. If it’s not already on your path, it’s in the folder for your most recent .Net Framework version (eg: C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe or C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe). All you have to do is run: csc Paste.cs and csc Clip.cs and you should end up with a Paste.exe and Clip.exe which you can use to copy and paste from the command-line…

Paste.cs


using System;
using System.Windows.Forms;
using System.Threading;

namespace Huddled {
   public class Paste {
      [STAThread]
      static void Main( string[] args )
      {
         foreach(string line in Clipboard.GetText().Split(
                                 new string[]{"\r\n","\n"},
                                 StringSplitOptions.None ))
            Console.WriteLine( line );
      }
   }
}

Clip.cs


using System;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Huddled {
public class Clip {
   [STAThread]
   static void Main( string[] args )
   {
      string s;
      StringBuilder output = new StringBuilder( string.Join(" ", args) );
      while ((s = Console.ReadLine()) != null)
         output.AppendLine(s);

      Clipboard.SetText( output.ToString() );
   }
}
}

Similar Posts:

  • More Custom Attributes for PowerShell (Parameter Transformation)
  • The problem with calling legacy/native apps from PowerShell
  • Parenthesis in PowerShell
  • Better error messages for PowerShell ValidatePattern
  • How to: Invoke PowerShell and use the results from C#?

Posted in Huddled | Tagged C++, Clipboard, Command Line, CSharp, PowerShell, Threading, Utilities

« Previous Next »

Lijit Search

Tags

.Net .Net 2008 Scripting Games Automation Bugs Design Development Funny Gadgets GeoShell GUI Huddled Masses Internet licensing Microsoft Modules My Software News Personal PInvoke Pipeline Politics PoshCode PoshConsole PowerBoots PowerShell PowerShell Functions PowerTips Rants Recommender Repository Scripting ShowUI Software Solutions Textile Tips User Group UserInterface WalkThrough WebHosting Windows 7 WordPress WPF Xml

About Huddled Masses

This is web site is dedicated to the musings of Joel Bennett (aka Jaykul) about technology, software, software development, the web, and the world.

Any resemblance of the views expressed and the views of my employer, my terminal, or the view out my window are purely coincidental. The resemblance between them and my own views is non-deterministic. The question of the existence of views in the absence of anyone to hold them is left as an exercise for the reader.

P.S.: I occasionally link to things I think are great. When I do, I occasionally find a "referral code" so I can make a little cash. I promise that I don't link to anything just because of that cash (I wouldn't cross the street for the amount of cash those links bring in, never mind write a whole blog post) ... but I do not promise that things I link to will stay great as time passes, nor that you will agree with me about their greatness!

Archives

  • April 2012
  • February 2012
  • January 2012
  • October 2011
  • August 2011
  • July 2011
  • June 2011
  • March 2011
  • February 2011
  • January 2011

Copyright © 2012 Joel Bennett.

Powered by WordPress and Hybrid.