9

I'm searching for an application that can show me the details of the content of the clipboard.

When copying some data into the clipboard, the data is associated with a specific MIME type. Normal text is text/plain, binary data can be copied as application/octet-stream, etc. I have an application that copies binary data, tagging it as its own MIME type, and I would like to see what type it is, and what data it has.

I can't just paste the clipboard content into a target notepad-like application, because the target expects the MIME type of the clipboard object to be text/plain.

An application that enumerates all currently existing MIME types of objects in the clipboard would also suffice.

6

Use xclip:

xclip -o -t TARGETS

to see all the available types. For example:

  1. copy something from you web browser
  2. investigate available types
$ xclip -o -t TARGETS
TIMESTAMP
TARGETS
MULTIPLE
text/html
text/_moz_htmlcontext
text/_moz_htmlinfo
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/x-moz-url-priv
  1. get the contents for the one you're interested in: xclip -o -t text/html
3

OK, I've actually written some code that does what I need. Good thing it's pretty easy in Qt.

Building info is at the bottom of this post.

xclipshow.cpp:

#include <QApplication>
#include <QTimer>
#include <QClipboard>
#include <QMimeData>
#include <QDebug>
#include <QStringList>

class App: public QObject {
    Q_OBJECT
private:
    void main();
public:
    App(): QObject() { }
public slots:
    void qtmain() { main(); emit finished(); }
signals:
    void finished();
};

void App::main() {
    QClipboard *clip = QApplication::clipboard();

    for(QString& formatName: clip->mimeData()->formats()) {
        std::string s;
        s = formatName.toStdString();

        QByteArray arr = clip->mimeData()->data(formatName);
        printf("name=%s, size=%d: ", s.c_str(), arr.size());

        for(int i = 0; i < arr.size(); i++) {
            printf("%02x ", (unsigned char) arr.at(i));
        }

        printf("\n");
    }
}

int main(int argc, char **argv) {
    QApplication app(argc, argv);
    App *task = new App();
    QObject::connect(task, SIGNAL(finished()), & app, SLOT(quit()));
    QTimer::singleShot(0, task, SLOT(qtmain()));
    return app.exec();
}

#include "xclipshow.moc"

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.0)
project(xclipshow)
find_package(Qt5Widgets)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(SRC
    xclipshow.cpp)

add_definitions(-std=c++11)
add_executable(xclipshow ${SRC})
qt5_use_modules(xclipshow Widgets Core)

Building info as requested in the comment by @slm: it depends on the system you're using. This code needs Qt5 and CMake to compile. If you have both, all you need to do is to run:

BUILD_DIR=<path to an empty temporary dir, which will contain the executable file>
SRC_DIR=<path to the directory which contains xclipshow.cpp>

$ cd $BUILD_DIR
$ cmake $SRC_DIR
$ make

or 'gmake' if you're on FreeBSD, or 'mingw32-make' if you're on Windows, etc.

If you don't have Qt5 or CMake, you can try to get away with Qt4 and manual compilation:

$ moc xclipshow.cpp > xclipshow.moc
$ g++ xclipshow.cpp -o xclipshow `pkg-config --cflags --libs QtGui` -I. --std=c++11

If you're getting information about invalid --std=c++11 option, try --std=c++0x instead, and consider upgrading your compiler ;).

  • 1
    Thanks for posting this solution. Could you add a bit of detail about how to compile it for future visitors? – slm Oct 20 '14 at 11:31
  • 2
    @slm, antonone, I've simplified/shortened your code, should also be easier to compile that way: gist.github.com/gsauthof/c955f727606f4a5b0cc2 – maxschlepzig Jan 27 '16 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.