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.
You can copy a string with Clipboard. copy("string") and paste it with Clipboard. paste() .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With