├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── security.md └── workflows │ └── main.yml ├── .editorconfig ├── index.test-d.ts ├── package.json ├── license ├── index.js ├── test.js ├── readme.md └── index.d.ts /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import {runAppleScript, runAppleScriptSync} from './index.js'; 3 | 4 | expectType>(runAppleScript('return "unicorn"')); 5 | expectType(runAppleScriptSync('return "unicorn"')); 6 | expectType>(runAppleScript('return {"unicorn"}', {humanReadableOutput: false})); 7 | expectType(runAppleScriptSync('return {"unicorn"}', {humanReadableOutput: false})); 8 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: macos-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "run-applescript", 3 | "version": "7.1.0", 4 | "description": "Run AppleScript and get the result", 5 | "license": "MIT", 6 | "repository": "sindresorhus/run-applescript", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsd" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "macos", 31 | "mac", 32 | "applescript", 33 | "osascript", 34 | "run", 35 | "execute" 36 | ], 37 | "devDependencies": { 38 | "ava": "^6.0.1", 39 | "tsd": "^0.30.0", 40 | "xo": "^0.56.0" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import process from 'node:process'; 2 | import {promisify} from 'node:util'; 3 | import {execFile, execFileSync} from 'node:child_process'; 4 | 5 | const execFileAsync = promisify(execFile); 6 | 7 | export async function runAppleScript(script, {humanReadableOutput = true, signal} = {}) { 8 | if (process.platform !== 'darwin') { 9 | throw new Error('macOS only'); 10 | } 11 | 12 | const outputArguments = humanReadableOutput ? [] : ['-ss']; 13 | 14 | const execOptions = {}; 15 | if (signal) { 16 | execOptions.signal = signal; 17 | } 18 | 19 | const {stdout} = await execFileAsync('osascript', ['-e', script, outputArguments], execOptions); 20 | return stdout.trim(); 21 | } 22 | 23 | export function runAppleScriptSync(script, {humanReadableOutput = true} = {}) { 24 | if (process.platform !== 'darwin') { 25 | throw new Error('macOS only'); 26 | } 27 | 28 | const outputArguments = humanReadableOutput ? [] : ['-ss']; 29 | 30 | const stdout = execFileSync('osascript', ['-e', script, ...outputArguments], { 31 | encoding: 'utf8', 32 | stdio: ['ignore', 'pipe', 'ignore'], 33 | timeout: 500, 34 | }); 35 | 36 | return stdout.trim(); 37 | } 38 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {runAppleScript, runAppleScriptSync} from './index.js'; 3 | 4 | test('async', async t => { 5 | t.is(await runAppleScript('return "unicorn"'), 'unicorn'); 6 | }); 7 | 8 | test('sync', t => { 9 | t.is(runAppleScriptSync('return "unicorn"'), 'unicorn'); 10 | }); 11 | 12 | test('async - non-human readable output', async t => { 13 | t.is(await runAppleScript('return "unicorn"', {humanReadableOutput: false}), '"unicorn"'); 14 | }); 15 | 16 | test('sync - non-human readable output', t => { 17 | t.is(runAppleScriptSync('return "unicorn"', {humanReadableOutput: false}), '"unicorn"'); 18 | }); 19 | 20 | test('async - non-human readable output (arrays)', async t => { 21 | t.is(await runAppleScript('return {"unicorn"}', {humanReadableOutput: false}), '{"unicorn"}'); 22 | }); 23 | 24 | test('sync - non-human readable output (arrays)', t => { 25 | t.is(runAppleScriptSync('return {"unicorn"}', {humanReadableOutput: false}), '{"unicorn"}'); 26 | }); 27 | 28 | test('async - abort with AbortController', async t => { 29 | const abortController = new AbortController(); 30 | 31 | setTimeout(() => { 32 | abortController.abort(); 33 | }, 100); 34 | 35 | await t.throwsAsync( 36 | runAppleScript('delay 2\nreturn "completed"', {signal: abortController.signal}), 37 | {name: 'AbortError'}, 38 | ); 39 | }); 40 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # run-applescript 2 | 3 | > Run AppleScript and get the result 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install run-applescript 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import {runAppleScript} from 'run-applescript'; 15 | 16 | const result = await runAppleScript('return "unicorn"'); 17 | 18 | console.log(result); 19 | //=> 'unicorn' 20 | ``` 21 | 22 | ## API 23 | 24 | ### runAppleScript(script, options?) 25 | 26 | Returns a `Promise` with the script result. 27 | 28 | #### script 29 | 30 | Type: `string` 31 | 32 | The script to run. 33 | 34 | #### options 35 | 36 | Type: `object` 37 | 38 | ##### humanReadableOutput 39 | 40 | Type: `boolean`\ 41 | Default: `true` 42 | 43 | Change the output style. 44 | 45 | When `false`, returns the value in a [recompilable source form](https://ss64.com/osx/osascript.html). 46 | 47 | ##### signal 48 | 49 | Type: `AbortSignal` 50 | 51 | An AbortSignal that can be used to cancel the AppleScript execution. 52 | 53 | ### runAppleScriptSync(script, options?) 54 | 55 | Returns a `string` with the script result. 56 | 57 | #### script 58 | 59 | Type: `string` 60 | 61 | The script to run. 62 | 63 | #### options 64 | 65 | Type: `object` 66 | 67 | ##### humanReadableOutput 68 | 69 | Type: `boolean`\ 70 | Default: `true` 71 | 72 | Change the output style. 73 | 74 | When `false`, returns the value in a [recompilable source form](https://ss64.com/osx/osascript.html). 75 | 76 | ## Related 77 | 78 | - [run-jxa](https://github.com/sindresorhus/run-jxa) - Run JXA code and get the result 79 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type Options = { 2 | /** 3 | Change the output style. 4 | 5 | When `false`, returns the value in a [recompilable source form](https://ss64.com/osx/osascript.html). 6 | 7 | @default true 8 | 9 | @example 10 | ``` 11 | import {runAppleScript} from 'run-applescript'; 12 | 13 | const result = await runAppleScript('return "unicorn"', {humanReadableOutput: false}); 14 | 15 | console.log(result); 16 | //=> '"unicorn"' 17 | ``` 18 | */ 19 | readonly humanReadableOutput?: boolean; 20 | 21 | /** 22 | An AbortSignal that can be used to cancel the AppleScript execution. 23 | 24 | Only supported by the async function. 25 | */ 26 | readonly signal?: AbortSignal; 27 | }; 28 | 29 | /** 30 | Run AppleScript asynchronously. 31 | 32 | @param script - The script to run. 33 | @returns The script result. 34 | 35 | @example 36 | ``` 37 | import {runAppleScript} from 'run-applescript'; 38 | 39 | const result = await runAppleScript('return "unicorn"'); 40 | 41 | console.log(result); 42 | //=> 'unicorn' 43 | ``` 44 | */ 45 | export function runAppleScript( 46 | script: string, 47 | options?: Options 48 | ): Promise; 49 | 50 | /** 51 | Run AppleScript synchronously. 52 | 53 | @param script - The script to run. 54 | @returns The script result. 55 | 56 | @example 57 | ``` 58 | import {runAppleScriptSync} from 'run-applescript'; 59 | 60 | const result = runAppleScriptSync('return "unicorn"'); 61 | 62 | console.log(result); 63 | //=> 'unicorn' 64 | ``` 65 | */ 66 | export function runAppleScriptSync(script: string, options?: Options): string; 67 | --------------------------------------------------------------------------------