1 |
3a515b92
|
cagy
|
# opn
|
2 |
|
|
|
3 |
|
|
> A better [node-open](https://github.com/pwnall/node-open). Opens stuff like websites, files, executables. Cross-platform.
|
4 |
|
|
|
5 |
|
|
If need this for Electron, use [`shell.openItem()`](https://electronjs.org/docs/api/shell#shellopenitemfullpath) instead.
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
#### Why?
|
9 |
|
|
|
10 |
|
|
- Actively maintained
|
11 |
|
|
- Supports app arguments
|
12 |
|
|
- Safer as it uses `spawn` instead of `exec`
|
13 |
|
|
- Fixes most of the open `node-open` issues
|
14 |
|
|
- Includes the latest [`xdg-open` script](http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
## Install
|
18 |
|
|
|
19 |
|
|
```
|
20 |
|
|
$ npm install opn
|
21 |
|
|
```
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
## Usage
|
25 |
|
|
|
26 |
|
|
```js
|
27 |
|
|
const opn = require('opn');
|
28 |
|
|
|
29 |
|
|
// Opens the image in the default image viewer
|
30 |
|
|
opn('unicorn.png').then(() => {
|
31 |
|
|
// image viewer closed
|
32 |
|
|
});
|
33 |
|
|
|
34 |
|
|
// Opens the url in the default browser
|
35 |
|
|
opn('http://sindresorhus.com');
|
36 |
|
|
|
37 |
|
|
// Specify the app to open in
|
38 |
|
|
opn('http://sindresorhus.com', {app: 'firefox'});
|
39 |
|
|
|
40 |
|
|
// Specify app arguments
|
41 |
|
|
opn('http://sindresorhus.com', {app: ['google chrome', '--incognito']});
|
42 |
|
|
```
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
## API
|
46 |
|
|
|
47 |
|
|
Uses the command `open` on macOS, `start` on Windows and `xdg-open` on other platforms.
|
48 |
|
|
|
49 |
|
|
### opn(target, [options])
|
50 |
|
|
|
51 |
|
|
Returns a promise for the [spawned child process](https://nodejs.org/api/child_process.html#child_process_class_childprocess). You would normally not need to use this for anything, but it can be useful if you'd like to attach custom event listeners or perform other operations directly on the spawned process.
|
52 |
|
|
|
53 |
|
|
#### target
|
54 |
|
|
|
55 |
|
|
Type: `string`
|
56 |
|
|
|
57 |
|
|
The thing you want to open. Can be a URL, file, or executable.
|
58 |
|
|
|
59 |
|
|
Opens in the default app for the file type. For example, URLs opens in your default browser.
|
60 |
|
|
|
61 |
|
|
#### options
|
62 |
|
|
|
63 |
|
|
Type: `Object`
|
64 |
|
|
|
65 |
|
|
##### wait
|
66 |
|
|
|
67 |
|
|
Type: `boolean`<br>
|
68 |
|
|
Default: `true`
|
69 |
|
|
|
70 |
|
|
Wait for the opened app to exit before fulfilling the promise. If `false` it's fulfilled immediately when opening the app.
|
71 |
|
|
|
72 |
|
|
On Windows you have to explicitly specify an app for it to be able to wait.
|
73 |
|
|
|
74 |
|
|
##### app
|
75 |
|
|
|
76 |
|
|
Type: `string` `Array`
|
77 |
|
|
|
78 |
|
|
Specify the app to open the `target` with, or an array with the app and app arguments.
|
79 |
|
|
|
80 |
|
|
The app name is platform dependent. Don't hard code it in reusable modules. For example, Chrome is `google chrome` on macOS, `google-chrome` on Linux and `chrome` on Windows.
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
## Related
|
84 |
|
|
|
85 |
|
|
- [opn-cli](https://github.com/sindresorhus/opn-cli) - CLI for this module
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
## License
|
89 |
|
|
|
90 |
|
|
MIT © [Sindre Sorhus](https://sindresorhus.com)
|