Today I struggled with the problem where I was getting a -1704 (kAENotAEDesc) error from one of my AppleEvent functions. More specifically, AEPutKeyDesc. I traced back through my thing: yes, I had AECreateList()ed the proper list.
This problem was also funny, since this code works perfectly in one case, but not the case I was after.
This is basic AppleEvent stuff. I’ve been using AppleEvents for the last 6 years or so, so I can do the basics in my sleep. Everything was right. Except one little thing:
The AEDesc that I passed to AEPutKeyDesc was a list, and not a record. You see, the raw AppleEvent manager is not typesafe. AEDescs mingle with AEDescLists and AEDescLists mingle with AERecords, because they’re all just
typedefs of AEDesc. There is no typesafety because, to the compiler, they all are of them same type.
Having a typesafe AE Manager would have saved me here - I would have known that what I was passing around a list instead of a AERecord.
I see why the AppleEvent manager isn’t typesafe - the lax type-safety allows you to put anything into the contents of an AppleEvent, without having having to worry about typesafety. Typesafety could be really hard to maintain, since you have applications trading data willy-nilly, and since typesafety is a compile time feature, well, it probably wouldn’t be very helpful.
That same “typesafety is a compile-time thing, but we’re at run-time” determines the AppleEvent Manager’s safety rule: “Ask the AE Manager to extract your data as a certain type, but that might not be how you go it.”
For some things I do like (compile time) typechecking - it provides another “test” for the application. If everything passes the strict type checking, then everything knows what kind of data to expect from everything else. (Unit testing then determines if the data makes any sense, or if it’s just N bytes of garbage of one form or another.) That’s what I was doing to AEPutKeyDesc - feeding it garbage.
But it still would be nice sometimes to have a type-safe AE manager. MacApp’s CAEDesc_AC doesn’t seem to have exactly what I want, although Nitrogen looks like it may hold some promise on this front (but I don’t want to include the entire framework just for typesafe versions of AEDesc, AEDescList, and AERecord.)
We shall see.