UNPKG

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