Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/karaggeorge/mac-focus-window

Focus a window and bring it to the front on macOS
https://github.com/karaggeorge/mac-focus-window

Last synced: 21 days ago
JSON representation

Focus a window and bring it to the front on macOS

Awesome Lists containing this project

README

        

# mac-focus-window

> Focus a window and bring it to the front on macOS

Requires [accessibility permissions](https://www.applegazette.com/mac/what-are-accessibility-permissions-all-about/).

Requires macOS 10.12 or later. macOS 10.13 or earlier needs to download the [Swift runtime support libraries](https://support.apple.com/kb/DL1998).

## Install

```
$ npm install mac-focus-window
```

## Usage

```js
const focusWindow = require('mac-focus-window');

if (focusWindow.isSupported && focusWindow.hasPermissions()) {
focusWindow(12345);

// true
} else {
focusWindow.requestPermissions();
}
```

## API

### `focusWindow(windowNumber: number | string): Boolean`

Focus the given window and bring it to the front.

An error will be thrown if:
- Accessibility permissions are not enabled for the current app
- You provide `windowNumber` as a string, but it's not a number
- There is no window with the given number

Returns `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:**
- The permissions dialog will only be shown if the app doesn't have permissions
- The user has to go into the System Preferences to give access, so if the app doesn't have permissions, this will return `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)

## Related

- [mac-window-select](https://github.com/karaggeorge/mac-window-select) - Prompt the user to select a window on macOS, mimicking the native screenshot utility
- [mac-windows](https://github.com/karaggeorge/mac-windows) - Provide Information about Application Windows running

## License

MIT