We have the following snippet.
OSStatus createErr = PasteboardCreate(kPasteboardClipboard, &m_pboard);
if (createErr != noErr) {
LOG((CLOG_DEBUG "failed to create clipboard reference: error %i" createErr));
}
This compiles fine, however, it fails to run when called from SSH. This is because there is no pasteboard available in the SSH terminal. However, the idea here is to share clipboards between computers.
When run from desktop terminal, this works just fine. But when run from SSH, PasteboardCreate returns -4960 (aka, coreFoundationUnknownErr). I assume that the only way around this issue is to run the application from within the same environment as the pasteboard, but is this possible?
Accessing the pasteboard directly looks to be a no-go. First, launchd won’t register the processes1 with the pasteboard server’s mach port. You’d first need find a way to get the pasteboard server’s mach port (
mach_port_names?). Also, direct communication between user sessions is prohibited2, and other communication is limited. I’m not sure if your program will have the rights to connect to the pasteboard server.Here’s a first shot at an illustrative example on using Apple events to get & set the clipboard as a string. Error handling is minimal to nonexistent (I’m not certain how I feel about
require_noerr). If you’re going to get/set clipboard data multiple times during a run, you can save the Apple events and, when copying to the clipboard, useAECreateDesc&AEPutParamDescor (maybe)AEBuildParameters. AEVTBuilder might be of use.I’m leaving the Core Foundation approach above, but you’ll probably want to use
NSAppleEventDescriptorto extract the clipboard contents from the Apple Event reply.An
NSAppleEventDescriptoris also easier to examine in a debugger than anAEDesc. To examine replies, you can also to set theAEDebugReceivesenvironment variable when using osascript or Script Editor.app:References:
mach_msg_headermach_msgCFMessagePortCreateRemoteCFMessagePortSendRequest