Using Apple Events in Osaxen

I had to figure all this gunk out on my own. If anyone knows any good books to recommend on dealing with AppleEvents, please, let me know.

Parameter Passing

The hardest part was figuring just how the heck the Macintosh was sending its parameters to me. Strings are pretty simple. They’re just strings. (That’s “typeChar” in C.)

Parameters make extensive use of “Size”s. That’s a four-letter code, like the resource manager uses for creator and file type. You can pull them off of a pointer with *(Size *)ThePointer.

Lists

  • C Type: AEList
  • Four-character code: ‘list’
  1. The first four characters are a long integer, representing the number of items in the list.
  2. The second four characters have, in my experience, always been zero.
  3. Repeat the following for every item in the list:
    1. The first four characters are the type of the next item. For example, ‘TEXT’ (typeChar).
    2. The second four characters are a long integer, representing the number of characters in this item.
    3. The rest of the characters are in fact the item itself. There are always an even number of characters. If the actual item length is odd, the item is padded with a single zero. If the actual item length is even, the item is not padded. You must rely on the second four characters to determine the length—do not use strlen(), as you cannot count on a trailing zero. If you are skipping through items, however, remember to bounce odd lengths up by one, or you’ll get off course pretty rapidly.

For example, I’ve got the list {"Hello",5,"test"}. Here’s how it’ll be represented:

00 00 00 03 00 00 00 00 There are three items in the list.
54 45 58 54 The first item is type TEXT.
00 00 00 05 The first item is five characters long.
48 65 6C 6C 6F 00 “Hello”, padded with a zero.
6E 6F 6C 67 The second item is a ‘long’ integer.
00 00 00 04 A long integer is four characters long.
00 00 00 05 And the actual item is “5”.
54 45 58 54 The last item is type TEXT.
00 00 00 04 The last item is four characters long.
74 65 73 74 “test”.

Records

Records are a lot like lists.

  • C Type: AERecord
  • Four-letter code: ‘reco’
  1. The first four characters are ‘reco’. Check for it with if (*(Size *)TheParameter == AERecord) {}.
  2. The second four characters are the length of the record, in characters. Add this to the beginning of the record and you get the end of the record.
  3. The third four characters are the number of items in the record.
  4. The fourth four characters have always been zeros in my experience.
  5. Repeat the following for each item in the record:
    1. The first four characters are the Size code for the item’s name. For example, in {Title:"Invisibles", Author:"Grant Morrison"}, the two items in the record are “Title” and “Author”. They get abbreviated, and will most likely be abbreviated to ‘titl’ and ‘auth’.
    2. The second four characters are the type of the item. For example, ‘long’ (typeInteger) for a number, or ‘TEXT’ (typeChar) for a string.
    3. The third four characters are the length of the item.
    4. The rest of the characters are the item itself. If the item is an odd length, it is padded with a single zero.

Styled Text

I only understand enough about styled text to extract the text and leave the style alone. I probably don’t even know that much.

  1. The first four characters are the number of “items”. As far as I can see, this is always two: styled text has two items, the text, and the styles.
  2. The second four characters are zeros.
  3. The third four characters are ‘ktxt’.
  4. The fourth four characters are ‘TEXT’.
  5. The fifth four characters are the size of the text.
  6. Then we have the actual text itself, padded with a zero if the text is an odd length.
  7. Then, we have the first four characters of the styled: ‘ksty’.
  8. The second four characters are ‘styl’.
  9. The third four characters are the size of the style info.
  10. The rest of it is the style info, which I don't understand. It looks as though it may be paragraph delimited.