├── .editorconfig ├── .gitattributes ├── .github ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.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 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /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 | /** 23 | Run AppleScript asynchronously. 24 | 25 | @param script - The script to run. 26 | @returns The script result. 27 | 28 | @example 29 | ``` 30 | import {runAppleScript} from 'run-applescript'; 31 | 32 | const result = await runAppleScript('return "unicorn"'); 33 | 34 | console.log(result); 35 | //=> 'unicorn' 36 | ``` 37 | */ 38 | export function runAppleScript( 39 | script: string, 40 | options?: Options 41 | ): Promise; 42 | 43 | /** 44 | Run AppleScript synchronously. 45 | 46 | @param script - The script to run. 47 | @returns The script result. 48 | 49 | @example 50 | ``` 51 | import {runAppleScriptSync} from 'run-applescript'; 52 | 53 | const result = runAppleScriptSync('return "unicorn"'); 54 | 55 | console.log(result); 56 | //=> 'unicorn' 57 | ``` 58 | */ 59 | export function runAppleScriptSync(script: string, options?: Options): string; 60 | -------------------------------------------------------------------------------- /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} = {}) { 8 | if (process.platform !== 'darwin') { 9 | throw new Error('macOS only'); 10 | } 11 | 12 | const outputArguments = humanReadableOutput ? [] : ['-ss']; 13 | 14 | const {stdout} = await execFileAsync('osascript', ['-e', script, outputArguments]); 15 | return stdout.trim(); 16 | } 17 | 18 | export function runAppleScriptSync(script, {humanReadableOutput = true} = {}) { 19 | if (process.platform !== 'darwin') { 20 | throw new Error('macOS only'); 21 | } 22 | 23 | const outputArguments = humanReadableOutput ? [] : ['-ss']; 24 | 25 | const stdout = execFileSync('osascript', ['-e', script, ...outputArguments], { 26 | encoding: 'utf8', 27 | stdio: ['ignore', 'pipe', 'ignore'], 28 | timeout: 500, 29 | }); 30 | 31 | return stdout.trim(); 32 | } 33 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "run-applescript", 3 | "version": "7.0.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 | -------------------------------------------------------------------------------- /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 | ### runAppleScriptSync(script, options?) 48 | 49 | Returns a `string` with the script result. 50 | 51 | #### script 52 | 53 | Type: `string` 54 | 55 | The script to run. 56 | 57 | #### options 58 | 59 | Type: `object` 60 | 61 | ##### humanReadableOutput 62 | 63 | Type: `boolean`\ 64 | Default: `true` 65 | 66 | Change the output style. 67 | 68 | When `false`, returns the value in a [recompilable source form](https://ss64.com/osx/osascript.html). 69 | 70 | ## Related 71 | 72 | - [run-jxa](https://github.com/sindresorhus/run-jxa) - Run JXA code and get the result 73 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------