Last active
November 19, 2023 12:47
-
-
Save kuzux/0706077adb57624e7b2d3ee540a7b095 to your computer and use it in GitHub Desktop.
accessing the osx clipboard via c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
void write_to_clipboard(const char* zero_terminated_string); | |
#ifdef __cplusplus | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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