Daniel Bogdan
Daniel Bogdan

Reputation: 765

Mono Clipboard fix

I'm developing an app in Visual Studio which runs on both Windows and MAC (on Mono framework 3.6.0). There are some issues on MAC which can't seem to get solved:

  1. The copy/paste functionality doesn't work. I know it has been asked:

Clipboard.GetText() always returns empty string in Mono on Mac

NSPasteboard and MonoMac

but I can't use NSPasteboard in Winforms.

  1. The app GUI is pretty sluggish on MAC. I have a TabControl and when I switch tabs sometimes the controls from the other tab remain and blend through the other tab controls. Pretty bad for a deployable application. Similar bugs reported:

http://lists.ximian.com/pipermail/mono-bugs/2010-December/107562.html

Is there any workaround for these issues ?

thanks

Upvotes: 1

Views: 1757

Answers (1)

Daniel Bogdan
Daniel Bogdan

Reputation: 765

Just in case someone else might need it, the solution was to implement copy/paste with pbcopy/pbpaste. Here is a helper class which can be used to copy/paste on OSX:

public class MacClipboard
{
    /// <summary>
    /// Copy on MAC OS X using pbcopy commandline
    /// </summary>
    /// <param name="textToCopy"></param>
    public static void Copy(string textToCopy)
    {
        try
        {
            using (var p = new Process())
            {

                p.StartInfo = new ProcessStartInfo("pbcopy", "-pboard general -Prefer txt");
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = false;
                p.StartInfo.RedirectStandardInput = true;
                p.Start();
                p.StandardInput.Write(textToCopy);
                p.StandardInput.Close();
                p.WaitForExit();
            }
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
        }


    }

    /// <summary>
    /// Paste on MAC OS X using pbpaste commandline
    /// </summary>
    /// <returns></returns>
    public static string Paste()
    {
        try
        {
            string pasteText;
            using (var p = new Process())
            {

                p.StartInfo = new ProcessStartInfo("pbpaste", "-pboard general");
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.Start();
                pasteText = p.StandardOutput.ReadToEnd();
                p.WaitForExit();
            }

            return pasteText;
        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
            return null;
        }

    }
}

There is no need for extra installations on MAC as with xsel.

Upvotes: 3

Related Questions