├── .npmrc ├── .gitattributes ├── .gitignore ├── index.test-d.ts ├── .editorconfig ├── index.d.ts ├── index.js ├── .github └── workflows │ └── main.yml ├── test.js ├── readme.md ├── package.json └── license /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import isJxa from './index.js'; 3 | 4 | expectType(isJxa); 5 | -------------------------------------------------------------------------------- /.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.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Check if your code is running in a [JXA](https://github.com/JXA-Cookbook/JXA-Cookbook) environment. 3 | 4 | @example 5 | ``` 6 | import isJxa from 'is-jxa'; 7 | 8 | if (isJxa) { 9 | // For example, polyfill something here when it's in a JXA environment 10 | } 11 | ``` 12 | */ 13 | declare const isJxa: boolean; 14 | 15 | export default isJxa; 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-undef */ 2 | 3 | const isJxa = (() => { 4 | try { 5 | return typeof $ === 'function' 6 | && typeof Application === 'function' 7 | && typeof Application.currentApplication === 'function' 8 | && typeof ObjC === 'object' 9 | && typeof ObjC.import === 'function'; 10 | } catch { 11 | return false; 12 | } 13 | })(); 14 | 15 | export default isJxa; 16 | -------------------------------------------------------------------------------- /.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 | - 16 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - run: npm install 20 | - run: npm test 21 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'node:fs'; 2 | import test from 'ava'; 3 | import execa from 'execa'; 4 | import isJxa from './index.js'; 5 | 6 | test('returns false on Node.js', t => { 7 | t.false(isJxa); 8 | }); 9 | 10 | // TODO: Enable when JXA supports ESM. 11 | test.failing('return true on JXA', async t => { 12 | const script = ` 13 | const module = {}; 14 | ${fs.readFileSync('index.js', 'utf8')} 15 | console.log(module.exports); 16 | `; 17 | 18 | const {stdout} = await execa('osascript', ['-l', 'JavaScript', '-e', script]); 19 | 20 | t.is(stdout, 'true'); 21 | }); 22 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-jxa 2 | 3 | > Check if your code is running in a [JXA](https://github.com/JXA-Cookbook/JXA-Cookbook) environment 4 | 5 | *JXA is JavaScript for Automation on macOS.* 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install is-jxa 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | import isJxa from 'is-jxa'; 17 | 18 | if (isJxa) { 19 | // For example, polyfill something here when it's in a JXA environment 20 | } 21 | ``` 22 | 23 | *You need to transpile the code for an JXA environment as it does not support ESM.* 24 | 25 | ## Related 26 | 27 | - [run-jxa](https://github.com/sindresorhus/run-jxa) - Run JXA code and get the result 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-jxa", 3 | "version": "3.0.0", 4 | "description": "Check if your code is running in a JXA environment", 5 | "license": "MIT", 6 | "repository": "sindresorhus/is-jxa", 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": "./index.js", 15 | "engines": { 16 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "jxa", 27 | "macos", 28 | "is", 29 | "detect", 30 | "check", 31 | "process", 32 | "environment", 33 | "automation", 34 | "mac", 35 | "osascript" 36 | ], 37 | "devDependencies": { 38 | "ava": "^3.15.0", 39 | "execa": "^5.1.1", 40 | "tsd": "^0.17.0", 41 | "xo": "^0.44.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------