Search code examples
applescript

How to write applescript to that closes a desktop space?


The below is a script to 1) click the 'add desktop' button 2) click on the last space added to go to it

on run
    my openSpace()
end run

on openSpace()
    tell application "System Events"
        tell application "Mission Control" to launch
        tell group 2 of group 1 of group 1 of process "Dock"
            click (every button whose value of attribute "AXDescription" is "add desktop")
            tell list 1
                set countSpaces to count of buttons
                delay 0.5
                click button (countSpaces)
            end tell
        end tell
    end tell
end openSpace

on closeSpace()
    
end closeSpace

However, I'm also trying to write a script to close a space. I'm having trouble finding which button/attribute to click. Accessibility inspector can't give me what I need, so in need of some help


Solution

  • Testing under macOS Catalina, the following example AppleScript code works for me to close the last desktop/space showing in Mission Control:

    my closeSpace()
    
    on closeSpace()
        tell application "Mission Control" to launch
        delay 1
        tell application "System Events"
            tell list 1 of group 2 of group 1 of group 1 of process "Dock"
                set countSpaces to count of buttons
                if countSpaces is greater than 1 then
                    perform action "AXRemoveDesktop" of button countSpaces
                end if
            end tell
            delay 0.25
            key code 53 --  # Esc key on US English Keyboard
        end tell
    end closeSpace
    

    Notes:

    The example AppleScript code what tested on a system using a single monitor. Systems with multiple monitors and if Mission Control preferences has Displays have separate Spaces checked, then additional coding may be necessary.

    The example AppleScript code above, as coded, may not actually remove the last Desktop. If the last button showing in Mission Control is a Full Screen View application window, then the perform action "AXRemoveDesktop" of button countSpaces just knocks the Full Screen View application window out of Full Screen View.

    If this is an issue for you, then to work around this and actually close the last Desktop, then use the following example AppleScript code as the closeSpace() handler:

    on closeSpace()
        tell application "Mission Control" to launch
        delay 1
        tell application "System Events"
            tell list 1 of group 2 of group 1 of group 1 of process "Dock"
                set buttonNames to the name of UI elements
                set buttonNames to the reverse of buttonNames
                set i to the length of buttonNames
                repeat with buttonName in buttonNames
                    if contents of buttonName is equal to ("Desktop " & i as text) then
                        if i is greater than 1 then
                            perform action "AXRemoveDesktop" of button i
                            exit repeat
                        end if
                    else
                        set i to i - 1
                    end if
                end repeat
            end tell
            delay 0.25
            key code 53 --  # Esc key on US English Keyboard
        end tell
    end closeSpace
    

    Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.