Получение свойства AppleScript NSAppleEventDescriptor

Я пытаюсь использовать некоторые события Apple в своем проекте Cocoa, чтобы получить доступ к приложению Terminal. Я не хотел использовать встроенные сценарии AppleScripts или какие-либо скомпилированные сценарии, поэтому я начал изучать NSAppleEventDescriptor,

Мне удалось создать новое окно, используя do script команда, и мне удалось получить окно из возвращаемого значения. Единственное, что я хочу сделать сейчас, - это получить свойство этого окна.

Теперь у меня была идея, как получить такую ​​собственность, поэтому я начал гуглить. К сожалению, хороших примеров использования Apple Events не так много, и мне не удалось найти то, что я искал.

Поэтому я начал копать глубже. И первым делом я искал код для get команда в Терминале .sdef файл. Я не смог найти его, поэтому я сделал grep на моем /System/ каталог и я нашел /System/Library/Frameworks/AppleScriptKit.framework/Versions/A/Resources/AppleScriptKit.sdef, Я, очевидно, нашел ядро ​​синтаксиса AppleScript. Этот файл действительно имел getd четырехзначный код

Так что теперь я знаю, что я должен использовать getd событие из Core Suite. Однако аргумент этой команды 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);

Как объяснено, приведенный выше код работает, только до последнего блока.
Заранее благодарю за помощь.

2 ответа

Решение

Благодаря Робу Кенигеру я добился того, чего хотел.

Очевидно, мне пришлось создать дескриптор записи, установить нужные свойства и затем принудительно установить его в typeObjectSpecifier,

Кроме того, я ошибся в настройке дескриптора окна в качестве получателя моего Apple Event. Вы всегда должны обратиться к самому приложению и установить 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;
}

Надеюсь, это кому-нибудь поможет.

Apple Events сложны в использовании. Если вы действительно не хотите тратить время на работу с замысловатым и в целом неприятным API, я рекомендую вам сэкономить много времени и душевных страданий с помощью превосходного Майка Эша. AEVTBuilder учебный класс.

Это хорошая оболочка Какао для всего мерзкого кода Carbon Apple Event.

Другие вопросы по тегам