Getting back to the OP question about switching to the previous desktop space.
@_jims's macros are very useful and seem to be the current state of the art for KM Desktop switching. But as @_jims will tell you, you have to switch windows using his macros for the system to keep track of where you are so that "previous" in the system matches what you think it ought to be.
Unfortunately, there are many ways to change Desktop Spaces, intentionally and unintentionally, without Jim's system, and in those cases, his Previous macro or menu choice won't be what you want it to be. For a simple example, you could be in Acrobat Reader and click on a link inside the PDF. Your default browser will open up a new tab in the most recently used browser window, which may or may not be in that same Desktop Space, and without jumping through hoops before you click on that link, you cannot tell. Suddenly finding yourself in another Desktop Space, of course you will want to return to where you were, and Jim's Previous macro will not be of any help, and if you don't happen to remember, then you have to hunt for it.
That's why I asked the question in the first place. seven months ago, and why, even though Jim's macros are very useful, I haven't market this issue as Solved.
I've come to the conclusion that any working system will require two pieces of information:
- The ID of the current desktop, recorded somewhere to be the "previous" location after a move, recorded at every move.
- A "trigger" to notify you when anything, user, browser, Finder, Terminal process, or whatever, changes the current Desktop Space.
There's good progress on the first of those above, from @_jims, and over in the thread How to test for a Space (Mission Control)?.
Ideal for the second would be a Keyboard Maestro trigger that the Desktop Space has changed, as I asked about in the OP. We don't have that (yet), but it sure would be nice, wouldn't it?
What if there was a way to create a "virtual" trigger? I think I have one, but I'm not the programmer (yet) to implement it.
I was recently given an "Amulet of Revealing" by the developer of the CurrentKey app. He said:
My swift code listens to this specific MacOS api, here is the line:
NSWorkspace.shared.notificationCenter.addObserver(forName:
NSWorkspace.activeSpaceDidChangeNotification,
object: nil, queue: nil, using:
<the function you want to handle this event> )
I'm not a Swift programmer. That's the first line of Swift code I've actually had an interest in using. I don't even know if I added linebreaks to the above in the correct syntax. What I did would not work for AppleScript.
So I need some help from the Swift programmers on this forum. I'm tagging @CJK, @ccstone, @DanThomas, @ComplexPoint, @MitchellModel, @peternlewis, and @_jims, as folks who I remember participating on this topic in various threads. My apologies to anyone I forgot and to anyone I tagged who isn't interested.
Please help me correct my misunderstandings:
The first part ends in .addObserver
so that says to me that this line only needs to be run once and an Observer
will be created that hangs around. Is that right?
Does the Observer terminate when the current code stops running, so the code has to stay open, or what? Is that Observer permanent until reboot? I really don't know how this works.
When the Observer is running, it's watching for this notification called NSWorkspace.activeSpaceDidChangeNotification
to turn true
. When that happens, because of what we're doing here there's no "object" involved (whatever that would be) and no "queue" involved (again, whatever that is), but it will use the function name that I provide to it, "to handle this event".
Can that function be an AppleScript function, or an AppleScript function inside a Swift wrapper, so that the AppleScript function can call a KM function?
If that Observer can indirectly call a KM macro as the "handler", then it seems that's getting most of the way there to having a virtual KM trigger of the Desktop Space being changed.
Am I right? What am I not understanding? What needs to be done to make this work so that KM can start this Observer, if it's not already running, and then get a macro triggered every time anything changes the current Desktop Space?
Thanks!