├── .npmrc ├── .gitattributes ├── .gitignore ├── main ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── index.test-d.ts ├── .editorconfig ├── main.m ├── test.js ├── readme.md ├── license ├── index.d.ts ├── index.js └── package.json /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/app-path/HEAD/main -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/app-path 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import appPath from './index.js'; 3 | 4 | expectType>(appPath('Safari')); 5 | expectType(appPath.sync('Safari')); 6 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.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 | - 14 15 | - 12 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /main.m: -------------------------------------------------------------------------------- 1 | @import AppKit; 2 | 3 | int main() { 4 | @autoreleasepool { 5 | NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; 6 | NSArray *args = [NSProcessInfo processInfo].arguments; 7 | 8 | if (args.count == 1) { 9 | return 1; 10 | } 11 | 12 | NSString *ret; 13 | NSString *app = args[1]; 14 | 15 | if ([app rangeOfString:@"."].location != NSNotFound) { 16 | ret = [workspace absolutePathForAppBundleWithIdentifier:app]; 17 | } else { 18 | ret = [workspace fullPathForApplication:app]; 19 | } 20 | 21 | if (!ret) { 22 | return 2; 23 | } 24 | 25 | puts(ret.UTF8String); 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import appPath from './index.js'; 3 | 4 | test('async - name', async t => { 5 | t.is(await appPath('Safari'), '/Applications/Safari.app'); 6 | }); 7 | 8 | test('async - bundle id', async t => { 9 | t.is(await appPath('com.apple.Safari'), '/Applications/Safari.app'); 10 | }); 11 | 12 | test('sync - name', t => { 13 | t.is(appPath.sync('Safari'), '/Applications/Safari.app'); 14 | }); 15 | 16 | test('sync - bundle id', t => { 17 | t.is(appPath.sync('com.apple.Safari'), '/Applications/Safari.app'); 18 | }); 19 | 20 | test('throws when app couldn\'t be found', async t => { 21 | await t.throwsAsync(appPath('fooAppBarBaz'), {message: 'Couldn\'t find the app'}); 22 | 23 | t.throws(() => { 24 | appPath.sync('fooAppBarBaz'); 25 | }, { 26 | message: 'Couldn\'t find the app' 27 | }); 28 | }); 29 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # app-path 2 | 3 | > Get the path to an app *(macOS)* 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install app-path 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import appPath from 'app-path'; 15 | 16 | console.log(await appPath('Safari')); 17 | //=> '/Applications/Safari.app' 18 | 19 | console.log(await appPath('com.apple.Safari')); 20 | //=> '/Applications/Safari.app' 21 | 22 | console.log(appPath.sync('Safari')); 23 | //=> '/Applications/Safari.app' 24 | ``` 25 | 26 | ## API 27 | 28 | ### appPath(appName) 29 | 30 | Returns a `Promise` with the path to the app specified in `appName`. Rejects when run on any other operating system than macOS. 31 | 32 | ### appPath.sync(appName) 33 | 34 | Returns the path to the app specified in `appName`. Throws when run on any other operating system than macOS. 35 | 36 | #### appName 37 | 38 | Type: `string` 39 | 40 | An app name or bundle identifier. 41 | 42 | ## Related 43 | 44 | - [app-path-cli](https://github.com/sindresorhus/app-path-cli) - CLI for this module 45 | -------------------------------------------------------------------------------- /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.d.ts: -------------------------------------------------------------------------------- 1 | declare const appPath: { 2 | /** 3 | Get the path to an app on macOS. 4 | 5 | @param appName - An app name or bundle identifier. 6 | @returns The path to the app specified in `appName`. Rejects when run on any other operating system than macOS. 7 | 8 | @example 9 | ``` 10 | import appPath from 'app-path'; 11 | 12 | console.log(await appPath('Safari')); 13 | //=> '/Applications/Safari.app' 14 | 15 | console.log(await appPath('com.apple.Safari')); 16 | //=> '/Applications/Safari.app' 17 | ``` 18 | */ 19 | (appName: string): Promise; 20 | 21 | /** 22 | Synchronously get the path to an app on macOS. 23 | 24 | @param appName - An app name or bundle identifier. 25 | @returns The path to the app specified in `appName`. Throws when run on any other operating system than macOS. 26 | 27 | @example 28 | ``` 29 | import appPath from 'app-path'; 30 | 31 | console.log(appPath.sync('Safari')); 32 | //=> '/Applications/Safari.app' 33 | 34 | console.log(appPath.sync('com.apple.Safari')); 35 | //=> '/Applications/Safari.app' 36 | ``` 37 | */ 38 | sync(appName: string): string; 39 | }; 40 | 41 | export default appPath; 42 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import {fileURLToPath} from 'node:url'; 2 | import path from 'node:path'; 3 | import execa from 'execa'; 4 | 5 | const dirname = path.dirname(fileURLToPath(import.meta.url)); 6 | 7 | const improveError = error => { 8 | if (error.exitCode === 2) { 9 | error.message = 'Couldn\'t find the app'; 10 | } 11 | 12 | return error; 13 | }; 14 | 15 | export default async function appPath(appName) { 16 | if (process.platform !== 'darwin') { 17 | throw new Error('macOS only'); 18 | } 19 | 20 | if (typeof appName !== 'string') { 21 | throw new TypeError('Please supply an app name or bundle identifier'); 22 | } 23 | 24 | try { 25 | const {stdout} = await execa('./main', [appName], {cwd: dirname}); 26 | return stdout; 27 | } catch (error) { 28 | throw improveError(error); 29 | } 30 | } 31 | 32 | appPath.sync = appName => { 33 | if (process.platform !== 'darwin') { 34 | throw new Error('macOS only'); 35 | } 36 | 37 | if (typeof appName !== 'string') { 38 | throw new TypeError('Please supply an app name or bundle identifier'); 39 | } 40 | 41 | try { 42 | return execa.sync('./main', [appName], {cwd: dirname}).stdout; 43 | } catch (error) { 44 | throw improveError(error); 45 | } 46 | }; 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "app-path", 3 | "version": "4.0.0", 4 | "description": "Get the path to an app (macOS)", 5 | "license": "MIT", 6 | "repository": "sindresorhus/app-path", 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" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd", 20 | "compile-x64": "clang main.m -fmodules -mmacosx-version-min=10.6 -target x86_64-apple-darwin -o main-x64", 21 | "compile-arm64": "clang main.m -fmodules -mmacosx-version-min=11.0 -target arm64-apple-darwin -o main-arm64", 22 | "compile": "npm run compile-x64 && npm run compile-arm64 && lipo -create -output main main-x64 main-arm64 && rm main-x64 main-arm64" 23 | }, 24 | "files": [ 25 | "index.js", 26 | "index.d.ts", 27 | "main" 28 | ], 29 | "keywords": [ 30 | "macos", 31 | "applescript", 32 | "app", 33 | "application", 34 | "path", 35 | "directory", 36 | "bundle", 37 | "location" 38 | ], 39 | "dependencies": { 40 | "execa": "^5.0.0" 41 | }, 42 | "devDependencies": { 43 | "ava": "^3.15.0", 44 | "tsd": "^0.14.0", 45 | "xo": "^0.39.1" 46 | } 47 | } 48 | --------------------------------------------------------------------------------