Last active
December 18, 2023 07:41
-
-
Save mogenson/3bdd41ea1abcba73e8791f4befef03b3 to your computer and use it in GitHub Desktop.
Write to the MacOS clipboard from LuaJIT via FFI bindings to Objective-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
ffi = require("ffi") | |
C = ffi.C | |
ffi.cdef([[ | |
typedef struct objc_object *id; | |
typedef struct objc_selector *SEL; | |
id objc_getClass(const char*); | |
SEL sel_registerName(const char*); | |
id objc_msgSend(id,SEL); | |
id NSPasteboardTypeString; | |
]]) | |
ffi.load("/System/Library/Frameworks/AppKit.framework/AppKit") | |
input = ffi.cast("char*", arg[1] or io.read()) | |
pboard = C.objc_msgSend(C.objc_getClass("NSPasteboard"), C.sel_registerName("generalPasteboard")) | |
C.objc_msgSend(pboard, C.sel_registerName("clearContents")) | |
str = ffi.cast("id(*)(id,SEL,char*)", C.objc_msgSend)(C.objc_getClass("NSString"), | |
C.sel_registerName("stringWithUTF8String:"), | |
input) | |
ret = ffi.cast("bool(*)(id,SEL,id,id)", C.objc_msgSend)(pboard, | |
C.sel_registerName("setString:forType:"), | |
str, | |
C.NSPasteboardTypeString) | |
os.exit(ret) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment