Minimizing windows of Cocoa applications
Minimizing windows of Cocoa applications
- Subject: Minimizing windows of Cocoa applications
- From: patrick machielse <email@hidden>
- Date: Fri, 5 Jan 2007 18:48:29 +0100
I'm experiencing a problem minimizing windows that belong to Cocoa
applications when using the Accessibility API.
My program should minimize the main window of the active application.
I firs get a CFTypeRef to the window in question. That works as it
should. I then try the following to minimize that window:
// THIS SHOULD WORK, BUT DOESN'T
AXError error = AXUIElementSetAttributeValue(window,
kAXMinimizedAttribute,
kCFBooleanTrue);
if ( error != kAXErrorSuccess ) {
NSLog(@"Could not minimize window");
}
This seems to work OK for some apps (Finder) while it doesn't work
for others (Xcode, Safari). For instance, when I minimize a Safari
window, _all_ windows will be minimized. Same for Xcode, with the
occasional 'ghost' of the run log window still being displayed after
minimize. In the end (and after much hair pulling) I came to the
conclusion that this may be one of those Carbon vs. Cocoa things (?).
My workaround is this: I get a reference to the window's minimize
button and press that.
// NB. It seems we can't just set the minimized attribute
// (won't work with Cocoa apps?). As a workaround we get
// the minimze button and press that...
CFTypeRef button = NULL;
AXError error = AXUIElementCopyAttributeValue(window,
kAXMinimizeButtonAttribute,
&button);
if ( error != kAXErrorSuccess ) {
NSLog(@"could not access the minimize button");
// Try the direct approach before giving up.
// We need this for freaks like iTunes. Doesn't
// seem to have adverse effects on windows that
// can't be minimized / really have no button...
error = AXUIElementSetAttributeValue(window,
kAXMinimizedAttribute,
kCFBooleanTrue);
if ( error != kAXErrorSuccess ) {
NSLog(@"could not minimize window");
}
return;
}
error = AXUIElementPerformAction (button, kAXPressAction);
if ( error != kAXErrorSuccess ) {
NSLog(@"could not press minimize button");
}
CFRelease(button);
Note that if I fail to get the minimize button, I will try to
minimize the window the old way. The reason for this is that an app
like iTunes seems to impersonate a minimize button, rather than sport
a real one.
This seems to work in my limited testing, but does not 'feel' quite
right (and it cost me the afternoon to come up with this solution).
Am I overlooking something? Is there a better way to achieve what I
want. (AppleScript isn't the answer).
patrick
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Accessibility-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden