Joshua Tompkins
Joshua Tompkins

Reputation: 1452

How can I copy text to the clipboard from a Mono/C# console application on OS X?

I'm writing a console app in C#, using Mono on OS X.

On Windows, here is how I copy text to the clipboard:

Clipboard.SetText("text");

...from a method marked up with the STAThread attribute:

[STAThread] // for OLE
public static void Main (string[] args)
{
    Clipboard.SetText("text");
}

Clipboard is defined in System.Windows.Forms. That code compiles and runs both under the official Microsoft .NET runtime on Windows and on the the Mono runtime on OS X.

On Windows, the text gets copied, so it's not an issue of forgetting a reference or a using statement.

Unfortunately, when I run the code under Mono/OS X, the text isn't actually copied to the Clipboard. Something appears quickly on my Dock and then disappears just as quickly, but either way, the text I'm trying to copy doesn't end up on the clipboard.

So: how do I copy text do the clipboard on OS X (and Linux?) using Mono?

If you're interested in seeing the entire project, the code is here: https://github.com/leeohsheeus/boom-sharp.

Upvotes: 4

Views: 5580

Answers (2)

Random Citizen
Random Citizen

Reputation: 1376

If you want to program for multiple platforms using Mono, Gtk.Clipboard might be interesting for you.

Minimum Usage-Example:

Gtk.Clipboard clipboard = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));

clipboard.Text = "Hello World";

Also this Stackoverflow-Question might be interesting.

Upvotes: 8

jstedfast
jstedfast

Reputation: 38618

Console apps themselves cannot copy text to a clipboard. The user will have to use Copy&Paste from the terminal application.

Upvotes: 0

Related Questions