Last active
December 14, 2015 18:39
-
-
Save havenwood/5131003 to your computer and use it in GitHub Desktop.
Copy and Paste to OS X, BSD, and Linux clipboards with Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Clipboard | |
CopyPaste = Struct.new :copy, :paste | |
def initialize | |
cmds = [['pbcopy', 'pbpaste'], | |
['xclip', 'xclip -o'], | |
['xsel', 'xsel -o']] | |
cmds.map! do |cmd| | |
CopyPaste.new cmd.first, cmd.last | |
end | |
@clipboard = cmds.find do |cmd| | |
system "which #{cmd.copy} > /dev/null" | |
end | |
end | |
def copy this | |
IO.popen @clipboard.copy, 'w' do |io| | |
io.print this | |
end | |
end | |
def paste | |
`#{@clipboard.paste}` | |
end | |
end | |
clip = Clipboard.new | |
clip.copy 'hiya' | |
#=> nil | |
clip.paste | |
#=> "hiya" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have tried to implement this in Linux, but when I tried to call
xclip
using the backtick, the program hangs.Do you have any idea what's going on? What's the difference between backtick and system? Thanks.