Switching to other applications (or windows) in the current Desktop space

I want to be able to <Opt><Tab> to the other applications (or windows) in the current Desktop space. I currently have a macro to this (see below for a macro connected with my default browser Arc) but the macro & apps are hardcoded for each app. I want a universal macro that will work for any space. Is this possible?

Appreciate any guidance

Mitch

While KM's Activate action, and its other application-related actions, require that you address a specific application at a time, AppleScript doesn't have that limitation, and you can access all running app names using AppleScript. I'll see if I can give it a shot at solving this. No guarantees.

But perhaps you are overlooking something. There is a similar (but not identical) feature that you are asking for in the built-in KM macro group called "Switcher Group". As long as you didn't disable it, it has a macro that lets you choose from running applications using CTRL-TAB along with the arrow keys (vertical and horizontal) to do something quite similar. It also has four our five things that make it behave slightly differently. Try it out. There is a similar shortcut called CMD-TAB which also does something similar, and has even more options.

Screenshot 2024-06-08 at 03.36.14

Appreciate your thoughts.

I checked out the Window & Application Switcher but I'm specifically looking to only address Apps in the the current Desktop. I have quite a few apps open at any one time and put related apps in the same space. If I switch to the space where I run a particular class of app (eg. messenging) then I want the ability to easily & quickly move between the apps that are currently open (via <Opt><Tab>).

My Applescript isn't strong & I don't know how to connect Applescript outputs with KM inputs. Appreciate any help you can provide.

Thanks

Mitch

PS. I used to use Witch which does this but realised that KM could do a similar job without loading up another app. My current "hard-wired" solution has been mostly ok but I would prefer a more universal solution if possible.

I've been thinking about it for the last few hours, but what does the above phrase mean? Are you talking about apps that are in one particular "Space"? Do you use Apple Spaces inside Mission Control? If so, I may be out of my depth, and I don't know if Apple even provides the necessary API to distinguish between the different spaces.

I believe it has been said a few times in this forum that no such API is available. For instance:

"Space" is, of course, an ambiguous search term if you want to look for more!

Even the versatile application/window switcher Witch is limited in what it can do with Spaces. I am sure that @griffman would be able to tell us more!

Yea; it's basically been said before (in this thread and in others), but there's no API that Apple provides developers for switching across Spaces. It is possible, but it's slow and clunky and often breaks, as you have to rely on AppleScript and a couple of other APIs that we can use.

But this example is simpler, because it's switching within the current Desktop space, and that's easy to do by default—and mitchmeister states he used to use Witch to do just that, but would like to do so using only KM.

It's quite easy to get a list of all running apps from AppleScript—I modified the bit I found there to list only visible applications:

tell application "System Events"
	get the name of every process whose background only is false and visible is true
end tell

But the problem is (as far as I know) is that there's no way to tell which Space an app is on from anything you can get back from AppleScript, unless you get the Accessibility API involved, and I think also possibly the Core Graphics API, i.e. write a full-on application.

Someone asked a similar question on StackOverflow and was told basically the same thing:

Spaces never did have much documentation, Applescript compatibility or API to speak of, & as no-one's done anything to improve it since the whole world bought laptops & started running everything fullscreen, I don't see as that's going to change.

I honestly don't know how you'd pull this off in Keyboard Maestro. Sorry :(.

-rob.

Sometimes these kinds of problems can be resolved by examining a complete list of all running processes (with "ps -ax") and inferring some conditions by comparing the lists. I've tried in this case. I've seen a slight difference, but I don't see enough information to help me.

Apple keeps the Spaces data incredibly well hidden, and doesn't expose it in any method or tool I've ever heard about, other than their own, of course.

Perhaps you could do it by looking at the location of every window. I didn't know how to write such a script, so I asked ChatGPT, and it returned this working code:

set windowInfoList to {}

tell application "System Events"
	set appList to application processes
	repeat with appProc in appList
		set appName to name of appProc
		set winList to windows of appProc
		repeat with aWin in winList
			set winPosition to position of aWin
			set {winLeft, winTop} to winPosition
			set end of windowInfoList to {appName, winLeft, winTop}
		end repeat
	end repeat
end tell

return windowInfoList

In brief testing, it did not return entries for windows on other Spaces, which makes sense, as AppleScript commands aren't Spaces-aware. But there are issues you'd have to overcome: It returns windows that aren't really associated with an app ("Electron" and some menu bar commands, in my case), it's slow (several seconds to run), and you'd have to process the list to extract the actual app names into a usable list without duplicates.

But maybe it's enough to get you started? The slowness aspect would be the biggest challenge to overcome, I think—you don't want to wait six seconds to switch apps.

-rob.

It takes 33 seconds on an M3 iMac.

The time would be based on the number of open windows, I think—I only had about a dozen open when I tested.

-rob.

I really don’t get this concept. Years ago, when I worked at Google, I had a 40 inch monitor and I could have an extremely cluttered desktop and still get work done just by pushing windows aside. At home I had a large desk and plugged two external monitors into my laptop and I could work the same way.

But when I retired I gave up the large desk and external monitors and am now working exclusively on my laptop. Using Mission Control Spaces has become more important than ever. I currently have about 30 spaces in active use, out of 45 that I have available. I couldn’t get any real work done without a large number of Spaces and a functional Spaces manager, which I am having to build myself (with lots of help from people here).

Just to correct the quoting, I didn't write that bit, I just quoted someone on StackExchange :).

Personally, even in retirement, I will always have multiple big displays. I never use Spaces, and doubt I ever will, because I like have everything close at hand and not having to remember where it is :).

Some people rely on Spaces, others do not, and it's nice that (for once), Apple gave us a choice.

-rob.

Thanks for much @Airy, @kevinb, @griffman & @August for your generous contribution :pray:t2:. Looks like I'll just have to stick with my current solution.

1 Like

If you are still wanting a Spaces-based solution and are interested in my home-brewed Spaces manager, let me know.

It's core has been pretty stable for me for many months and right now I'm working on creating and tuning various UI features and functions. I'm imagining publishing it in some form; my current idea is an ebook that first describes what you can put togther for yourself without KBM and then shows how much functionality you can by using KBM (or maybe fold the two threads together, I'm not sure yet).

The KBM macros will be available here on the Forum. I want it to be mostly DIY because I don't want the responsibility of maintaining software that I sell. I would only be selling the How-To book and if you're clever and read the KBM macros and the discussion threads, you probably can do it without the handholding of the book. However it goes, I will need "guinea pigs" to try out whether or not my instructions can be followed.

I had an "Isn't it sweet" moment with it this morning. I had to reboot. As soon as the reboot process was complete (with some apps rebooting into the Space they had been, some rebooting into Desktop 1, and many ending up on the KBM Desktop) I could start moving windows using KMB hotkeys to get everything back where it belonged. For me, the miracle was not having to set up my desktop names before I could do anything else. The names and hotkeys for 30+ are (finally) just there, working as they "should".