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
- Host: GitHub
- URL: https://github.com/lujjjh/node-selection
- Owner: lujjjh
- Created: 2022-03-31T11:32:54.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-16T16:03:31.000Z (about 1 year ago)
- Last Synced: 2025-04-28T15:53:11.375Z (29 days ago)
- Topics: accessibility, macos, nodejs, windows
- Language: C++
- Homepage:
- Size: 1.05 MB
- Stars: 17
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
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)