node-osascript is a utility library for Node.js that enables the execution of AppleScript code directly from JavaScript. It facilitates bidirectional communication by allowing JavaScript variables to be injected into AppleScript and by transforming AppleScript results (lists, records, dates, numbers, booleans, strings) into their corresponding JavaScript data types using PEG.js. The current stable version is 2.1.0, last published over 8 years ago. While the release cadence is not explicitly stated, the project appears to be in a maintenance or abandoned phase given its age. Its key differentiator is the automatic, structured type conversion of AppleScript results into native JavaScript objects, simplifying data handling compared to raw `exec` calls, which typically return plain strings.
npm install node-osascriptVerified import paths — ran on the pinned version, not inferred.
Demonstrates executing AppleScript with dynamically injected JavaScript variables, handling asynchronous results, and basic error trapping. This showcases the core `execute` method and variable passing.
Ensure your application environment is macOS. For cross-platform desktop applications, consider alternatives like Electron's `shell.openExternal` or native OS APIs for basic tasks.
For older Node.js or strict ESM environments, use `const osascript = require('node-osascript');` or investigate dynamic `import()` or `createRequire` for explicit CJS interop.Always sanitize or escape any user-provided data before incorporating it into AppleScript strings. Prefer passing data via the `variables` object where possible, as it typically handles escaping.
Wrap `osascript.execute` and `osascript.executeFile` in a `new Promise()` to enable `async/await` usage, as demonstrated in the quickstart example. Consider newer alternatives like `@7c/osascript` for native Promise support.
Implement robust timeout logic around your `osascript.execute` calls, using `Promise.race` with a `setTimeout` for `async/await` patterns. Be aware that killing the process might leave applications in an undesirable state.
Ensure your application is running on a macOS environment. Verify that `/usr/bin/osascript` exists and is accessible, and that `/usr/bin` is included in your system's PATH. If on a server, confirm macOS is the OS.
Use `const osascript = require('node-osascript');` (CommonJS) or `import osascript from 'node-osascript';` (ESM) and then call `osascript.execute(...)`.Carefully review your AppleScript for syntax errors. Test the script directly in Script Editor on macOS to debug any issues before incorporating it into your Node.js code. Ensure applications targeted by the script are installed and running.
No dependency data recorded yet.