1 | # opn
|
2 |
|
3 | > A better [node-open](https://github.com/pwnall/node-open). Opens stuff like websites, files, executables. Cross-platform.
|
4 |
|
5 |
|
6 | #### Why?
|
7 |
|
8 | - Actively maintained
|
9 | - Supports app arguments
|
10 | - Safer as it uses `spawn` instead of `exec`
|
11 | - Fixes most of the open `node-open` issues
|
12 | - Comes with a CLI for use in your scripts
|
13 | - Includes the latest [`xdg-open` script](http://cgit.freedesktop.org/xdg/xdg-utils/commit/?id=c55122295c2a480fa721a9614f0e2d42b2949c18) for Linux
|
14 |
|
15 |
|
16 | ## Install
|
17 |
|
18 | ```
|
19 | $ npm install --save opn
|
20 | ```
|
21 |
|
22 |
|
23 | ## Usage
|
24 |
|
25 | ```js
|
26 | var opn = require('opn');
|
27 |
|
28 | opn('unicorn.png');
|
29 | // opens the image in the default image viewer
|
30 |
|
31 | opn('http://sindresorhus.com');
|
32 | // opens the url in the default browser
|
33 |
|
34 | opn('http://sindresorhus.com', 'firefox');
|
35 | // specify the app to open in
|
36 |
|
37 | opn('http://sindresorhus.com', ['google chrome', '--incognito']);
|
38 | // specify app arguments
|
39 | ```
|
40 |
|
41 |
|
42 | ## API
|
43 |
|
44 | Uses the command `open` on OS X, `start` on Windows and `xdg-open` on other platforms.
|
45 |
|
46 | ### opn(target, [app], [callback])
|
47 |
|
48 | #### target
|
49 |
|
50 | *Required*
|
51 | Type: `string`
|
52 |
|
53 | The thing you want to open. Can be a URL, file, or executable.
|
54 |
|
55 | Opens in the default app for the file type. Eg. URLs opens in your default browser.
|
56 |
|
57 | #### app
|
58 |
|
59 | Type: `string`, `array`
|
60 |
|
61 | Specify the app to open the `target` with, or an array with the app and app arguments.
|
62 |
|
63 | The app name is platform dependent. Don't hard code it in reusable modules. Eg. Chrome is `google chrome` on OS X, `google-chrome` on Linux and `chrome` on Windows.
|
64 |
|
65 | #### callback(error)
|
66 |
|
67 | Type: `function`
|
68 |
|
69 | Called when the opened app exits.
|
70 |
|
71 | On Windows you have to explicitly specify an app for it to be able to wait.
|
72 |
|
73 |
|
74 | ## CLI
|
75 |
|
76 | ```
|
77 | $ npm install --global opn
|
78 | ```
|
79 |
|
80 | ```
|
81 | $ opn --help
|
82 |
|
83 | Usage
|
84 | $ opn <file|url> [app] [app arguments]
|
85 |
|
86 | Example
|
87 | $ opn http://sindresorhus.com
|
88 | $ opn http://sindresorhus.com firefox
|
89 | $ opn http://sindresorhus.com 'google chrome' --incognito
|
90 | $ opn unicorn.png
|
91 | ```
|
92 |
|
93 |
|
94 | ## License
|
95 |
|
96 | MIT © [Sindre Sorhus](http://sindresorhus.com)
|