Class: Ace::Support::MacClipboard::Reader
- Inherits:
-
Object
- Object
- Ace::Support::MacClipboard::Reader
- Extended by:
- FFI::Library
- Defined in:
- lib/ace/support/mac_clipboard/reader.rb
Class Method Summary collapse
-
.available_types(pasteboard) ⇒ Object
Get all available UTI types on the pasteboard.
-
.create_nsstring(str) ⇒ Object
Helper to create NSString from Ruby string.
-
.get_general_pasteboard ⇒ Object
Get the general (system) pasteboard.
-
.objc_send(obj, selector, *args) ⇒ Object
Helper to send Objective-C messages.
-
.read ⇒ Object
Read clipboard content.
-
.read_file_urls(pasteboard) ⇒ Object
Read file URLs from pasteboard.
-
.read_filenames_pboard_type(pasteboard) ⇒ Object
Read file paths from NSFilenamesPboardType (used by Finder).
-
.read_public_file_url(pasteboard) ⇒ Object
Read file URL from public.file-url.
-
.read_string(pasteboard, uti) ⇒ Object
Read string content for text types.
-
.read_type(pasteboard, uti) ⇒ Object
Read data for a specific UTI type.
Class Method Details
.available_types(pasteboard) ⇒ Object
Get all available UTI types on the pasteboard
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 55 def self.available_types(pasteboard) types_array = objc_send(pasteboard, "types") return [] unless types_array && !types_array.null? count = objc_msgSend_uint(types_array, sel_registerName("count")) return [] if count.zero? types = [] count.times do |i| type_obj = objc_msgSend_uint64(types_array, sel_registerName("objectAtIndex:"), i) next unless type_obj && !type_obj.null? utf8_selector = sel_registerName("UTF8String") type_cstr = objc_msgSend(type_obj, utf8_selector) next if type_cstr.null? types << type_cstr.read_string end types rescue => e warn "Error in available_types: #{e.}" warn e.backtrace.first(5) [] end |
.create_nsstring(str) ⇒ Object
Helper to create NSString from Ruby string
203 204 205 206 207 208 209 210 211 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 203 def self.create_nsstring(str) ns_string_class = objc_getClass("NSString") return nil unless ns_string_class utf8_selector = sel_registerName("stringWithUTF8String:") objc_msgSend_id(ns_string_class, utf8_selector, FFI::MemoryPointer.from_string(str)) rescue nil end |
.get_general_pasteboard ⇒ Object
Get the general (system) pasteboard
49 50 51 52 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 49 def self.get_general_pasteboard ns_pasteboard_class = objc_getClass("NSPasteboard") objc_send(ns_pasteboard_class, "generalPasteboard") end |
.objc_send(obj, selector, *args) ⇒ Object
Helper to send Objective-C messages
24 25 26 27 28 29 30 31 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 24 def self.objc_send(obj, selector, *args) sel = sel_registerName(selector.to_s) if args.empty? objc_msgSend(obj, sel) else objc_msgSend_id(obj, sel, *args) end end |
.read ⇒ Object
Read clipboard content
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 34 def self.read return {success: false, error: "Not on macOS"} unless RUBY_PLATFORM.match?(/darwin/) pasteboard = get_general_pasteboard return {success: false, error: "Could not access pasteboard"} unless pasteboard types = available_types(pasteboard) return {success: true, types: [], text: nil, attachments: []} if types.empty? {success: true, types: types, raw_pasteboard: pasteboard} rescue => e {success: false, error: e.} end |
.read_file_urls(pasteboard) ⇒ Object
Read file URLs from pasteboard
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 128 def self.read_file_urls(pasteboard) # Try NSFilenamesPboardType first (Finder uses this for copied files) file_paths = read_filenames_pboard_type(pasteboard) return file_paths if file_paths.any? # Fallback to public.file-url file_paths = read_public_file_url(pasteboard) return file_paths if file_paths.any? [] rescue => e warn "Error reading file URLs: #{e.}" [] end |
.read_filenames_pboard_type(pasteboard) ⇒ Object
Read file paths from NSFilenamesPboardType (used by Finder)
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 144 def self.read_filenames_pboard_type(pasteboard) uti_str = create_nsstring("NSFilenamesPboardType") return [] unless uti_str # Get property list (NSArray of file path strings) prop_list_sel = sel_registerName("propertyListForType:") array_obj = objc_msgSend_id(pasteboard, prop_list_sel, uti_str) return [] unless array_obj && !array_obj.null? # Get count of files count_sel = sel_registerName("count") count = objc_msgSend_uint(array_obj, count_sel) return [] if count.zero? # Extract file paths file_paths = [] count.times do |i| obj_at_index_sel = sel_registerName("objectAtIndex:") path_obj = objc_msgSend_uint64(array_obj, obj_at_index_sel, i) next unless path_obj && !path_obj.null? utf8_sel = sel_registerName("UTF8String") cstr = objc_msgSend(path_obj, utf8_sel) next if cstr.null? path = cstr.read_string file_paths << path if File.exist?(path) end file_paths rescue => e warn "Error reading NSFilenamesPboardType: #{e.}" [] end |
.read_public_file_url(pasteboard) ⇒ Object
Read file URL from public.file-url
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 180 def self.read_public_file_url(pasteboard) data = read_type(pasteboard, "public.file-url") return [] unless data # Parse URL from data url_str = data.force_encoding("UTF-8").strip # Remove "file://" prefix if present url_str = url_str.sub(%r{^file://}, "") # URL decode url_str = begin URI.decode_www_form_component(url_str) rescue url_str end File.exist?(url_str) ? [url_str] : [] rescue [] end |
.read_string(pasteboard, uti) ⇒ Object
Read string content for text types
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 107 def self.read_string(pasteboard, uti) # Try to get as string first objc_getClass("NSString") uti_str = create_nsstring(uti) return nil unless uti_str string_obj = objc_msgSend_id(pasteboard, sel_registerName("stringForType:"), uti_str) if string_obj && !string_obj.null? utf8_selector = sel_registerName("UTF8String") cstr = objc_msgSend(string_obj, utf8_selector) return cstr.read_string unless cstr.null? end # Fallback to reading as data data = read_type(pasteboard, uti) data&.force_encoding("UTF-8") rescue nil end |
.read_type(pasteboard, uti) ⇒ Object
Read data for a specific UTI type
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ace/support/mac_clipboard/reader.rb', line 82 def self.read_type(pasteboard, uti) # Create NSString for the UTI objc_getClass("NSString") uti_str = create_nsstring(uti) return nil unless uti_str # Get data from pasteboard data = objc_msgSend_id(pasteboard, sel_registerName("dataForType:"), uti_str) return nil unless data && !data.null? # Get byte length length = objc_msgSend_uint(data, sel_registerName("length")) return nil if length.zero? # Get bytes pointer bytes = objc_msgSend(data, sel_registerName("bytes")) return nil if bytes.null? # Read binary data bytes.read_bytes(length) rescue nil end |