Get current selected text by using system accessibility APIs.
This project is forked from node-selection
What is New
- Enlarge the text selection range, no 256 characters limit.
- Support text selection in more scenarios such as eletcron, vscode, cursor, etc. You just need to run the program with administrator privileges.
- npm
npm install @hollway/node-selection- yarn
yarn add @hollway/node-selectionmacOS requires accessibility permissions to be granted before a program can control the Mac by using accessibility features.
options:<Object>prompt:<boolean>Default:false
Returns: <Promise> 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.
import { checkAccessibilityPermissions } from '@hollway/node-selection';
if (!(await checkAccessibilityPermissions({ prompt: true }))) {
console.log('grant accessibility permissions and restart this program');
process.exit(1);
}Returns: <Promise> Fullfills upon success with an object with one property:
text:<string>|<undefined>Current selected text.process:Object|<undefined>pid:<number>|<undefined>The process ID.name:<string>|<undefined>The filename of the process.bundleIdentifier:<string>|<undefined>The bundle identifier of the process (macOS only).
import { getSelection } from '@hollway/node-selection';
try {
const { text, process } = await getSelection();
console.log('current selection:', { text, process });
} catch (error) {
// no valid selection
console.error('error', error);
}- For windows, open
Visual Studio Installerand clickModifyto install the C++ Desktop Development Kit: v143 C++ ATL(x86/x64)
C++ code is under src/, if you changed the code, use the following command to rebuild.
npm run build