How to get the list of windows, in order of recent usage

I know this from document:

CGWindowListCopyWindowInfo returns windows in the front-to-back order only when CGWindowListOption.optionOnScreenOnly is specified. In this case, however, only windows in the current space are returned.


Using objective-C I want to get the list of windows on the macOS, in order of recent usage.

Is there a way to sort/get the windows in the desired order?

How do you define usage ? Is it when the window comes to front ? When it is edited ?


The way I would do it would be:

- create a dictionary, with the window reference as key and date as value

- update this date when the window becomes key and front

- when you need it, create an array from dictionary and sort it.

Window is used, when the window comes to front.

I want to get the access order of all windows like cmd-tab, including the records before my app starts.

So, you could save the aforementioned array in userDefaults.

Thank you very much for your reply, I am learning to develop macos software, so I am not very familiar with this aspect.

If I understand correctly, I have another confusion. How do I record and update the aforementioned array values when my software is not running?

Well, I thought you wanted to keep info only on your app windows.


If your app is not running, you cannot have control on windows that are not from your app.

Otherwise, have a look at this:

https://stackoverflow.com/questions/47480873/set-the-size-and-position-of-all-windows-on-the-screen-in-swift

You would need to have a user agent running when the user logs in to watch for window event ordering changes and record them, then via XPC transmit that information to your application. I'm not sure that you can find out about windows not in your process, though since Mac OS 9 days. I'd have to look deeper. You can use NSWorkspace to watch for applications switching in and out of the foreground position.

How to get the list of windows, in order of recent usage
 
 
Q