Hello.
I'm primarily a system programmer, unix one. Currently I have to implement
the following logic:
On Mac OS X enumerate all running application only those that are displaying
windows.
Find which of them is "active", where "active" is defined as "has focuse".
Then find name of this application and log it.
It looks like the only (well, viable, as it must be done about 10 times per
second) way to do it (for me, at last) is Carbon and Accessibility API. I
don't know much about Mac windowing system and unfortunatelly can't spend
few months learning and possibly mastering it. So I'm asking for help
Here is code I got currently (highly experimental) with questions as
comments in it
<code>
#include <iostream>
#include <Carbon.h>
int main()
{
OSErr err = noErr;
OSStatus oss = noErr;
ProcessSerialNumber psn = { 0, kNoProcess };
while(1) {
// get all processes, one by one
err = GetNextProcess(&psn);
// Is there html or similar documentation for error codes?
// I know, there are headers, but I'd like something more... readable?
if(err != noErr) {
break;
}
pid_t pid;
oss = GetProcessPID(&psn, &pid);
if(oss != noErr)
continue;
CFStringRef name;
oss = CopyProcessName(&psn, &name);
if(oss != noErr)
continue;
const size_t buff_size = 256;
char buff[buff_size];
Boolean ok = CFStringGetCString(name, buff, buff_size,
kCFStringEncodingASCII);
std::cout << "name: " << buff << std::endl;
// Okay, here it begins. I'm creating an "object" representing
// application
AXUIElementRef ref = AXUIElementCreateApplication(pid);
if(!ref) {
std::cout << "ref is NULL" << std::endl;
continue;
}
AXUIElementRef top_window;
// And this one makes me go crazy. I'm currently trying
// to use uncommented parameters, but I have also tried
// others. Unfortunatelly, I can't find any usable examples
// on Apple's site - browsing through it just a week, possibly
// missing something - certainly not "search" text box
err = AXUIElementCopyAttributeValue(
ref,
// kAXFocusedApplicationAttribute,
kAXFocusedWindowAttribute,
// kAXFocusedUIElementAttribute,
// kAXTopLevelUIElementAttribute,
// not quite sure if this is properly cast. Well, it is
// since this is what is required, but maybe it should be
// (const void**)(& * top_window) ?
(const void**)(&top_window)
);
if(!err) {
std::cout << "not good, yes, not good" << std::endl;
continue;
}
// is this a proper way to check this? There is no telling
// what does attribute kAXFocusedWindowAttribute returns,
// all signs on heaven and earth make me assume, that this is
// AXUIElementRef, but here:
//
http://developer.apple.com/releasenotes/Accessibility/AssistiveAPI.html
// there is no such attribute and this is the only document
// with such information I could find.
if(top_window) {
std::cout << "this one has focus" << std::endl;
}
CFTypeID window_type_id = AXUIElementGetTypeID();
std::cout << "window_type_id = " << window_type_id << std::endl;
if(top_window)
CFRelease(top_window);
CFRelease(ref);
}
}
</code>
Can someone point me to any examples, clearer (and full...) documentation?
ANY help appreciated, code in pascal, geez, even assembler. I suppose in
future I'll have to write more applications using this API so I'll learn it
overtime, but now... Geeez... I'm really frustrated.
Oh, I found this:
http://www.monkeybreadsoftware.de/pluginhelp/example-Accessibilityserv...s-Activ
and it looks like it does exactly the thing I want it to do so I got logic
at least, but it doesn't help at all when it comes to write code in C,
except for attributes and functions names. And it doesn't help me that
much..
TIA,
JD