MacOS – Applescript – the activate command makes application “half-active”

alfredapplescriptmacos

What I did:

Using this link,

I created the following Applescript:

tell application "System Events"
    click (first button whose subrole is "AXZoomButton") of (first window whose subrole is "AXStandardWindow") of (first process whose frontmost is true)
    set frontApp to (name of first application process whose frontmost is true) as string
    tell application frontApp to activate
end tell

This code allows me to "press" the green button at the top left corner of the window.

I run the code, using Alfred.

(Mountain Lion 10.8.4, Alfred 2.0.6)

What happens:

Zooming works fine, but

  1. the zoomed window of the application loses focus.
  2. the three buttons, red, yellow and green, at the top-left corner of the window are all dimmed in grey after running the code.
  3. the menu bar still shows the application title, indicating that the application is still foremost.

(In this sense, the window is "half-activated"?)

If I click the window, those buttons go back to normal, showing red, yellow and green.

What I want:

I want to keep focus on the zoomed application window after running the code using Alfred.

Can anyone help me out, please?

Best Answer

Move the activate command outside the tell application "System Events" block:

tell application "System Events" to tell (process 1 where frontmost is true)
    click (button 1 where subrole is "AXZoomButton") of window 1
end tell
activate application (path to frontmost application as text)

Or you could probably just remove the activate command completely.