From: Brian M. <ma...@ex...> - 2006-01-31 04:54:38
|
On Jan 18, 2006, at 6:18 PM, Brian Marick wrote: > I want to put arbitrary ASCII strings onto the clipboard (including > NULs, control chars, etc.). I could do this: Here's the code. Thanks to Mark Hubbart for getting me started. (Note: I don't include OSX because I haven't explained that yet to the audience for this code.) require 'osx/cocoa' require 'test-strings/copy-util' MAC_OS_ROMAN_ENCODING=30 UTF8_ENCODING=4 def copy(string) copy_with_encoding(string, MAC_OS_ROMAN_ENCODING) true end #Example: unicopy %w{ 03b4 03d4 03a6 } def unicopy(hex_string_array) assert_names_of_acceptable_length(hex_string_array) copy_with_encoding(utf8(hex_string_array), UTF8_ENCODING) true end # Utilities def utf8(hex_string_array) number_array = hex_string_array.collect do | hex_name | hex_name.to_i(16) end number_array.pack("U*") end def copy_with_encoding(string, encoding) data = OSX::NSData.dataWithRubyString(string) ns_string = OSX::NSString.alloc.initWithData(data, :encoding, encoding) pb = OSX::NSPasteboard.generalPasteboard pb.declareTypes(["NSStringPboardType"], :owner, nil) pb.setString(ns_string, :forType, "NSStringPboardType") end ----- Brian Marick, independent consultant Mostly on agile methods with a testing slant www.exampler.com, www.testing.com/cgi-bin/blog Book in progress: www.exampler.com/book |