好像您正在使用 Windows,这适用于 Windows 7 下的 Ruby 1.9.3。
有关原始答案,请参阅使用 Fiddle 将 Ruby 复制到剪贴板
require 'open3'
module Clipboard; end
module Clipboard::Windows
extend self
CF_TEXT = 1
CF_UNICODETEXT = 13
GMEM_MOVEABLE = 2
# get ffi function handlers
begin
require 'ffi'
rescue LoadError
raise LoadError, 'Could not load the required ffi gem, install it with: gem install ffi'
end
module User32
extend FFI::Library
ffi_lib "user32"
ffi_convention :stdcall
attach_function :open, :OpenClipboard, [ :long ], :long
attach_function :close, :CloseClipboard, [ ], :long
attach_function :empty, :EmptyClipboard, [ ], :long
attach_function :get, :GetClipboardData, [ :long ], :long
attach_function :set, :SetClipboardData, [ :long, :long ], :long
end
module Kernel32
extend FFI::Library
ffi_lib 'kernel32'
ffi_convention :stdcall
attach_function :lock, :GlobalLock, [ :long ], :pointer
attach_function :unlock, :GlobalUnlock, [ :long ], :long
attach_function :size, :GlobalSize, [ :long ], :long
attach_function :alloc, :GlobalAlloc, [ :long, :long ], :long
end
# see http://www.codeproject.com/KB/clipboard/archerclipboard1.aspx
def paste(_ = nil)
ret = ""
if 0 != User32.open( 0 )
hclip = User32.get( CF_UNICODETEXT )
if hclip && 0 != hclip
pointer_to_data = Kernel32.lock( hclip )
data = ""
# Windows Unicode is ended by to null bytes, so get the whole string
size = Kernel32.size( hclip )
data << pointer_to_data.get_bytes( 0, size - 2 )
if RUBY_VERSION >= '1.9'
ret = data.force_encoding("UTF-16LE").encode(Encoding.default_external) # TODO catch bad encodings
else # 1.8: fallback to simple CP850 encoding
require 'iconv'
utf8 = Iconv.iconv( "UTF-8", "UTF-16LE", data)[0]
ret = Iconv.iconv( "CP850", "UTF-8", utf8)[0]
end
if data && 0 != data
Kernel32.unlock( hclip )
end
end
User32.close( )
end
ret || ""
end
def clear
if 0 != User32.open( 0 )
User32.empty( )
User32.close( )
end
paste
end
def copy(data_to_copy)
if ( RUBY_VERSION >= '1.9' ) && 0 != User32.open( 0 )
User32.empty( )
data = data_to_copy.encode("UTF-16LE") # TODO catch bad encodings
data << 0
handler = Kernel32.alloc( GMEM_MOVEABLE, data.bytesize )
pointer_to_data = Kernel32.lock( handler )
pointer_to_data.put_bytes( 0, data, 0, data.bytesize )
Kernel32.unlock( handler )
User32.set( CF_UNICODETEXT, handler )
User32.close( )
else # don't touch anything
Open3.popen3( 'clip' ){ |input,_,_| input << data_to_copy } # depends on clip (available by default since Vista)
end
paste
end
end
Clipboard::Windows.copy("test")
puts Clipboard::Windows.paste
在我的收藏中,我有另一个曾经在 Windows 7 32 位中工作的脚本,如果你对第一个有问题并且只使用 32 位,试试这个
#!/usr/bin/env ruby -w
# win32 only
require 'singleton'
require 'thread'
require 'Win32API'
class Clipboard
include Singleton
CF_TEXT = 1
def initialize
@@mutex = Mutex.new
@@open = Win32API.new("user32","OpenClipboard",['L'],'L')
@@close = Win32API.new("user32","CloseClipboard",[],'L')
@@empty = Win32API.new("user32","EmptyClipboard",[],'L')
@@set = Win32API.new("user32","SetClipboardData",['L','P'],'L')
@@get = Win32API.new("user32", "GetClipboardData", ['L'], 'L')
@@lock = Win32API.new("kernel32", "GlobalLock", ['L'], 'P')
@@unlock = Win32API.new("kernel32", "GlobalUnlock", ['L'], 'L')
end
def copy
@@mutex.synchronize do
@@open.Call(0)
str = @@lock.Call(@@get.Call(CF_TEXT))
@@unlock.Call(@@get.Call(CF_TEXT))
@@close.Call
return str
end
end
def paste(str)
@@mutex.synchronize do
@@open.Call(0)
@@empty.Call
@@set.Call(CF_TEXT, str)
@@close.Call
@@lock = Win32API.new("kernel32", "GlobalLock", ['L'], 'P')
@@unlock = Win32API.new("kernel32", "GlobalUnlock", ['L'], 'L')
return nil
end
end
end
clip = Clipboard.instance
puts clip.copy
puts str
clip.paste("foo")
puts clip.copy
如果您不介意安装 gem,这里有一个更简单的解决方案,适用于 windows7 64 位,Ruby 1.9.3。
#gem install clipboard
require 'clipboard'
Clipboard.copy("This is a sentence that has been copied to your clipboard")
puts Clipboard.paste