MacOS – Applescript to click green (zoom) button with Option down

applescriptmacos

Based on Applescript – the activate command makes application "half-active", I wrote the following Applescript which would allow me to click the green (zoom) button at the top-left corner of an window. I want to apply the script to MacVim and others which have the green button as "AXFullScreenButton". But, running the script makes the window to FULL SCREEN rather than a "maximized" window. Can anyone tell what is wrong?

tell application "System Events"
    key down option
    click (first button whose subrole is "AXFullScreenButton") of ¬ 
            (first window whose subrole is "AXStandardWindow") of ¬
            (first process whose frontmost is true)
    key up option
    set frontApp to (name of first application process whose ¬
            frontmost is true) as string
end tell
tell application frontApp to activate

Best Answer

You can use one of the actions of the "AXFullScreenButton":

  • "AXZoomWindow" action to maximize.
  • "AXPress" action to full screen.

Use the perform action command, like this.

tell application "System Events"
    perform action "AXZoomWindow" of (first button whose subrole is "AXFullScreenButton") of (first window whose subrole is "AXStandardWindow") of (first process whose frontmost is true)
end tell