How to create/destroy a new macOS space via script?

I found a new love on window management called Amethyst. With it, came a new appreciation for macOS spaces as well and now I’m using them more and more.

However, I can’t find a way to reliably open a new space via some scripting language that won’t require me disabling SIP, such as installing yabai.

I found this approach bellow, but since it relies on UI Scripting and the AXDescription of a button, I guess that couldn’t be properly replicated on a non-English macOS install such as mine (at least, I couldn’t get it to work on my end).

do shell script "open -a 'Mission Control'"
delay 0.5
tell application "System Events" to ¬
    click (every button whose value of ¬
        attribute "AXDescription" is "add desktop") of ¬
        group 2 of group 1 of group 1 of process "Dock"
delay 0.5
tell application "System Events" to key code 53

So I guess I could either:

  1. Fix this for my Brazilian Portuguese install by identifying the AXDescription of that button, but I have no idea how to find out this property.
  2. Find another way/app to tie to an Keyboard Maestro Macro.
1 Like

Further googling made me find this, which is currently working fine on Monterey.

do shell script "open -b 'com.apple.exposelauncher'"
delay 0.5
tell application id "com.apple.systemevents"
	tell (every application process ¬
		whose bundle identifier = "com.apple.dock") to ¬
		click (button 1 of group 2 of group 1 of group 1)
	delay 0.5
	key code 53 -- esc key
end tell
2 Likes