Skip to content

Instantly share code, notes, and snippets.

@kuzux
Last active November 19, 2023 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuzux/0706077adb57624e7b2d3ee540a7b095 to your computer and use it in GitHub Desktop.
Save kuzux/0706077adb57624e7b2d3ee540a7b095 to your computer and use it in GitHub Desktop.
accessing the osx clipboard via c++
set -ex
clang -x objective-c -c osx.m -o osx.o
g++ -std=c++17 -c main.cpp -o main.o
g++ main.o osx.o -framework AppKit -o cliptest
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void write_to_clipboard(const char* zero_terminated_string);
#ifdef __cplusplus
}
#endif
#include <iostream>
#include <array>
#include "capi.h"
using namespace std;
constexpr size_t BUFSIZE = 640 * 1024; // should be enough for everyone
array<char, BUFSIZE> buffer;
int main(int argc, char** argv) {
cin.read(buffer.data(), buffer.size());
write_to_clipboard(buffer.data());
return 0;
}
#import <AppKit/AppKit.h>
void write_to_clipboard(const char* zero_terminated_string) {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSString *string = [NSString stringWithUTF8String:zero_terminated_string];
[pasteboard clearContents];
[pasteboard setString:string forType:NSPasteboardTypeString];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment