Skip to content

Instantly share code, notes, and snippets.

@davidbalbert
Last active August 5, 2022 01:00
Show Gist options
  • Save davidbalbert/f4b963f54c65f1972cdf642a76b15760 to your computer and use it in GitHub Desktop.
Save davidbalbert/f4b963f54c65f1972cdf642a76b15760 to your computer and use it in GitHub Desktop.
NSPasteboard UTF-16 nonsense
#import <Cocoa/Cocoa.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
@implementation NSData (NSArrayConversion)
- (NSArray<NSNumber*>*)byteArray {
NSMutableArray<NSNumber*> *a = [NSMutableArray arrayWithCapacity:[self length]];
const unsigned char *bytes = [self bytes];
for (int i = 0; i < [self length]; i++) {
a[i] = [[NSNumber alloc] initWithUnsignedChar:bytes[i]];
}
return a;
}
@end
@implementation NSArray (SwiftDescription)
- (NSString *)swiftDescription {
NSMutableString *s = [[NSMutableString alloc] init];
[s appendString:@"["];
[s appendString:[self componentsJoinedByString:@", "]];
[s appendString:@"]"];
return s;
}
@end
@implementation NSString (SwiftDescription)
- (NSString *)swiftDescription {
NSMutableString *s = [[NSMutableString alloc] init];
[s appendString:@"\""];
[s appendString: self];
[s appendString:@"\""];
[s replaceOccurrencesOfString:@"\0" withString:@"\\0" options:0 range:NSMakeRange(0, [s length])];
[s applyTransform:@"[:Cc:] Any-Hex/Perl" reverse:false range:NSMakeRange(0, [s length]) updatedRange:nil];
[s replaceOccurrencesOfString:@"\\x{" withString:@"\\u{" options:0 range:NSMakeRange(0, [s length])];
NSRegularExpression *singleDigitCodepoint = [[NSRegularExpression alloc] initWithPattern:@"\\\\u\\{([a-zA-Z0-9])\\}" options:0 error:nil];
[singleDigitCodepoint replaceMatchesInString:s options:0 range:NSMakeRange(0, [s length]) withTemplate:@"\\\\u{0$1}"];
return s;
}
@end
void
print(NSString *format, ...) {
va_list args;
va_start(args, format);
NSString *out = [[NSString alloc] initWithFormat:format arguments:args];
fprintf(stderr, "%s\n", [out UTF8String]);
va_end(args);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
[[[NSAppleScript alloc] initWithSource:@"set the clipboard to \"ABC\""] executeAndReturnError:nil];
NSPasteboard *pasteboard = NSPasteboard.generalPasteboard;
// [pasteboard clearContents];
// NSData *data = [@"ABC" dataUsingEncoding:NSUTF16LittleEndianStringEncoding];
// NSString *utf16 = [UTTypeUTF16PlainText identifier];
// assert([utf16 isEqualTo:@"public.utf16-plain-text"]);
// [pasteboard setData:data forType:utf16];
for (NSPasteboardType type in pasteboard.types) {
// for (NSPasteboardType type in pasteboard.pasteboardItems.firstObject.types) {
NSData *data = [pasteboard dataForType:type];
NSString *s = [pasteboard stringForType:type];
print(@"%@ %@ %@", type, [[data byteArray] swiftDescription], [s swiftDescription]);
}
}
return 0;
}
import Cocoa
import UniformTypeIdentifiers
NSAppleScript(source: "set the clipboard to \"ABC\"")?.executeAndReturnError(nil)
let pasteboard = NSPasteboard.general
// pasteboard.clearContents()
// let data = "ABC".data(using: .utf16LittleEndian)!
// let utf16 = NSPasteboard.PasteboardType(UTType.utf16PlainText.identifier)
// assert(utf16.rawValue == "public.utf16-plain-text")
// pasteboard.setData(data, forType: utf16)
for type in pasteboard.types ?? [] {
// for type in pasteboard.pasteboardItems?.first?.types ?? [] {
let d: String
if let data = pasteboard.data(forType: type) {
d = Array(data).debugDescription
} else {
d = "(null)"
}
let s = pasteboard.string(forType: type)?.debugDescription ?? "(null)"
print(type.rawValue, d, s)
}
public.utf16-plain-text [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
public.utf8-plain-text [65, 66, 67] "ABC"
com.apple.traditional-mac-plain-text [65, 66, 67] "ABC"
dyn.ah62d4rv4gk81g7d3ru [1, 0, 0, 0, 0, 0, 16, 0, 14, 0, 3, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0] "\u{01}\0\0\0\0\0\u{10}\0\u{0E}\0\u{03}\0\0\0\u{0C}\0\0\0\0\0\0\0"
public.utf16-plain-text [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
CorePasteboardFlavorType 0x75747874 [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
public.utf8-plain-text [65, 66, 67] "ABC"
NSStringPboardType [65, 66, 67] "ABC"
com.apple.traditional-mac-plain-text [65, 66, 67] "ABC"
CorePasteboardFlavorType 0x54455854 [65, 66, 67] "ABC"
dyn.ah62d4rv4gk81g7d3ru [1, 0, 0, 0, 0, 0, 16, 0, 14, 0, 3, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0] "\u{01}\0\0\0\0\0\u{10}\0\u{0E}\0\u{03}\0\0\0\u{0C}\0\0\0\0\0\0\0"
CorePasteboardFlavorType 0x7374796C [1, 0, 0, 0, 0, 0, 16, 0, 14, 0, 3, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0] "\u{01}\0\0\0\0\0\u{10}\0\u{0E}\0\u{03}\0\0\0\u{0C}\0\0\0\0\0\0\0"
public.utf16-plain-text [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
public.utf8-plain-text [65, 66, 67] "ABC"
public.utf16-plain-text [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
CorePasteboardFlavorType 0x75747874 [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
public.utf8-plain-text [65, 66, 67] "ABC"
NSStringPboardType [65, 66, 67] "ABC"
public.utf16-plain-text [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
public.utf8-plain-text [65, 66, 67] "ABC"
com.apple.traditional-mac-plain-text [65, 66, 67] "ABC"
dyn.ah62d4rv4gk81g7d3ru [1, 0, 0, 0, 0, 0, 16, 0, 14, 0, 3, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0] "\u{01}\0\0\0\0\0\u{10}\0\u{0E}\0\u{03}\0\0\0\u{0C}\0\0\0\0\0\0\0"
public.utf16-plain-text [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
CorePasteboardFlavorType 0x75747874 [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
public.utf8-plain-text [65, 66, 67] "ABC"
NSStringPboardType [65, 66, 67] "ABC"
com.apple.traditional-mac-plain-text [65, 66, 67] "ABC"
CorePasteboardFlavorType 0x54455854 [65, 66, 67] "ABC"
dyn.ah62d4rv4gk81g7d3ru [1, 0, 0, 0, 0, 0, 16, 0, 14, 0, 3, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0] "\u{01}\0\0\0\0\0\u{10}\0\u{0E}\0\u{03}\0\0\0\u{0C}\0\0\0\0\0\0\0"
CorePasteboardFlavorType 0x7374796C [1, 0, 0, 0, 0, 0, 16, 0, 14, 0, 3, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0] "\u{01}\0\0\0\0\0\u{10}\0\u{0E}\0\u{03}\0\0\0\u{0C}\0\0\0\0\0\0\0"
public.utf16-plain-text [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
public.utf8-plain-text [65, 66, 67] "ABC"
public.utf16-plain-text [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
CorePasteboardFlavorType 0x75747874 [65, 0, 66, 0, 67, 0] "A\0B\0C\0"
public.utf8-plain-text [65, 66, 67] "ABC"
NSStringPboardType [65, 66, 67] "ABC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment