summaryrefslogtreecommitdiff
path: root/vcl/workben/pasteboard.mm
blob: 4b003358f8c51e854b9940492bfa15dbe8e77b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*-

/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

// List the contents of the macOS pasteboard

// Build with: clang++ -Wall -o pasteboard vcl/workben/pasteboard.mm -framework AppKit

#import <unistd.h>

#import <iostream>
#import <AppKit/AppKit.h>

static void usage()
{
    std::cout << "Usage: pasteboard\n"
                 "        --List the types on the pasteboard and in each pasteboard item.\n"
                 "       pasteboard -t type\n"
                 "        --Output the data for the type in question to stdout. Note: output will "
                 "in many cases be binary.\n";
}

int main(int argc, char** argv)
{
    NSString* requestedType;

    int ch;

    while ((ch = getopt(argc, argv, "t:")) != -1)
    {
        switch (ch)
        {
            case 't':
                requestedType = [NSString stringWithUTF8String:optarg];
                break;
            case '?':
                usage();
                break;
        }
    }

    argc -= optind;
    argv += optind;

    if (argc > 0)
    {
        usage();
        return 1;
    }

    NSPasteboard* pb = [NSPasteboard generalPasteboard];

    if ([requestedType length] > 0)
    {
        NSData* data = [pb dataForType:requestedType];

        std::cout.write((const char*)[data bytes], [data length]);

        return 0;
    }

    {
        NSArray<NSPasteboardType>* types = [pb types];
        std::cout << "Types directly on pasteboard:\n";
        for (unsigned i = 0; i < [types count]; i++)
        {
            std::cout << "  " << i << ": " << [types[i] UTF8String] << "\n";
        }
    }

    NSArray<NSPasteboardItem*>* items = [pb pasteboardItems];
    std::cout << "New-style items on pasteboard:\n";

    for (unsigned i = 0; i < [items count]; i++)
    {
        std::cout << "  Item " << i << ", types:\n";
        NSArray<NSPasteboardType>* types = [items[i] types];
        for (unsigned j = 0; j < [types count]; j++)
        {
            std::cout << "    " << j << ": " << [types[j] UTF8String];

            if ([types[j] isEqualToString:(NSString*)kUTTypePlainText] ||
                [types[j] isEqualToString:(NSString*)kUTTypeUTF8PlainText] ||
                [types[j] isEqualToString:(NSString*)kUTTypeText] ||
                [types[j] isEqualToString:(NSString*)kUTTypeHTML] ||
                [types[j] isEqualToString:(NSString*)kUTTypeRTF] ||
                [types[j] isEqualToString:(NSString*)kUTTypeUTF16ExternalPlainText])
            {
                NSString* string = [items[i] stringForType:NSPasteboardTypeString];
                if ([string length] > 500)
                    string = [[string substringToIndex:501] stringByAppendingString:@"..."];
                std::cout << ": '" << [string UTF8String] << "'";
            }
            std::cout << "\n";
        }
    }

    return 0;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */