#include <Carbon/Carbon.h>
int main (int argc, const char * argv[]) {
char *bundleID = "com.apple.textedit";
AppleEvent event, reply;
AEDesc listDesc, nameDesc;
Size nameSize;
char *name;
long listLen, i;
OSStatus err;
// build Apple event
err = AEBuildAppleEvent(kAECoreSuite,
kAEGetData,
typeApplicationBundleID,
bundleID,
strlen(bundleID),
kAutoGenerateReturnID,
kAnyTransactionID,
&event,
NULL,
"'----':'obj '{form:prop, want:type(prop), seld:type(pnam), from:"
"'obj '{form:indx, want:type(cwin), seld:abso('all '), from:()}}");
if (err) return err;
// send Apple event
err = AESendMessage(&event, &reply, kAEWaitReply, kAEDefaultTimeout);
AEDisposeDesc(&event);
if (err) return err;
// get return value from reply event
err = AEGetParamDesc(&reply,
keyAEResult,
typeAEList,
&listDesc);
AEDisposeDesc(&reply);
if (err) return err;
// get each descriptor from list
err = AECountItems(&listDesc, &listLen);
if (err) goto cleanup;
for (i=0; i<listLen; i++) {
AEKeyword key;
err = AEGetNthDesc(&listDesc,
i + 1,
typeUTF8Text,
&key,
&nameDesc);
if (err) goto cleanup;
// get UTF8 text from descriptor
nameSize = AEGetDescDataSize(&nameDesc);
name = malloc(nameSize);
err = AEGetDescData(&nameDesc,
name,
nameSize);
AEDisposeDesc(&nameDesc);
if (err) goto cleanup;
// do stuff with it...
printf("%.*s\n", nameSize, name);
free(name);
}
cleanup:
AEDisposeDesc(&listDesc);
return err;
}