我想在我的Cocoa項目中使用一些Apple事件來訪問終端應用程序。我不想使用嵌入式AppleScript或任何編譯腳本,所以我開始研究NSAppleEventDescriptor
。獲取NSAppleEventDescriptor的AppleScript屬性
我已經成功地使用do script
命令創建了一個新窗口,並且我成功地從返回值中獲取窗口。我現在想要做的唯一事情就是獲得該窗口的屬性。
我現在已經知道如何獲得這樣的屬性,所以我開始使用Google搜索。不幸的是,沒有很多關於如何使用Apple事件的很好的例子,我沒有找到我想要的東西。
所以我開始深入挖掘。我做的第一件事是在Terminal .sdef
文件中尋找get
命令的代碼。我沒有找到它,所以我在我的/System/
目錄上做了grep
,我找到了/System/Library/Frameworks/AppleScriptKit.framework/Versions/A/Resources/AppleScriptKit.sdef
。我顯然找到了AppleScript語法的核心。該文件確實有getd
四個字符的代碼。
所以現在我知道我必須使用Core Suite中的getd
事件。但是,該命令的參數是objectSpecifier
。我搜索了一個使用kAEGetData
的示例。但是我沒有找到任何可以學習任何東西的代碼。
所以我在這裏問:我該如何建立這樣一個objectSpecifier
描述符?
這是我已經有了:
代碼來創建並獲得標籤描述
NSAppleEventDescriptor *createEvent;
NSAppleEventDescriptor *scriptParam;
AppleEvent aeReply;
OSErr err;
/* Make the do script event */
createEvent = [NSAppleEventDescriptor
appleEventWithEventClass:kAECoreSuite
eventID:kAEDoScript
targetDescriptor:_applicationDescriptor
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
if(createEvent == nil) {
NSLog(@"%s Failed to create a do script event",
__PRETTY_FUNCTION__);
return nil;
}
/* Make script parameter */
scriptParam = [NSAppleEventDescriptor descriptorWithString:@"echo Hello World"];
if(scriptParam == nil) {
NSLog(@"%s Failed to create the script parameter",
__PRETTY_FUNCTION__);
return nil;
}
/* Set parameter */
[createEvent setParamDescriptor:scriptParam forKeyword:keyDirectObject];
/* Send event */
err = AESendMessage([createEvent aeDesc], &aeReply,
kAEWaitReply | kAENeverInteract, kAEDefaultTimeout);
if(err != noErr) {
NSLog(@"%s Failed to send the create command",
__PRETTY_FUNCTION__);
return nil;
}
/* Retrieve information */
{
/* SEE BELOW TO SEE HOW I GET THE WINDOW DESCRIPTOR */
}
現在,我嘗試在獲得該選項卡的窗口成功
// NSAppleEventDescriptor *ansr is set to the result of the code above
NSAppleEventDescriptor *mainObj;
NSAppleEventDescriptor *desiredClass;
NSAppleEventDescriptor *window;
mainObj = [ansr paramDescriptorForKeyword:keyDirectObject];
if([mainObj descriptorType] != typeObjectSpecifier) {
NSLog(@"%s Main object was not an object specifier",
__PRETTY_FUNCTION__);
return nil;
}
desiredClass = [mainObj paramDescriptorForKeyword:keyAEDesiredClass];
if([desiredClass typeCodeValue] != kAETerminalTab) {
NSLog(@"%s Main object's desired class was not a Terminal tab",
__PRETTY_FUNCTION__);
return nil;
}
window = [mainObj paramDescriptorForKeyword:keyAEContainer];
if(window == nil) {
NSLog(@"%s Couldn't get container of the tab",
__PRETTY_FUNCTION__);
return nil;
}
desiredClass = [window paramDescriptorForKeyword:keyAEDesiredClass];
if([desiredClass typeCodeValue] != cWindow) {
NSLog(@"%s The container of the tab was not a window",
__PRETTY_FUNCTION__);
return nil;
}
return window;
現在我失敗getti NG,讓我們說,bounds屬性
// _windowDescriptor is the result of the code above
NSAppleEventDescriptor *getEvent;
NSAppleEventDescriptor *prop;
AppleEvent aeReply;
NSAppleEventDescriptor *reply;
FourCharCode propName;
OSErr err;
propName = keyAEBounds;
/* Create get event */
getEvent = [NSAppleEventDescriptor
appleEventWithEventClass:kAECoreSuite
eventID:kAEGetData
targetDescriptor:_windowDescriptor
returnID:kAutoGenerateReturnID
transactionID:kAnyTransactionID];
if(getEvent == nil) {
NSLog(@"%s Failed to create a get event",
__PRETTY_FUNCTION__);
return NSZeroRect;
}
/* Get property */
prop = [NSAppleEventDescriptor
descriptorWithDescriptorType:typeProperty
bytes:&propName length:sizeof(propName)];
if(prop == nil) {
NSLog(@"%s Failed to create the bounds property",
__PRETTY_FUNCTION__);
return;
}
/* Set parameter */
[getEvent setParamDescriptor:prop forKeyword:keyDirectObject];
/* Exectue */
err = AESendMessage([getEvent aeDesc], &aeReply,
kAEWaitReply | kAENeverInteract, kAEDefaultTimeout);
if(err != noErr) {
NSLog(@"%s Failed to send the get message",
__PRETTY_FUNCTION__);
return;
}
reply = [[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&aeReply];
[reply autorelease];
NSLog(@"Bounds: %@", reply);
如前所述,上面的代碼工作,只是直到最後塊。
非常感謝您的幫助。
其實我真的很喜歡使用Apple Event API的核心。 – v1Axvw 2012-03-19 15:35:29
太棒了,我很高興能夠提供一點幫助。 – 2012-03-21 20:42:22