Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 281
Joshua Tompkins Avatar asked Jan 19 '23 18:01

Joshua Tompkins


1 Answers

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.

like image 56
Random Citizen Avatar answered Jan 21 '23 09:01

Random Citizen