cocoa - 获取 NSAppleEventDescriptor 的 AppleScript 属性

标签 cocoa applescript appleevents

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

我已经成功使用do script命令创建了一个新窗口,并且我已经成功从返回值中获取了该窗口。我现在唯一想做的就是获取该窗口的属性。

我现在知道如何获得这样的属性(property),所以我开始谷歌搜索。不幸的是,没有很多关于如何使用 Apple Events 的好例子,而且我没能找到我想要的东西。

所以我开始更深入地挖掘。我做的第一件事就是在终端 .sdef 文件中查找 get 命令的代码。我找不到它,所以我在 /System/ 目录上执行了 grep ,然后找到了 /System/Library/Frameworks/AppleScriptKit.framework/Versions/A/Resources/AppleScriptKit.sdef。我显然找到了 AppleScript 语法的核心。该文件确实具有 getd 四字符代码。

现在我知道我必须使用核心套件中的 getd 事件。然而,该 get-command 的参数是一个 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);

正如所解释的,上面的代码有效,直到最后一个 block 。
预先感谢您的帮助。

最佳答案

感谢 Rob Keniger,我已经成功实现了我想要的目标。

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

此外,我将窗口描述符设置为 Apple 事件的接收器是错误的。您始终必须解决应用程序本身的问题,并将直接对象的 from (keyAEContainer) 属性设置为您想要的窗口。

这是工作代码,其中包含一些 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;
}

我希望这会对某人有所帮助。

关于cocoa - 获取 NSAppleEventDescriptor 的 AppleScript 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762205/

相关文章:

cocoa - 需要有关 Mac 应用程序编程开发的 Cocoa 教程的建议

macos - Mac OS X 中的 USB 设备控制

objective-c - 有没有办法以编程方式突出显示状态栏项目?

Applescript 逐行读取文件

c - 注册 AppleEvents

objective-c - 为什么 TableView 不显示绑定(bind)数据?

audio - 按下cmd + enter时applescript播放随机声音

swift - AppleScript 电子邮件附件在处理程序中不起作用

objective-c - 应用程序退出事件

objective-c - 从 Cocoa 应用程序访问 Safari 文档的 DOM