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

https://github.com/lujjjh/node-selection

Get current selected text by using system accessibility APIs
https://github.com/lujjjh/node-selection

accessibility macos nodejs windows

Last synced: 29 days ago
JSON representation

Get current selected text by using system accessibility APIs

Awesome Lists containing this project

README

        

# node-selection

> Get current selected text by using system accessibility APIs

## Installation

```sh
npm i node-selection
```

## Usage

### Accessibility permissions

macOS requires accessibility permissions to be granted before a program
can control the Mac by using accessibility features.

#### `checkAccessibilityPermissions([options])`

- `options`: ``
- `prompt`: `` **Default:** `false`

Returns: `` Fullfills upon success with a boolean indicating
whether accessibility permissions have been granted to this program.

If `prompt` is `true`, a prompt window will be shown when accessibility
permissions have not been granted.

If this method is invoked on non-macOS platform, it always returns `true`.

```js
import { checkAccessibilityPermissions } from 'node-selection';

if (!(await checkAccessibilityPermissions({ prompt: true }))) {
console.log('grant accessibility permissions and restart this program');
process.exit(1);
}
```

### Selection

#### `getSelection()`

Returns: `` Fullfills upon success with an object with one property:

- `text`: `` | `` Current selected text.
- `process`: `Object` | ``
- `pid`: `` | `` The process ID.
- `name`: `` | `` The filename of the process.
- `bundleIdentifier`: `` | `` The bundle identifier of the process (macOS only).

```js
import { getSelection } from 'node-selection';

try {
const { text, process } = await getSelection();
console.log('current selection:', { text, process });
} catch (error) {
// no valid selection
console.error('error', error);
}
```

## Examples

- [Node](example/node-example)
- [Electron](example/electron-example)