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++ -Wall -o pasteboard vcl/workben/pasteboard.mm -framework AppKit
18 #import <AppKit/AppKit.h>
22 std::cout << "Usage: pasteboard\n"
23 " List the types on the pasteboard and in each pasteboard item.\n"
25 " Output the data for all types to stdout. Note: output will\n"
26 " in many cases be binary. The different types are separated by a textual header.\n"
27 " pasteboard -t type\n"
28 " Output the data for the type in question to stdout. Note: output will\n"
29 " in many cases be binary.\n";
32 int main(int argc, char** argv)
34 NSString* requestedType;
38 while ((ch = getopt(argc, argv, "at:")) != -1)
46 requestedType = [NSString stringWithUTF8String:optarg];
63 NSPasteboard* pb = [NSPasteboard generalPasteboard];
65 if ([requestedType isEqualToString:@"*"])
67 NSArray<NSPasteboardType>* types = [pb types];
68 for (unsigned i = 0; i < [types count]; i++)
70 NSData* data = [pb dataForType:types[i]];
71 std::cout << i << ": " << [types[i] UTF8String] << ": " << std::to_string([data length]) << " bytes:\n";
74 std::cout.write((const char*)[data bytes], [data length]);
81 if ([requestedType length] > 0)
83 NSData* data = [pb dataForType:requestedType];
86 std::cerr << "No data for " << [requestedType UTF8String] << std::endl;
88 std::cout.write((const char*)[data bytes], [data length]);
94 NSArray<NSPasteboardType>* types = [pb types];
95 std::cout << "Types directly on pasteboard:\n";
96 for (unsigned i = 0; i < [types count]; i++)
98 std::cout << " " << i << ": " << [types[i] UTF8String] << "\n";
102 NSArray<NSPasteboardItem*>* items = [pb pasteboardItems];
103 std::cout << "New-style items on pasteboard:\n";
105 for (unsigned i = 0; i < [items count]; i++)
107 std::cout << " Item " << i << ", types:\n";
108 NSArray<NSPasteboardType>* types = [items[i] types];
109 for (unsigned j = 0; j < [types count]; j++)
111 std::cout << " " << j << ": " << [types[j] UTF8String];
113 if ([types[j] isEqualToString:(NSString*)kUTTypePlainText] ||
114 [types[j] isEqualToString:(NSString*)kUTTypeUTF8PlainText] ||
115 [types[j] isEqualToString:(NSString*)kUTTypeText] ||
116 [types[j] isEqualToString:(NSString*)kUTTypeHTML] ||
117 [types[j] isEqualToString:(NSString*)kUTTypeRTF] ||
118 [types[j] isEqualToString:(NSString*)kUTTypeUTF16ExternalPlainText])
120 NSString* string = [items[i] stringForType:NSPasteboardTypeString];
121 if ([string length] > 500)
122 string = [[string substringToIndex:501] stringByAppendingString:@"..."];
123 std::cout << ": '" << [string UTF8String] << "'";
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */