Thanks @griffman Rob, thanks @_jims Jim,
I'm unfamiliar with Semaphores and Dictionaries, so it's going to take me a while to trace out how those sample macros work. First, what's going on that both macros are trying to Lock the same Semaphore? Neither is reading it? OK, I looked at examples in the Wiki and I think I get it. I haven't found yet if there's a way to determine if a semaphore lock is currently active. It just queues things, right?
As for the context of what I'm trying to do, I tried to describe it in the OP. But it was pretty wordy.
What I want starts with a list of Spaces visited, a pushdown stack so the top list element is always the Current Space and the second element is the Previous Space. I have that working. After a Space Change trigger, a macro updates a pushdown list with the name of the current Space.
Right now I mostly use the top two list elements in macros. I also generate a Prompt With List of the Space names in chronological order, which helps when go down a rabbit hole and ask "What was I working on?"
So far, so good.
When I make a manual change by hotkey, it works by opening the little ID window that I've created in each Space. Thus that lD window becomes frontmost. I've been trying to figure out (for a while) how to restore the previously frontmost app window. When my ID windows is frontmost, the one I want will be second, but there seems to be no easy way of finding out what that is just from the existing view. I can find out what the previously active app was, but that may not be in the current Space, and for any app I can find out what the order of its windows is. But the general case, which is frequent, appears to be difficult. Of course the OS knows, but it doesn't make it public.
And it's a bit of a pain point for me. Over and over, I switch into a different Space and I think the window I'm looking at is frontmost so I'll paste the text or image that I went elsewhere to grab, and it ends up in the ID window instead of the window that I intend. And sometimes I don't notice because the ID window is tiny. And then when I do notice, cleaning it up is an interruption.
So I'm working on a different approach. Instead of trying to figure out what the order of windows of different apps is on the screen, after I've moved to a new Space, I'm looking at keeping a separate pushdown stack list for each Space, by adding an entry (and removing any older, duplicate entries) to that stack, essentially at every Focused Window change. Because I'll be saving an entry for every Focused Window change, I need to keep the process very brief.
It appears to only take a few milliseconds to update that stack, and what it buys me is that when I leave a Space, the frontmost app of that space is already in its app list. So when I return to that Space, I can just look in its app list and know which app window I should now make frontmost.
There's a wide variety of ways that the Space can be changed, my hotkeys, Mission Control, apps returning to a previously active window from their Recent Files list, and more. In many of those ways, the frontmost app is already the appropriate one, either because it's the app that initiated the Space and Window change or because Mission Control automatically makes the appropriate app window frontmost.
With my own hotkeys, I'm in control of picking the new window after the Space change. But when the Space is changing by any means other than my hotkey macros, there's this timing issue. An app or Mission Control or whatever is bringing some app widow in the new Space to be frontmost and it's also changing the Space at the same time. An app is presumably picking the window first and the Space automatically follows, while Mission Control is picking the Space first the former frontmost app window automatically follows.
Normally, without a Space change, a Focused Window Trigger macro can save the new window name in the app list for the current space, ready to be identified as the frontmost again if I leave this Space while it's still frontmost, and then sometime later return to this Space.
Because I will be updating the current Space's app list at every Focused Window change, I really cannot afford to wait — every time — more than a fraction of a second to see if there might be a Space change happening at the same time. I don't think.
But I do need to know if a Space change is happening to know which Space's app list to update. Simultaneous Space and Focused Window changes means the new window name belongs in the new Space's list. If there is no Space change going on at the same time, then the new app window name goes in the current Space's list.
So maybe I need to figure out how to handle it in each of the various cases. Maybe semaphores are the answer, I don't know yet. In my current thinking, I think I would need each of the two macros to be able to determine whether or not there was a semaphore already locked and after waiting for the other macro to complete, then have different behavior according to whether it is running first or second. Is that possible?
Sorry, that also turned out to be wordy, but it's a different set of words than before. Maybe I'm getting the concept across. And maybe I'm homing in on how to use semaphores.
Thanks for your efforts in trying to figure out what I'm trying to describe.