2

I saw Using applescript to create spaces and launch apps, but the provided AppleScript didn't work.

I did some reading to understand what was happening, and how they selected the right UI element, in case that was all that had changed. Thanks to How to know the name of UI elements using Accessibility inspector (or any other tool), I can understand how one would compose a script like this, but it feels too hacky that the best way to add a new space is via affecting a user clicking.

I want to be able to create or delete spaces with AppleScript. Is there a better way than using AppleScript to click GUI buttons?

1

1 Answer 1

0

In a word, "no".

If you launch Script Editor, then File → Open Dictionary and look at the list of applications there, you'll see that neither "Mission Control" nor "Dock" - the two applications most clearly responsible for spaces - show up. This means that neither one has a scripting dictionary, which suggests there is no way to automate the creation of spaces. "System Events", the catch-all for system stuff, similarly has no terminology for "spaces" available.

Digging a bit deeper into API documentation, you can read a bit of interesting information about spaces from NSWorkspace, but none of that lets you do anything to spaces, only see their existing configuration. This is borne out by the available functionality in applications like Spaceman, which all let you view your Spaces, but not switch between them or create new ones.

It seems clear that Apple doesn't want anyone programmatically monkeying with Spaces. Sorry!

3
  • Thanks for digging into this. I didn't think to look at the NS (NeXTSTEP) functions that Apple exposes to us. I don't have the proper mental model of what's going on, but I'm real curious... I see that there's a "Create NSWorkspace configuration" object - developer.apple.com/documentation/appkit/… - but I don't see how it can be used! NSWorkspace is not 1:1 with spaces, but I can't see anything that is. (Yet) Jul 12, 2021 at 20:35
  • NSWorkspaceConfiguration is a bit confusingly named in this context, it's not a "configuration for a workspace", it's like, the configuration that a workspace uses to launch an app. That's just the thing that sets obscure options for automation launching apps like "don't bother prompting the user if they haven't authorized this code signature yet, just silently fail" or "create a duplicate application instance" or "launch in the background, don't take focus".
    – Glyph
    Jul 13, 2021 at 21:12
  • 1
    And yes, NSWorkspace is so named for the "workspace manager", which was sort of like NeXTSTEP's equivalent of the Finder and the Dock; it was "the thing that launches applications" which is why application enumeration and launching APIs are on it. It does have notifications related to spaces, like NSWorkspaceActiveSpaceDidChangeNotification, but again, it's all read-only, it's not designed to be modified or configured by users or by applications.
    – Glyph
    Jul 13, 2021 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .