You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用AppleScript切换Spaces自动排列设置及查看窗口组件

Toggle Spaces Auto-Arrangement via AppleScript & Inspect UI Components

First, to answer your question about viewing the window's component structure: use Apple's Accessibility Inspector—it’s the go-to tool for exploring UI elements when writing UI scripting code like this.

How to Use Accessibility Inspector to Inspect UI Elements

  • First, install Xcode (from the App Store) if you haven’t already. Accessibility Inspector is included with Xcode.
  • Open Accessibility Inspector: You can find it in /Applications/Xcode.app/Contents/Applications/Accessibility Inspector.app, or search for it via Spotlight.
  • Click the crosshair/target icon in the Inspector window, then drag it over the "Automatically rearrange Spaces based on most recent use" checkbox in Mission Control settings.
  • The Inspector will show you the full hierarchy of UI elements leading to the checkbox (e.g., System Preferences process → Mission Control window → Scroll Area → Table → Row → Checkbox). This lets you confirm the exact labels and indices needed for your script.

Working AppleScript to Toggle the Checkbox

This script uses UI scripting to navigate to the checkbox and toggle its state. It’s designed to be reliable across macOS versions by targeting the row via its text label instead of a fixed index:

-- Activate System Preferences and navigate to Mission Control pane
tell application "System Preferences"
    activate
    set current pane to pane id "com.apple.preference.expose"
    delay 0.5 -- Give the pane time to load fully
end tell

-- Use UI scripting to toggle the checkbox
tell application "System Events"
    tell process "System Preferences"
        tell window "Mission Control"
            tell table 1 of scroll area 1
                -- Find the row containing the target setting
                set targetRow to first row where value of static text 1 is "Automatically rearrange Spaces based on most recent use"
                -- Toggle the checkbox
                click checkbox 1 of targetRow
            end tell
        end tell
    end tell
end tell

-- Optional: Close System Preferences after toggling
quit application "System Preferences"

Important Notes

  • Accessibility Permissions: Before running the script, you need to grant accessibility access to the app running the script (e.g., Script Editor, Automator, or your compiled app). Go to System Preferences > Security & Privacy > Privacy > Accessibility, then add the app to the list and check the box next to it.
  • Delay Adjustment: If the script fails occasionally, increase the delay 0.5 value to give System Preferences more time to load the Mission Control pane.
  • macOS Version Differences: While targeting the row by text label is more reliable, if the checkbox's position or label changes in a future macOS update, use Accessibility Inspector to recheck the element hierarchy and adjust the script accordingly.

内容的提问来源于stack exchange,提问作者Alex Stedman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:47:45