Focus a window and bring it to the front on macOS
Focus a window and bring it to the front on macOS
Requires accessibility permissions.
Requires macOS 10.12 or later. macOS 10.13 or earlier needs to download the Swift runtime support libraries.
$ npm install mac-focus-window
const focusWindow = require('mac-focus-window');
if (focusWindow.isSupported && focusWindow.hasPermissions()) {
focusWindow(12345);
// true
} else {
focusWindow.requestPermissions();
}
focusWindow(windowNumber: number | string): Boolean
Focus the given window and bring it to the front.
An error will be thrown if:
windowNumber
as a string, but it's not a numberReturns true
if the window was focused successfully, and false
otherwise.
focusWindow.isSupported
Will be true
if the module is supported (based on macOS version).
focusWindow.hasPermissions(): Boolean
Check if the current app has accessibility permissions. This will not prompt the user with the system dialog.
Returns true
if the app has permissions, and false
otherwise.
focusWindow.checkPermissions(): Boolean
Same as hasPermissions
, but it will present the user with the native permissions dialog.
Notes:
false
immediately after presenting the dialog. You have to check again after the user has granted the permissions for this to be true
(restart of the app not required)MIT