orderWindows not reliable/changed in Ventura

My application can be controlled by scripts using AppleScript, python (through scripting bridge), or my own internal scripting language in a script thread. I just noticed that scripts that need to know the current front window have stopped working in Ventura.

I find the front window as the first relevant window in [NSApp orderedWindows] (in Objective C). I found the problem with Ventura by adding some debugging output as follows:

  1. Run a script thread to control my application

  2. When a new window is needed, this thread uses performSelectorOnMainThread to create a window and waits for it to appear and to get added to the document.

  3. Before returning from main thread selector, the ordered windows are checked and they are correct (the new window is an AncestorController).

  • DEBUG: check window order after creating an AncestorController
  • DEBUG: frontGEDCOMController windows are:
  • ...1 of 2 <NSWindow: 0x123691e20> <AncestorController: 0x600001fc50e0>
  • ...2 of 2 <NSWindow: 0x123641ad0> <IndexController: 0x6000019c0c40>
  • ...current front window is AncestorController
  1. Immediately on return to script thread, however, another performSelectorOnMainThread is called to check the order of the windows again and it has changed in an instant to the wrong order (wrong because the AncestorController is still in front).
  • DEBUG: recheck window order after 0.000000 seconds
  • DEBUG: frontGEDCOMController windows are:
  • ...1 of 2 <NSWindow: 0x123641ad0> <IndexController: 0x6000019c0c40>
  • ...2 of 2 <NSWindow: 0x123691e20> <AncestorController: 0x600001fc50e0>
  • ...front window type is IndexController
  1. But, if I sleep the thread, the order changes back to correct order in <= 1/30 second
  • DEBUG: recheck window order after 0.033333 seconds
  • DEBUG: frontGEDCOMController windows are:
  • ...1 of 2 <NSWindow: 0x123691e20> <AncestorController: 0x600001fc50e0>
  • ...2 of 2 <NSWindow: 0x123641ad0> <IndexController: 0x6000019c0c40>
  • ... front window type is AncestorController

This issue is new to Ventura. For some reason Ventura adds a window to the list in the correct position. It then movies to the wrong possible for a short amount of time before then returning to the correct position. That did not happen in MacOS 12.x or earlier. Now maybe I can find all the places my scripts that need to pause for Ventura to get things right, but it would be better to know why it has happened and then know where I need to insert Ventura hacks? Or is there another way that can reliably get ordered windows in Ventura?

Curiously this problem does not occur when scripting by AppleScript. Maybe that is just so slow that pauses happen all the time anyway.

I did find an alternative method to get ordered windows introduced in 10.12:

 enumerateWindowsWithOptions:usingBlock:

with option

 NSWindowListOrderedFrontToBack

but it is identical to using orderedWindows (i.e., gets the order wrong for a while).

orderWindows not reliable/changed in Ventura
 
 
Q