1 // -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*-
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 // List the contents of the macOS pasteboard
13 // Build with: clang++ -g -Wall -o pasteboard pasteboard.mm -framework AppKit -framework UniformTypeIdentifiers
18 #import <AppKit/AppKit.h>
19 #import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
23 std::cout << "Usage: pasteboard\n"
24 " List the types on the pasteboard and in each pasteboard item.\n"
26 " Output the data for all types to stdout. Note: output will\n"
27 " in many cases be binary. The different types are separated by a textual header.\n"
28 " pasteboard -t type\n"
29 " Output the data for the type in question to stdout. Note: output will\n"
30 " in many cases be binary.\n";
33 int main(int argc, char** argv)
35 NSString* requestedType = @"";
39 while ((ch = getopt(argc, argv, "at:")) != -1)
47 requestedType = [NSString stringWithUTF8String:optarg];
64 NSPasteboard* pb = [NSPasteboard generalPasteboard];
66 if ([requestedType isEqualToString:@"*"])
68 NSArray<NSPasteboardType>* types = [pb types];
69 for (unsigned i = 0; i < [types count]; i++)
71 NSData* data = [pb dataForType:types[i]];
72 std::cout << i << ": " << [types[i] UTF8String] << ": " << std::to_string([data length]) << " bytes:\n";
75 std::cout.write((const char*)[data bytes], [data length]);
82 if ([requestedType length] > 0)
84 NSData* data = [pb dataForType:requestedType];
87 std::cerr << "No data for " << [requestedType UTF8String] << std::endl;
89 std::cout.write((const char*)[data bytes], [data length]);
95 NSArray<NSPasteboardType>* types = [pb types];
96 std::cout << "Types directly on pasteboard:\n";
97 for (unsigned i = 0; i < [types count]; i++)
99 std::cout << " " << i << ": " << [types[i] UTF8String] << "\n";
103 NSArray<NSPasteboardItem*>* items = [pb pasteboardItems];
104 std::cout << "New-style items on pasteboard:\n";
106 for (unsigned i = 0; i < [items count]; i++)
108 std::cout << " Item " << i << ", types:\n";
109 NSArray<NSPasteboardType>* types = [items[i] types];
110 for (unsigned j = 0; j < [types count]; j++)
112 std::cout << " " << j << ": " << [types[j] UTF8String] << std::flush;
114 if ([types[j] isEqualToString:[UTTypePlainText identifier]] ||
115 [types[j] isEqualToString:[UTTypeUTF8PlainText identifier]] ||
116 [types[j] isEqualToString:[UTTypeText identifier]] ||
117 [types[j] isEqualToString:[UTTypeHTML identifier]] ||
118 [types[j] isEqualToString:[UTTypeRTF identifier]] ||
119 [types[j] isEqualToString:[UTTypeUTF16ExternalPlainText identifier]])
121 NSString* string = [items[i] stringForType:NSPasteboardTypeString];
122 if ([string length] > 500)
123 string = [[string substringToIndex:501] stringByAppendingString:@"..."];
124 std::cout << ": '" << [string UTF8String] << "'";
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */