kasper / phoenix

A lightweight macOS window and app manager scriptable with JavaScript
https://kasper.github.io/phoenix/
Other
4.36k stars 128 forks source link

Window.recent() will not return recently focused windows if the app they belong to hangs #221

Open alexdelorenzo opened 6 years ago

alexdelorenzo commented 6 years ago

If you select an a window that belongs to a process that is hung, Window.recent() will not accurately reflect "recent" window selections.

Instead, it will return a list of windows with the foremost window result reflecting the most recently selected window belonging to a running application, even if you have a window belong to a stopped process in focus.

To reproduce:

I've included a slim Phoenix configuration to reproduce this issue. Please load the configuration and reload Phoenix for it to take effect. To emulate a hung process, please run something like

kill -STOP `pgrep -f Safari`

And then select a running app other than Safari into focus, then switch to Safari.

A modal displaying the current application that the most recently focused window belongs to will pop up.

If you ran the above command to stop a process, please run this command to change the status from stopped.

 kill -CONT `pgrep -f Safari`

Here's the Phoenix configuration:

const ALERT_APPEARANCE = 'dark';
const ALERT_DURATION = 1;
const ALERT_WEIGHT = 24;

function alert(text, icon, duration = ALERT_DURATION) {
  const frame = Screen.main().flippedFrame();

  Modal.build ({
    origin ( mFrame ) {
      return {
        x: frame.x + ( frame.width / 2 ) - ( mFrame.width / 2 ),
        y: ( frame.height / 2 ) - ( mFrame.height / 2 )
      };
    },
    weight: ALERT_WEIGHT,
    duration,
    appearance: ALERT_APPEARANCE,
    text,
    icon
  }).show();

}

Event.on('mouseDidLeftClick', () => {
    const location = Mouse.location();
    const [focused, ...windowsBehind] = Window.recent();
    const currentApp = focused.app();
    const currentPID = currentApp.processIdentifier();

    alert(`currentApp ${currentPID}`, currentApp.icon());
});
fabiospampinato commented 5 years ago

For the same reason Window.focused () returns undefined.

One can detect if the app hung because the following osascript tell application "System Events" to return name of first process whose frontmost is true always returns the name of the app.