8

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
3
  • The green button has never meant maximize, at least in the sense of maximizing a window in Windows, and has always been kind of hit-or-miss as to what it actually does. Recently, though, it's changed function to operate as "switch to full screen". That is the function of the button, so your script is operating correctly for what you are telling it to do.
    – tubedogg
    Mar 5, 2015 at 5:04
  • By "maximize", I meant "maximize" in the Mac sense, i.e. enlarging a window. I know that the function of the green button has changed in Yosemite. However, the behavior of the green button depends on whether or not Option is pressed (eg. Safari). With Option pressed, the button behaves in the "classic" way (enlarging a window rather than a full screen). For example, see this [link]( osxdaily.com/2014/10/28/maximize-zoom-windows-os-x-mac).
    – T_T
    Mar 5, 2015 at 8:01
  • I was just looking into the same thing and decided I would create a keyboard shortcut to activate the "Zoom" (since my automator recording was different for the same green button in different apps). Would a keyboard shortcut satisfy your ultimate goal or are you doing something more sophisticated?
    – hepcat72
    Jun 11, 2020 at 15:58

2 Answers 2

8

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
6
  • Hi, thanx a lot. I*d never heard of a "perform action" command before. Where did you get your "inspiration"?!? Dec 18, 2015 at 23:21
  • @clensam lang, In the AppleScript dictionary of "System Events" : look at Processes Suite --> the perform command, and the UI element--> contains actions, attributes, browsers, and many more.
    – jackjr300
    Dec 19, 2015 at 20:28
  • When I told Script-Editor to display all actions of Green button, it said: . . . {action "AXPress" of button 2 of window "No title" of application process "Script Editor" of application "System Events", action "AXZoomWindow" of button 2 of window "No title" of application process "Script Editor" of application "System Events"} . . . But Accessibility Inspector always listed "accessibilityPerformPress" under "Actions", no matter whether "option" was pressed: WHERE can one learn the "functions" a button contains? Dec 21, 2015 at 5:55
  • Use the old "UIElementinspector" application, download this archive --> https://developer.apple.com/library/mac/#samplecode/UIElementInspector/UIElementInspector.zip This archive contains the source code and the application.
    – jackjr300
    Dec 23, 2015 at 20:41
  • I couldn't get your link to work (for me). Accessibility Inspector did just what I described 2 posts above. What DID get a result was: tell application "System Events" to actions of (first button whose subrole is "AXFullScreenButton") of (first window whose subrole is "AXStandardWindow") of (first process whose frontmost is true). THAT one returned action "AXPress" of button 2 of […], action "AXZoomWindow" of button 2 […]... though. Dec 29, 2015 at 22:08
1

Just in case you use an app that does NOT react to AXZoomWindow in a standard way (like BBEdit) or you happen to have different "favourite" settings for windows of different apps (I like Mail less wide than Safari), I wrote a script that maximises an app's frontmost window and RESETS it to your individual preferences.

Here are some issues that I met and had to solve:
1.) "frontmost process" when called in a script is "System Events" or "ScriptEditor" – set visible to 0.
     (Does NOT apply to Karabiner shortcuts.)
2.) Max. values of desktop are defined by size of scroll area 1 of process "Finder"
3.) Menu bar's height on my system is 22px so I had to subtract them in various places
4.) I combined values for position & size in a mutual variable per app – Replace with your own !
5.) In order to only address apps you want to "Zoom" there is "maxMin" to exclude others –
     add or delete apps as you like
6.) Of course you'll need to register the script/app with "Assistive Access" (Security/System Prefs)
7.) Either shortcut this script as a "Service" (via Automator) in Sys-Prefs, Keyboard, shortcuts, …
8.) … or use the ingenious key-remapper Karabiner Elements with a simple "Complex Modification":

"from": {"key_code": "m", "modifiers": {"mandatory": ["left_option"]} },
  "to": [{"shell_command": "osascript ~/.config/myScpts/maxMin_zoom.app"}], **
"type": "basic"  
≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈≈
(** path and name: your choice)    

which delivers a suitable shortcut on the spot – I settled for "Opt-M" as most "Z" combos are in use.
Here's the script (DELETE lines 2 & 3 for Karabiner):

tell application "System Events"
    set visible of first process whose frontmost is 1 to 0
    delay 0.2
    set dTopSize to size of scroll area 1 of process "Finder" as list
    set maxPos to {0, 22}
    set realApp to first process whose frontmost is true
    set froAppName to (name of first process whose frontmost is true) as string
    set maxMin to true

    if (froAppName is "BBEdit") then
        set appBounds to {0, 22, 888, (item 2 of dTopSize) - 22}
    else if (froAppName is "Safari") then
        set appBounds to {38, 22, 1372, (item 2 of dTopSize) - 22}
    else if (froAppName is "Mail") then
        set appBounds to {53, 22, 1314, (item 2 of dTopSize) - 22}
    else
        set maxMin to false
    end if
    if maxMin then
        if item 1 of (size of window 1 of realApp as list) is not equal to ¬
        item 1 of dTopSize then
            set position of window 1 of realApp to maxPos
            set size of window 1 of realApp to {(item 1 of dTopSize), ¬
            (item 2 of dTopSize) - 22}
        else
            set size of window 1 of realApp to {item 3 of appBounds, ¬
            item 4 of appBounds}
            set position of window 1 of realApp to {item 1 of appBounds, ¬
            item 2 of appBounds}
        end if
    end if
    tell realApp to set frontmost to true
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .