OSX Lion AppleScript : How to get current space # from mission control?

Viewed 18185

I'm trying to figure out how to get the current space # from mission control. Source would be helpful, but more helpful would be info on how to figure this out myself. I've written a few applescripts, but more often than not it seems like any time I need to do something new (that I can't find dictionary documentation for) it falls under the category of "tell this specific app (e.g. "System Events") this very specific thing" and I've no clue how I would actually figure that out.


Specifically what I am trying to do:

I hate the new mission control in OSX 10.7. I want my spaces "grid" back since I used it all the time. I used to navigate between spaces using arrow keys (e.g. ALT+) every few seconds. Now I'm stuck with this clunky 1x9 array of spaces instead of an elegant 3x3 grid. I've re-mapped all my spaces to use the number pad, which partially takes care of the problem (since it is a 3x3 grid), but only when I have an external keyboard attached.

Basically, I want to be able to use ALT+ and again, but to do so I need to detect the current space # so that I can switch from space 5-->2, for example.

Dave's answer below, although far more detailed than I expected, requires writing an app to do this (plus it still doesn't fully answer the question). If it's at all possible, I'd rather just bind a few keys to an applescript.

8 Answers

http://switchstep.com/ReSpaceApp

This works, is free (right now) and is awesome.

Just be sure to manually create as many spaces as your layout (in preferences) is expecting.

I have come up with a workaround for this for myself in macOS Catalina, though I expect this should work for multiple macOS versions. This solution solves my problems, namely:

  1. The inability to identify which desktop contains which project, because desktops cannot be named. (I usually am splitting time on work on multiple projects at once and each desktop is dedicated to work on a different project [and they all use the same apps])
  2. The inability to programmatically(/easily) determine which desktop I'm on at any one time
  3. The lack of tools to track time spent on each desktop

I solved item 1 quite some time back using Stickies.app. I put the project name in a huge enough font that it's easily legible in the desktop thumbnails in Mission Control and I hide the stickie window behind my Dock, assigned specifically to the corresponding project's desktop. (I also duplicate the desktop name in small superscripted text that pokes out from under the left side of the dock so that I can identify the current desktop outside of mission control.)

I just solved item 2 via applescript just now. In the stickie, I add a tiny, unobtrustive font string that identifies the stickie as the desktop name, e.g. 'dtop'. E.g. "small_superscripted_name LARGE_NAME tiny_dtop_string" or "project1 PROJECT1 dtop". Note, this script assumes that the project name contains no spaces (i.e. it's just one word). You can split on a different charcter/string, if you wish. Here is the applescript that, when run, results in the desktop name:

tell application "System Events"
    --obtain the stickie with the desktop name
    set dstr to name of first item of (windows of application process "Stickies" of application "System Events" whose name contains "dtop")

    --Parse the desktop name from the stickie
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to " "
    set dname to first item of (dstr's text items)
    set AppleScript's text item delimiters to astid

    --Show the result in a dialog window
     display dialog "Desktop: " & dname
end tell

And as far as item 3 goes, I have yet to implement it, but I could easily poll via a cron job by calling the script using osascript. However, I may explore the possibility of mapping the desktop keyboard shortcuts I use to trigger the script, say, after a delay like 1 second after a control-right/left-arrow, 10 seconds after F3 or control-up-arrow. (It wouldn't catch window-drags that trigger desktop changes, but that hasn't worked anyway since I started using 2 monitors.)

Once I have that set up, I'll likely output the desktop name and a timestamp to a log so I can track time spent on each desktop.

Related