Edward
Edward

Reputation: 548

Making a another applications's window frontmost and focused

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?

Upvotes: 2

Views: 892

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96393

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.

Upvotes: 1

Related Questions