3

Is there a way to get current desktop/space number on osx programatically?

I've found this questions but it seems to be a little bit outdated: OSX Lion AppleScript : How to get current space # from mission control?

I've also found this: http://www.hammerspoon.org/docs/hs.spaces.watcher.html but unfortunately it does not work.

Community
  • 1
  • 1
trzeciakp
  • 109
  • 1
  • 9

3 Answers3

2

Since recent OS version, it is no longer possible to access via script to spaces. the only thing I found is how to get, via Applescript the name/path of desktop picture on current space :

tell application "Finder" to set A to (desktop picture) as string

Except that, since Yosemite (at least !) Applescript dictionary no longer contain space scriptable elements.

pbell
  • 2,965
  • 1
  • 16
  • 13
  • Thanks for explanation. I find it useful. I replaced wallpapers on all desktops with filenames containing desktop number and your script works well on one screen. However it does not support multiple external screens. It always gives name of wallpaper on main screen. Do you know how to fix it? – trzeciakp Jan 11 '16 at 10:20
  • No, and I have single 27' monitor, so I can't even test & found. sorry. – pbell Jan 17 '16 at 16:26
1

In Yosemite the following oneliner (built on this answer) works:

killall Dock \
&& sleep 0.1 \
&& defaults export com.apple.spaces - \
| plutil -convert json - -o - \
| jq '.SpacesDisplayConfiguration."Management Data".Monitors[0] | ."Current Space".uuid as $current_desktop_uuid | [.Spaces[].uuid]|index($current_desktop_uuid)'

(Requires jq version 1.5 for whitespace-containing key access, installable with e.g. Homebrew.)

On my Desktop 3 this oneliner will output:

$ killall Dock && sleep 0.1 && defaults export com.apple.spaces - | plutil -convert json - -o - | jq '.SpacesDisplayConfiguration."Management Data".Monitors[0] | ."Current Space".uuid as $current_desktop_uuid | [.Spaces[].uuid]|index($current_desktop_uuid)'
3

Rationale: as described in this answer, the desktops and their contents are stored in ~/Library/Preferences/com.apple.spaces.plist. However, this file isn't automatically updated when changing the desktop. So, to get the current desktop number we

  • enforce the desktop property list update with killall Dock. It takes a moment for the data to be stored to the disk so
  • wait for 0.1 seconds (enough on my machine) for the file to be written onto disk.
  • Then export the updated desktop property list using defaults as XML,
  • use plutil to convert the XML to JSON (a personal preference)
  • and extract with jq the current space uuid and get its index in the space uuid list. Since the space uuid list contains Mac OS X Dashboard, the resulting index is exactly the number of the current desktop.

Note: This snippet did get the desktop number of my primary monitor on Yosemite – my colleague tested this on El Capitan with a different setup with no luck. But assuming that killall Dock still works, the JSON output should still contain the relevant information.

Community
  • 1
  • 1
7mp
  • 119
  • 5
1

I have been using a Gihub utility called WhichSpace on Mojave and Catalina. There have been no reports that I've seen of it not working on later versions as well, so I presume it does. https://github.com/gechr/WhichSpace

When running, it places a small numerical icon in the upper toolbar. It does a good job of updating that number reliably, no matter what kind of process causes the change of Desktop Workspace. It also does not seem to care about the psuedo-limit of 16 Desktops. I currently have 45 Desktops. The icon size might not allow acceptable results when the number is over 99, I have not tested it.

I access the number using AppleScript.

tell application ¬
    "System Events" to tell process ¬
    "WhichSpace" to set temp to (title of menu bar items of menu bar 1)
return item 1 of temp

I got the tip to the WhichSpace utility and the AppleScript snippet from the Keyboard Maestro Forum. https://forum.keyboardmaestro.com/t/macros-desktop-spaces-macros-for-navigation-and-window-management-v1-1-superseded/21507/11

I hope this helps.

August
  • 343
  • 3
  • 10