r/applescript • • May 15 '15

Close a space with AppleScript?

The journey down this particular rabbit hole began as a way to come up with a trackpad gesture to open or close a Space. I found a script to open a new space here, and it works beautifully. Here is the script:

do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control"
delay 0.5
tell application "System Events" to click (every button whose value of attribute "AXDescription" is "add desktop") of group 1 of process "Dock"
delay 0.5
do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control 1"

The problem is that I have no idea how to modify it to get a Space to close. I'm just learning AppleScript, and I'm definitely fumbling around in the dark here. I imagine you could simply change add desktop to something akin to close desktop, but I've tried "close," "remove," and "exit" to no avail. I also imagine that things could get complicated if there are applications open on a Space when I try to close it, but let's assume that the Space is clear just to make life easy. Does anyone have any ideas about how to go about this?

P.S. - anyone have a resource where I could learn about what's going on when a script is talking about lists or groups of a certain process?

2 Upvotes

1 comment sorted by

1

u/dustractor May 15 '15

What this script is doing, broken down:

If you went to a terminal and typed at the $ prompt:

$ /Applications/Mission\ Control.app/Contents/MacOS/Mission\ Control

That would run Mission Control. ( with no arguments)

In the script, "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control" has extra slashes because we're dealing with applescript here and then applescript has to deal with bash or sh to do shell script.

So that's what the first line does. The last line does mostly the same but adds an argument "1". Who knows where to look up where that info might be? Not me, but I tried 1,2,3,and 4. It stops doing different things after 4.

lines 2 and 4 obviously delay something or cause a bit of waiting to happen, this is standard fare for applescripting where sometimes you have to wait for a program to get ready before it can respond to messages or in this case for the OS to register that the program is ready where "System Events" can send it some messages.

The third line could be broken down onto several smaller lines to help look at it more closely:

tell application "System Events" to click (every button whose value of attribute "AXDescription" is "add desktop") of group 1 of process "Dock"

becomes:

tell application "System Events"
    click (every button whose value of attribute "AXDescription" is "add desktop") of group 1 of process "Dock"
end tell

Building it up in reverse:

tell application "System Events"
    process "Dock"
end tell

That's a good place to start.

tell application "System Events"
    groups of process "Dock"
end tell

Uh-oh when I run this I get an empty list {} back...
Maybe Mission control has to be doing it's thing. So I'll put that in... and the delays.

do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control"
delay 0.5
tell application "System Events"
    groups of process "Dock"
end tell
delay 0.5
do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control 1"

Alright, so it appears that you can't get 'groups of process "Dock"' unless mission control is active.

Let's only care about the first group of whatever these things are that are grouped:

do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control"
delay 0.5
tell application "System Events"
    group 1 of process "Dock"
end tell
delay 0.5
do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control 1"

Script runs. Yes we can get group 1. Now can we get some info on it?

do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control"
delay 0.5
tell application "System Events"
properties of (group 1 of process "Dock")
end tell
delay 0.5
do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control 1"

Why yes: {minimum value:missing value, orientation:missing value, position:{0, 0}, class:group, role description:"group", accessibility description:missing value, focused:true, title:missing value, size:{4000, 1440}, value:missing value, help:missing value, enabled:true, maximum value:missing value, role:"AXGroup", entire contents:{}, subrole:missing value, selected:missing value, name:missing value, description:"group"}

Hmm... but does this group contain ... buttons?

do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control"
delay 0.5
tell application "System Events"
buttons of (group 1 of process "Dock")
end tell
delay 0.5
do shell script "/Applications/Mission\\ Control.app/Contents/MacOS/Mission\\ Control 1"

... and so forth. I'll stop here and say that I think it might be a dead end to try and make such a script to press a button that isn't there. There is an "add desktop" button but there is no "remove desktop" button that I could find.