May 1, 2022 at 8:25 PM by Dr. Drang
As an example of a case where I did use JXA, let’s look at a Keyboard Maestro macro I use quite often at work. I get lots of data from clients in Excel files that are formatted more for presentation than for analysis. I move the data over to Numbers (I hate working in Excel), clean it up into a simple tabular form, and export it as CSV for analysis with Pandas and other bits of Python.
And here’s the code itself:
I wrote this in JXA instead of AppleScript because of Line 8. Although some text processing is easy in AppleScript, most of it is a pain in the ass. And simple regex substitutions fall into the “pain in the ass” category. If it’s just one text substitution, you can cheat with a little
do shell script, like this:
applescript: 1: tell application "Numbers" 2: -- Get the frontmost Numbers document and a path to its file 3: set topDoc to front document 4: set topPathString to file of topDoc as text 5: 6: -- Make a path to the CSV file we're going to export to. 7: set csvPathString to do shell script "sed 's/\\.numbers$/.csv/' <<< " & quoted form of topPathString 8: 9: -- Save the Numbers file and export it as a CSV 10: save topDoc 11: export topDoc to file csvPathString as CSV 12: end tell
The combination of
Still, there is one part of the JXA code I don’t like: Line 12:
I just find it hard to get the hang of this syntax. There are three arguments to this function. One of them is passed in as a regular variable, but the other two have to be rolled into an object. That’s just weird.
I might find it easier to get over my argument hangup if Apple put more effort into its JXA documentation. And by “more” effort I mean “any.” Here’s the AppleScript dictionary entry for the Numbers
export command. It’s written more or less as you would write it in AppleScript.
export is a method, we’re not told what it’s a method of. My first instinct is that it should be a method of the document, not the app.
I didn’t write the AppleScript code until I started putting together this post and felt the need for a counterexample. ↩