You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,12 +31,14 @@ const myModule = require( './my-module' ); // local modules too
31
31
> ES6 module syntax (`import from`) isn't currently supported, so stick with `require()`, and use `module.exports` in your own local modules.
32
32
33
33
You can also use your **favorite Javascript editor** instead of the OS X Script Editor, and use workflow that is much more familiar to JS developers (vs. compiling .js files to .scpt binary format).
34
+
35
+
So long as your editor can launch a shebang'd script, you can run or debug JXA while you edit.
34
36
35
37
## Installation and usage
36
38
37
39
You'll likely want to install node-jxa globally:
38
40
```bash
39
-
yarn global add node-jxa # or the npm equivalent
41
+
yarn global add node-jxa # or the npm equivalent: npm install -g node-jxa
40
42
```
41
43
42
44
This will install `node-jxa` and make it available in your `PATH` env var.
@@ -81,6 +83,9 @@ All modules you `require` in your scripts must be installed (i.e. in the `node_m
81
83
82
84
I suggest managing your node-jxa scripts like any node.js project, with a package.json specifying the needed module dependencies. Simply use `yarn` or `npm` to add and remove the libraries you need.
83
85
86
+
> Note that Browserify won't automatically package modules for which the `require`d path is computed at runtime. For example, `let myNeededModulePath = './my/needed/module' ; let myModule = require( myNeededModulePath );` will leave the module out and it won't work. This can happen within modules you're getting from npm.
87
+
> Browserify has techniques for handling this, but node-jxa doesn't currently employ them.
88
+
84
89
### Using Applescript libraries
85
90
86
91
You can use your Applescript libraries in your JXA scripts using the `Library` global function, like so:
@@ -99,19 +104,19 @@ This works with JXA libraries too - you can use `Library()` to import .scpt scri
99
104
100
105
### Scripting Dictionaries
101
106
102
-
With node-jxa you can use your favorite JS editor for writing and managing your JXA code, but the OS X Script is still useful for viewing the Scripting Dictionaries for your scriptable apps. Essentially, this is the documentation for the API exposed by the apps' developers. Look for **Open Dictionary...** on the File Menu.
107
+
With node-jxa you can use your favorite JS editor for writing and managing your JXA code, but the OS X Script is still useful for viewing the Scripting Dictionaries for your scriptable apps. Essentially, this is the documentation for the API exposed by the apps' developers. Look for **Open Dictionary...** on the File Menu, and be sure to select `JavaScript` in the language selector at the top.
103
108
104
109
### Be prepared for some weirdness
105
110
106
111
Some things are weird, including but not limited to:
107
112
108
113
- trying to `console.log` a JXA application object will probably crash the process.
109
-
- arrays returned directly by Applications (i.e. searching or filtering) are weird. A simple trick like `array.map( el => el )` will give you a real JS array.
114
+
-[Element Arrays](https://developer.apple.com/library/content/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW9) (arrays provided directly by Applications (or [filtering via `whose()`](https://developer.apple.com/library/content/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-10.html#//apple_ref/doc/uid/TP40014508-CH109-SW10)) are weird. A simple trick like `array.map( el => el )` will give you a real JS array.
110
115
- property access for JXA objects is expensive. If you're doing many reads, your script can take a long time and may even time out. A caching strategy can help.
111
116
112
117
### Debugging
113
118
114
-
You can [debug your JXA scripts](https://developer.apple.com/library/content/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-11.html#//apple_ref/doc/uid/TP40014508-CH110-SW3)using Safari dev tools.
119
+
You can debug your JXA scripts using Safari dev tools. To debug, [enable JSContexts support](https://developer.apple.com/library/content/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/OSX10-11.html#//apple_ref/doc/uid/TP40014508-CH110-SW3)in Safari, then simply include the `debugger;` command in your script. When you run the script it'll stop and open at that spot in the Safari debugger (from here, you can add additional breakpoints in Safari's debugger).
115
120
116
121
If you add the `--debug` (or `-d`) switch to the `node-jxa`, command (including in your shebangs), Browserify will include sourcemaps in the bundled code, for a cleaner debugging experience that is more focused on your own code.
0 commit comments