Close windows before opening a modal dialog: InitializePsychSound or PsychPortAudio

I’d like to share a simple trick for solving an annoying problem of a hang up where the software is displaying an urgent dialog or error message behind the user’s full-screen window, leaving the user unaware and unable to respond. For me it arose developing precursors to MacDisplaySettings which use AppleScript to access System preferences. The issue is that it occasionally needs to put up a dialog or throw a fatal error, but the user might have used Screen to put up a full screen window. Thus the program displays its dialog or error BEHIND the user’s screen. The hapless user sits there, waiting potentially forever, unaware of the error message.

The solution is that whenever you throw a fatal error or a dialog box that demands a response, FIRST check for any open windows, and if so, call sca (Screen Close All). This guarantees that your error message or dialog won’t be hidden. I’ve pasted below the trivial routine CloseWindows I call whenever I need to be sure the user will see what’s displayed.

In particular this would be a great enhancement for ** InitializePsychSound or PsychPortAudio **:

Under macOS, MATLAB requires the user’s permission to use the microphone. The user grants permission by going to System Preferences : Security & Privacy : Privacy and clicking the checkbox next to “MATLAB”.


Note that each time we upgrade MATLAB, the user will again have to grant permission to the new MATLAB application. If we wait for speech while MATLAB does not yet have permission, then either InitializePsychSound or PsychPortAudio puts up a modal dialog asking the user to grant permission. Alas, if this comes up while we have a full-screen window from Screen, then the dialog is displayed behind our window and cannot be seen. MATLAB waits forever for the user to click the invisible dialog box.

The fix would be for InitializePsychSound or PsychPortAudio to call CloseWindows before putting up a dialog window or throwing an error.
Best
Denis

function CloseWindows
if exist(‘PsychtoolboxVersion’,‘file’) && ~isempty(Screen(‘Windows’))
% Close any user windows to make sure that our error message can be
% seen.
sca
end
end