如何将 applescript 写入关闭桌面 space?

How to write applescript to that closes a desktop space?

下面是一个脚本,用于 1) 单击 'add desktop' 按钮 2) 单击最后添加的 space 转到它

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

不过,我也在尝试编写一个脚本来关闭 space。我无法找到要单击的 button/attribute。 Accessibility inspector 不能给我我需要的东西,所以需要一些帮助

macOS Catalina下测试,以下示例 AppleScript 代码 帮我关闭 desktop/space 显示在 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

备注:

示例 AppleScript 代码 在使用单个显示器的系统上测试的内容。具有多个显示器的系统,如果 Mission Control 偏好设置 Displays 有单独的 Spaces 检查,则可能需要额外的编码。

上面的 example AppleScript code 实际上可能不会删除最后一个桌面。如果 Mission Control 中显示的最后一个 按钮 全屏视图 应用程序 window,然后 perform action "AXRemoveDesktop" of button countSpaces 全屏视图 应用程序 window 淘汰全屏视图

如果这对您来说是个问题,那么要解决这个问题并实际关闭最后一个 Desktop,然后使用以下 example AppleScript code 作为 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

注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要或需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5,适当设置延迟

这是我使用 window 平铺管理器 yabai 做出的解决方案。

需要安装 yabaijq

将其保存到文件中,例如 close_current_space.sh:

#!/bin/bash

index=$(yabai -m query --spaces --space | jq .index)

osascript -e \
  "tell application \"System Events\"
    do shell script quoted form of \"/System/Applications/Mission Control.app/Contents/MacOS/Mission Control\"
    delay 0.3
    perform action \"AXRemoveDesktop\" of button $index of list 1 of group \"Spaces Bar\" of group 1 of group \"Mission Control\" of process \"Dock\"
    delay 0.5
    do shell script quoted form of \"/System/Applications/Mission Control.app/Contents/MacOS/Mission Control\"
  end tell"

然后运行终端中的那个会关闭当前的active space.

我还对其进行了调整以处理多个显示,如下所示:

#!/bin/bash

display=$(yabai -m query --displays --display | jq .index)
spaceIndex=$(yabai -m query --spaces --space | jq .index)
actualIndex=$(($(yabai -m query --displays --display | jq ".spaces | to_entries | .[] | select(.value == $spaceIndex) | .key")+1))

osascript -e \
  "tell application \"System Events\"
    do shell script quoted form of \"/System/Applications/Mission Control.app/Contents/MacOS/Mission Control\"
    delay 0.3
    perform action \"AXRemoveDesktop\" of button $actualIndex of list 1 of group \"Spaces Bar\" of group $display of group \"Mission Control\" of process \"Dock\"
    delay 0.5
    do shell script quoted form of \"/System/Applications/Mission Control.app/Contents/MacOS/Mission Control\"
  end tell"

我已经使用 skhd 将该脚本绑定到击键并且效果很好。

干杯!