訪問紙板直接看起來是一個不走。首先,使用紙板服務器的mach端口啓動won't register the processes1。您首先需要找到一種方法來獲取紙板服務器的馬赫端口(mach_port_names
?)。此外,用戶會話之間的直接通信是prohibited2,其他通信有限。我不確定您的程序是否有權連接到粘貼板服務器。
下面是使用Apple事件獲取&將剪貼板設置爲字符串的說明性示例的第一步。錯誤處理對於不存在是最小的(我不確定我對require_noerr
的看法)。如果您要在運行過程中多次獲取/設置剪貼板數據,則可以保存Apple事件,並在複製到剪貼板時使用AECreateDesc
& AEPutParamDesc
或(可能)AEBuildParameters
。 AEVTBuilder可能有用。
NSString* paste() {
NSString *content;
AppleEvent paste, reply = { typeNull, 0L };
AEBuildError buildError = { typeNull, 0L };
AEDesc clipDesc = { typeNull, 0L };
OSErr err;
err = AEBuildAppleEvent(kAEJons, kAEGetClipboard,
typeApplicationBundleID, "com.apple.finder", strlen("com.apple.finder"),
kAutoGenerateReturnID, kAnyTransactionID,
&paste, &buildError,
""
);
require_noerr(err, paste_end);
err = AESendMessage(&paste, &reply, kAEWaitReply, kAEDefaultTimeout);
err = AEGetParamDesc(&reply, keyDirectObject, typeUTF8Text, &clipDesc);
require_noerr(err, pastErr_getReply);
Size dataSize = AEGetDescDataSize(&clipDesc);
char* clipData = malloc(dataSize);
if (clipData) {
err = AEGetDescData(&clipDesc, clipData, dataSize);
if (noErr == err) {
content = [NSString stringWithCString:clipData encoding:NSUTF8StringEncoding];
} else {}
free(clipData);
}
AEDisposeDesc(&clipDesc);
pastErr_getReply:
AEDisposeDesc(&reply);
pasteErr_sending:
AEDisposeDesc(&paste);
paste_end:
return content;
}
OSStatus copy(NSString* clip) {
AppleEvent copy, reply = { typeNull, 0L };
AEBuildError buildError = { typeNull, 0L };
OSErr err = AEBuildAppleEvent(kAEJons, kAESetClipboard,
typeApplicationBundleID, "com.apple.finder", strlen("com.apple.finder"),
kAutoGenerateReturnID, kAnyTransactionID,
©, &buildError,
"'----':utf8(@)",
AEPARAMSTR([clip UTF8String])
/*
"'----':obj {form: enum(prop), want: type(@), seld: type(@), from: null()}"
"data:utf8(@)",
AEPARAM(typeUTF8Text),
AEPARAM(pClipboard),
AEPARAMSTR([clip UTF8String])
*/
);
if (aeBuildSyntaxNoErr != buildError.fError) {
return err;
}
AESendMessage(©, &reply, kAENoReply, kAEDefaultTimeout);
AEDisposeDesc(&reply);
AEDisposeDesc(©);
return noErr;
}
我要離開的核心基礎之上的做法,但你可能會想使用NSAppleEventDescriptor
提取從蘋果事件回覆剪貼板中的內容。
err = AESendMessage(&paste, &reply, kAEWaitReply, kAEDefaultTimeout);
require_noerr(err, pasteErr_sending);
// nsReply takes ownership of reply
NSAppleEventDescriptor *nsReply = [[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply];
content = [[nsReply descriptorAtIndex:1] stringValue];
[nsReply release];
pasteErr_sending:
AEDisposeDesc(&paste);
paste_end:
return content;
}
的NSAppleEventDescriptor
也更容易在調試器比AEDesc
檢查。要檢查回覆,您還可以在使用osascript或腳本編輯器時設置AEDebugReceives
環境變量。應用:
AEDebugReceives=1 osascript -e 'tell application "Finder" to get the clipboard'
參考文獻:
- "Configuring User Sessions"
- "Communicating Across Login Sessions"
- Mach Kernel Interface,尤其是:
- CFMessagePort Reference(Mach端口包裝器):
- Apple Events Programming Guide
- Apple Event Manager Reference
- AEBuild*, AEPrint* and Friends
- AEBuildAppleEvent上CocoaDev
- Mac OS X Debugging Magic(用於AEDebugSends等是AEDebug *環境變量)在Leopard或虎
啊,很好。值得一看。 – 2009-10-25 16:54:28
AppleScript使用Apple事件。它們很混亂,一些文檔很差。 – outis 2009-10-28 14:10:34