正在获取NSAppleEventDescriptor的AppleScript属性



我正试图在我的Cocoa项目中使用一些Apple事件来访问终端应用程序。我不想使用嵌入式AppleScripts或任何编译的脚本,所以我开始研究NSAppleEventDescriptor

我已经成功地使用do script命令创建了一个新窗口,并成功地从返回值中获取了该窗口。我现在唯一想做的就是得到那个窗户的一处房产。

我现在已经知道如何获得这样的房产了,所以我开始在谷歌上搜索。不幸的是,没有太多关于如何使用Apple Events的好例子,我也没能找到我想要的东西。

所以我开始挖得更深。我做的第一件事是在Terminal .sdef文件中查找get命令的代码。我找不到它,所以我在/System/目录上做了一个grep,找到了/System/Library/Frameworks/AppleScriptKit.framework/Versions/A/Resources/AppleScriptKit.sdef。我显然找到了AppleScript语法的核心。该文件确实包含getd四个字符的代码。

所以现在我知道我必须使用Core Suite中的getd事件。但是,get命令的参数是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;

现在我无法获得,比方说,边界属性

// _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);

如上所述,上面的代码一直工作到最后一个块
提前感谢您的帮助。

多亏了Rob Keniger,我成功地实现了我想要的。

显然,我必须创建一个记录描述符,设置我想要的属性,然后将其强制为typeObjectSpecifier

此外,我错误地将窗口描述符设置为我的Apple Event的接收器。您总是必须寻址应用程序本身,并将直接对象的fromkeyAEContainer)属性设置为所需的窗口。

以下是工作代码,带有一点NSLog语句:

- (NSRect)bounds {
    // ! ! !
    // _windowDescriptor is an instance variable which points to a valid
    // window NSAppleEventDescriptor
    // ! ! !
    NSAppleEventDescriptor *getEvent;
    NSAppleEventDescriptor *objSpec;
    NSAppleEventDescriptor *propEnum, *propType, *propSeld;
    AppleEvent aeReply;
    NSAppleEventDescriptor *reply;
    FourCharCode propName;
    OSErr err;
    propName = keyAEBounds;
    /* Create get event */
    getEvent = [NSAppleEventDescriptor
                appleEventWithEventClass:kAECoreSuite 
                eventID:kAEGetData 
                targetDescriptor:[[FTMTerminalApp sharedApp] AEDescriptor] 
                returnID:kAutoGenerateReturnID 
                transactionID:kAnyTransactionID];
    if(getEvent == nil) {
        NSLog(@"%s Failed to create a get event",
              __PRETTY_FUNCTION__);
        return NSZeroRect;
    }
    /* Get property */
    /* create object specifier main ojcect */
    objSpec = [[[NSAppleEventDescriptor alloc] initRecordDescriptor] 
               autorelease];
    if(objSpec == nil) {
        NSLog(@"%s Failed to create the object specifier",
              __PRETTY_FUNCTION__);
        return NSZeroRect;
    }
    NSLog(@"%s Created object specifier %@",
          __PRETTY_FUNCTION__, objSpec);
    /* create property enum, we want a property */
    propEnum = [NSAppleEventDescriptor
                descriptorWithEnumCode:formPropertyID];
    if(propEnum == nil) {
        NSLog(@"%s Failed to create the property enum",
              __PRETTY_FUNCTION__);
        return NSZeroRect;
    }
    NSLog(@"%s Created property enum %@",
          __PRETTY_FUNCTION__, propEnum);
    [objSpec setDescriptor:propEnum forKeyword:keyAEKeyForm];
    /* create prop type */
    propType = [NSAppleEventDescriptor
                descriptorWithTypeCode:typeProperty];
    if(propType == nil) {
        NSLog(@"%s Failed to create the property type",
              __PRETTY_FUNCTION__);
        return NSZeroRect;
    }
    NSLog(@"%s Created property type %@",
          __PRETTY_FUNCTION__, propType);
    [objSpec setDescriptor:propType forKeyword:keyAEDesiredClass];
    /* create property key data */
    propSeld = [NSAppleEventDescriptor
                descriptorWithTypeCode:keyAEBounds];
    if(propSeld == nil) {
        NSLog(@"%s Failed to create the bounds property type",
              __PRETTY_FUNCTION__);
        return NSZeroRect;
    }
    NSLog(@"%s Created property key data %@",
          __PRETTY_FUNCTION__, propSeld);
    [objSpec setDescriptor:propSeld forKeyword:keyAEKeyData];
    /* Set destination */
    NSLog(@"%s Setting from key %@",
          __PRETTY_FUNCTION__, _windowDescriptor);
    [objSpec setDescriptor:_windowDescriptor forKeyword:keyAEContainer];
    /* Send message */
    objSpec = [objSpec coerceToDescriptorType:typeObjectSpecifier];
    NSLog(@"Coerced: %@", objSpec);
    [getEvent setParamDescriptor:objSpec forKeyword:keyDirectObject];
    err = AESendMessage([getEvent aeDesc], &aeReply, 
                        kAEWaitReply | kAENeverInteract, kAEDefaultTimeout);
    if(err != noErr) {
        NSLog(@"%s Failed to send the message (event = %@)",
              __PRETTY_FUNCTION__, getEvent);
        return NSZeroRect;
    }
    reply = [[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&aeReply];
    NSLog(@"BOUNDS = %@", reply);
    [reply autorelease];
    return NSZeroRect;
}

我希望这能帮助到别人。

Apple事件使用起来很复杂。除非你真的想花时间处理复杂且普遍令人讨厌的API,否则我建议你使用Mike Ash出色的AEVTBuilder类来节省大量时间和心痛。

这是一个很好的可可包装所有讨厌的碳苹果事件代码。

相关内容

  • 没有找到相关文章

最新更新