Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy to Clipboard in Ruby, HTML or C#

Tags:

c#

clipboard

ruby

How do you copy text to the clipboard in Ruby?

Sounds simple right? But I've been trying to do this for 5 days now, with no success.

I searched on internet, but all I got is how to do it in newer versions of Ruby (I'm using 1.8.7 and no I can't use a newer version).

So I tried making a HTML file to do it for me. After trying 4-5 different ways (from online guides), in 3 browsers, and even looking at Photobucket's source code to try figuring how it copies img codes, I gave up. Nothing worked for me.

So I made a C# .exe and made my Ruby program call it. Finally something is being sent to the clipboard. It's a step forward, but still, it's only the first word in a string. When I try copying two words, only two is copied.

my Ruby program looks like this:

system  ("CopyClip.exe #{text}")

and in C# (in CopyClip), it does:

Clipboard.set_text(args[0])

Basically, I don't care if you help me do it in Ruby, HTML, C#, or any other language, as long as it works.

like image 306
yasmin Avatar asked Oct 09 '13 19:10

yasmin


People also ask

How do you copy to clipboard in Ruby?

You can copy a string with Clipboard. copy("string") and paste it with Clipboard. paste() .


1 Answers

This answer works great for OSX:

def pbcopy(input)
 str = input.to_s
 IO.popen('pbcopy', 'w') { |f| f << str }
 str
end

def pbpaste
 `pbpaste`
end
like image 60
Roman B. Avatar answered Oct 05 '22 23:10

Roman B.