AEDesc and NSAppleEventDescriptor

  • Hi All,

    I'm trying to make a contextual menu module

    (see /Developer/examples/carbon/SampleCMPlugin/ )

    Now, I can see some of you wondering why I'm posting about a carbon
    example

    well, I'm attempting to convert much of the code to using Objective-C
    and the like
    and I'm having trouble getting it to work, from what I understand an
    NSAppleEventDescriptor
    is just an Objective-C wrapper for AEDesc and friends.

    Now my trouble is, I'm getting everything working correctly but when
    my CMM comes back to the main
    function call (i suppose it's the equivalent of main for a CMM [CMM
    is short for Contextual Menu Module for those
    of you don't know the term =) ])

    as you can see from the code below, I'm doing the messy business of
    mixing Cocoa and Carbon code together,

    my main problem here though is once i get back to the
    MyPlugintExamineContext() I can't really figure out how to put
    the built NSAppleEventDescriptor into outCommandPairs, my NSLog below
    prints something to the effect
    "No valid AEDesc"

    please I would appreciate it a lot if some one could help me, if you
    want to message me on instant messenger I'm almost
    always on that.

    AIM: ss2cire
    Yahoo!: ss2cire (at) yahoo.com
    MSN: ss2cire (at) hotmail.com
    (change (at) to @ )

    thanks in advance and please dont hesitate to let me know if you need
    more info...

    regards

    Eric

    my code:

    static OSStatus MyPlugintExamineContext(void* thisInstance, const
    AEDesc* inContext, AEDescList* outCommandPairs)
    {
    // Sequence the command ids
    SInt32 result;

    // Verify that we've got an up-to-date CMM
    verify_noerr( Gestalt( gestaltContextualMenuAttr, &result ) );

    // make sure the descriptor isn't null
    if ( inContext != NULL )
    {
      // Create create the New Document (ND) Submenu
      NSAppleEventDescriptor *commandPairs = [NSAppleEventDescriptor
    listDescriptor];
      CreateMyPluginSubmenu( commandPairs );
      NSLog(@"Command Pairs: %@", commandPairs);
    AEPutParamDesc(
      outCommandPairs,    // the list we're putting our command into
      0,      // stick this command onto the end of our list
      [[[commandPairs coerceToDescriptorType:typeAEList] data]
    bytes] );    // the command I'm putting into the list*/

      NSData *commandPairsData = [[commandPairs
    coerceToDescriptorType:typeAEList] data];
      NSLog(@"%@", [NSAppleEventDescriptor
    descriptorWithDescriptorType:typeAEList
            data:[NSData dataWithBytes:[[[commandPairs
    coerceToDescriptorType:typeAEList] data] bytes]
                length:sizeof([[[commandPairs
    coerceToDescriptorType:typeAEList] data] bytes])]]);
    }
    else
    {
      printf( "NewDocumentPluginExamineContext: Hey! What's up with the
    NULL descriptor?\n" );
    }

    return noErr;
    }

    static OSStatus CreateMyMenuSubmenu(NSAppleEventDescriptor*
    ioCommandList)
    {
    OSStatus theError = noErr;

    NSAppleEventDescriptor *theSubmenuCommands;
    NSAppleEventDescriptor *theSupercommand;
    NSString *theSupercommandText = @"New Document";

    // the first thing we should do is create an AEDescList of
    // subcommands

    // set up the AEDescList
    //theError = AECreateList( NULL, 0, false, &theSubmenuCommands );
    theSubmenuCommands = [NSAppleEventDescriptor listDescriptor];
    //require_noerr( theError, CreateNDSubmenu_Complete_fail );

    // stick some commands in this subcommand list
    theError = AddCommandToAEDescList(@"Subcommand 1", typeCFStringRef,
    1001, 0, 0, theSubmenuCommands);
    theError = AddCommandToAEDescList(@"Subcommand 2", typeCFStringRef,
    1002, 0, 0, theSubmenuCommands);

    // now, we need to create the supercommand which will "own" the
    // subcommands.  The supercommand lives in the root command list.
    // this looks very much like the AddCommandToAEDescList function,
    // except that instead of putting a command ID in the record,
    // we put in the subcommand list.

    // create an apple event record for our supercommand
    theSupercommand = [NSAppleEventDescriptor recordDescriptor];

    // stick the command text into the aerecord
    NSAppleEventDescriptor *superCommandText = [NSAppleEventDescriptor
    descriptorWithString:theSupercommandText];

    [theSupercommand  setDescriptor:superCommandText forKeyword:keyAEName];

    // stick the subcommands into into the AERecord
    [theSupercommand setParamDescriptor:theSubmenuCommands
    forKeyword:keyContextualMenuSubmenu];

        return theError;

    } // CreateMyPluginSubmenu

    static OSStatus AddCommandToAEDescList(NSString *inCommandString,
    DescType inDescType,
        SInt32 inCommandID, MenuItemAttributes inAttributes, UInt32
    inModifiers,
        NSAppleEventDescriptor* ioCommandList)
    {
    OSStatus theError = noErr;

    NSAppleEventDescriptor *theCommandRecord = [NSAppleEventDescriptor
    recordDescriptor];

    // create an apple event record for our command
    //theError = AECreateList( NULL, kAEDescListFactorNone, true,
    &theCommandRecord );
    //require_noerr( theError, AddCommandToAEDescList_fail );

    // stick the command text into the AERecord
    if ( inCommandString != NULL )
    {
      //NSLog(@"Command string is not NULL");
      if ( inDescType == typeCFStringRef )
      {

      NSString *myString = [[NSString alloc]
    initWithString:inCommandString];
      NSAppleEventDescriptor *theCommandString = [NSAppleEventDescriptor
    descriptorWithString:myString];

      [theCommandRecord setDescriptor:theCommandString
    forKeyword:keyAEName];

      // do not release the string; the Contextual Menu Manager will
    release it for us
      }
    }

    // stick the command ID into the AERecord
    if ( inCommandID != 0 )
    {
      NSAppleEventDescriptor *commandID = [NSAppleEventDescriptor
    descriptorWithInt32:inCommandID];
      [theCommandRecord setAttributeDescriptor:commandID
    forKeyword:keyContextualMenuCommandID];
    }

    // stick the attributes into the AERecord
    if ( inAttributes != 0 )
    {
      NSAppleEventDescriptor *attributes = [NSAppleEventDescriptor
    descriptorWithInt32:inAttributes];
      [theCommandRecord setAttributeDescriptor:attributes
    forKeyword:keyContextualMenuAttributes];
    }

    // stick the modifiers into the AERecord
    if ( inModifiers != 0 )
    {
      NSAppleEventDescriptor *modifiers = [NSAppleEventDescriptor
    descriptorWithInt32:inModifiers];
      [theCommandRecord setAttributeDescriptor:modifiers
    forKeyword:keyContextualMenuModifiers];
    }

    // stick this record into the list of commands that we are
    // passing back to the CMM
    [ioCommandList insertDescriptor:theCommandRecord atIndex:0];

        return theError;

    } // AddCommandToAEDescList
  • ss2cire wrote
    :
    > I'm trying to make a contextual menu module
    > [...]
    > my main problem here though is once i get back to the
    > MyPlugintExamineContext() I can't really figure out how to put
    > the built NSAppleEventDescriptor into outCommandPairs, my NSLog below
    > prints something to the effect
    > "No valid AEDesc"

    One thing that immediately jumps out is the following:

    > AEPutParamDesc(
    > outCommandPairs,             // the list we're putting our command into
    > 0,                         // stick this command onto the end of our list
    > [[[commandPairs coerceToDescriptorType:typeAEList] data]
    > bytes] );    // the command I'm putting into the list*/

    AEPutParamDesc requires an AEDesc for its third argument but you're
    passing it the AEDesc's data handle. Use the following instead:

    [[commandPairs coerceToDescriptorType:typeAEList] aeDesc]

    There may be other issues as well, but I'm not familiar with CMM
    programming so will defer to others with more experience there.

    HTH

    has
    --
    http://appscript.sourceforge.net
    http://rb-appscript.rubyforge.org
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today