1 | osascript
|
2 | ===
|
3 |
|
4 | > Node.js module for doing Apple Open Scripting Architecture (OSA) in JavaScript or AppleScript.
|
5 |
|
6 | ## Install
|
7 | ```
|
8 | npm install --save osascript
|
9 | ```
|
10 |
|
11 | ## Requirements
|
12 |
|
13 | For using JavaScript as Automation language, Mac OS X Yosemite is required.
|
14 | On versions before that, you can pass AppleScripts.
|
15 |
|
16 | ## Usage
|
17 |
|
18 | By default, if no other type is defined and the passed file is not a AppleScript
|
19 | file (with extensions `.scpt` or `.applescript`), JavaScript is used.
|
20 |
|
21 | See the last example for overriding this behaviour and passing on AppleScript
|
22 | instead. All API's are the same.
|
23 |
|
24 | ### Default stream
|
25 |
|
26 | ```javascript
|
27 | var osascript = require('osascript');
|
28 | var fs = require('fs');
|
29 |
|
30 | // Run JavaScript file through OSA
|
31 | fs.createReadStream('someFile.js')
|
32 | .pipe(osascript())
|
33 | .pipe(process.stdout);
|
34 | ```
|
35 |
|
36 | This will pipe the data from `someFile.js` to the Apple Open Scripting Architecture (OSA)
|
37 | and print the result to the standard output.
|
38 |
|
39 | You can also do this in a short-hand:
|
40 |
|
41 | ```javascript
|
42 | // note the file method after require ¬
|
43 | var osascript = require('osascript').file;
|
44 |
|
45 | // Run JavaScript file through OSA
|
46 | osascript('someFile.js').pipe(process.stdout);
|
47 | ```
|
48 |
|
49 | Or if you only have a string, you can do that as well
|
50 | ```javascript
|
51 | // note the eval method after require ¬
|
52 | var osascript = require('osascript').eval;
|
53 |
|
54 | // Run JavaScript text through OSA
|
55 | osascript('console.log("Hello, world!");').pipe(process.stdout);
|
56 | ```
|
57 |
|
58 | ### Using a callback
|
59 |
|
60 | If you don't want to use a stream (just get the buffered output)
|
61 | you can do this on the `file` and `eval` methods by passing a
|
62 | function as the last argument:
|
63 |
|
64 | ```javascript
|
65 | // note the file method after require ¬
|
66 | var osascript = require('osascript').file;
|
67 |
|
68 | // Run JavaScript file through OSA
|
69 | osascript('someFile.js', function (err, data) {
|
70 | console.log(err, data);
|
71 | });
|
72 | ```
|
73 |
|
74 | ```javascript
|
75 | // note the eval method after require ¬
|
76 | var osascript = require('osascript').eval;
|
77 |
|
78 | // Run JavaScript text through OSA
|
79 | osascript('console.log("Hello, world!");', function (err, data) {
|
80 | console.log(err, data);
|
81 | });
|
82 | ```
|
83 |
|
84 | ### Using AppleScript
|
85 |
|
86 | As JavaScript as OSA isn't working on versions before Yosemite,
|
87 | we can use AppleScript as well. JavaScript is the default
|
88 | to try to encourage JS instead of AppleScript. When
|
89 | a filename is passed, AppleScript will be used if the filename
|
90 | has an AppleScript extension (`.scpt` or `.applescript`).
|
91 |
|
92 |
|
93 | ```javascript
|
94 | var osascript = require('osascript');
|
95 | var fs = require('fs');
|
96 |
|
97 | // Run JavaScript file through OSA
|
98 | fs.createReadStream('someAppleScript.applescript')
|
99 | // Need to override options to define AppleScript
|
100 | .pipe(osascript({ type: 'AppleScript' }))
|
101 | .pipe(process.stdout);
|
102 | ```
|
103 |
|
104 | ```javascript
|
105 | // note the file method after require ¬
|
106 | var osascript = require('osascript').file;
|
107 |
|
108 | // No need to pass options, as it can be deduced from filename.
|
109 | osascript('someFile.applescript', function (err, data) {
|
110 | console.log(err, data);
|
111 | });
|
112 | ```
|
113 |
|
114 | See [more examples](./examples).
|
115 |
|
116 | ### API
|
117 |
|
118 | API from base function required in this way:
|
119 |
|
120 | ```javascript
|
121 | var osascript = require('osascript');
|
122 | ```
|
123 |
|
124 | All endpoints uses `options`:
|
125 |
|
126 | ```javascript
|
127 | var defaultOptions = {
|
128 | type: 'JavaScript',
|
129 | args: [] // List of string arguments
|
130 | };
|
131 | ```
|
132 |
|
133 | Type is passed as language (flag `-l`) to `osascript`.
|
134 | Can be either `JavaScript` (in Yosemite) or `AppleScript`.
|
135 |
|
136 | `args` is a list of strings passed in as arguments to your scripts. In JavaScript
|
137 | you can access them using:
|
138 |
|
139 | ```js
|
140 | ObjC.import('Cocoa');
|
141 | var args = ObjC.deepUnwrap($.NSProcessInfo.processInfo.arguments).slice(4);
|
142 | ```
|
143 |
|
144 | `.slice(4)` is to cut away program name and language argument. In addition,
|
145 | `node-osascript` has to put in a `-` to indicate that the following list of
|
146 | strings should be passed as arguments. This `-` is on index 3.
|
147 |
|
148 | #### `osascript([options: Object])`
|
149 |
|
150 | Creates a PassThrough stream that can get piped text manually
|
151 | (text or files).
|
152 |
|
153 | #### `osascript.file(file[, options: Object, callback:function (err, data)])`
|
154 | See `options` as defined above.
|
155 |
|
156 | If callback function is passed, the buffered output from
|
157 | the OSA is passed as data (initiates the data immediately)
|
158 |
|
159 | #### `osascript.eval(scriptText[, options: Object, callback:function (err, data)])`
|
160 | `scriptText` is script in the language type as defined.
|
161 |
|
162 | See `options` as defined above.
|
163 |
|
164 | If callback function is passed, the buffered output from
|
165 | the OSA is passed as data (initiates the data immediately)
|
166 |
|
167 | ## TODO
|
168 |
|
169 | * [ ] Tests
|
170 | * [x] Error handling
|