views:

45

answers:

1

I've been trying to get any given window from another Application to become front most and focused.

Here are the two methods I have tried:

AppleScript

tell application "Safari"
   set index of window "Downloads" to 1
   activate
end tell

Why it doesn't work: While it seems to change the z-Order, it doesn't change the focus! The previous frontmost window, even if obfuscated, retains focus.

CoreGraphicServices

CGSWindowOrder(cid, wid, kCGSOrderAbove, nil)

Why it doesn't work: Without being the Universal Owner, you can't change the order of a window owned by another process. In this case, making myself the Universal Owner is not an option -- I don't want to depose the Dock.

Recap

I need to make a window frontmost and focused from one process to another. AppleScript fails to focus, and CGS isn't an option since I don't own the window.

Any solutions/ideas?

A: 

Try using the Accessibility API to send an AXPress action to the AXWindow. Obviously, the user will need to have access for assistive devices turned on.

Peter Hosey
Is there any easy to way create an AXWindow using a CGSWindow handle/id?
Edward
Edward: Nope. The only way to get an AXWindow is to find the window by title among the AXApplication's windows. This also means that if two windows have the same title, you're screwed.
Peter Hosey