I have recently developed an interest in learning JXA, but there is a relative dearth of information/tutorials out there. I found a post on MacStories and an interesting GitHub repo, but I cannot find a more formal introduction to JXA (not even from Apple). Anyone know any good resources? I am actually not asking how to learn Javascript - I know plenty of (free) resources for that. I am asking how to use Javascript to automate the Mac.
Thanks for the tip - I have seen several of these resources on various websites, and I think that there is a GitHub repo that tried to centralize all of these…
In the meantime, I did find this repo with some useful stuff on it:
It’s just an instance of JSContext with an Automation library object, the keys of which can be listed (and copied to clipboard) for inspection, by evaluating this snippet:
(() => {
'use strict';
// Rob Trew 2020
// Indented outline of macOS Automation library keys
// (copied to clipboard)
ObjC.import('AppKit');
// main :: IO ()
const main = () =>
copyText(
showOutline(
keyTree([
'Automation', 'name', 'prototype',
'__private__', '0', 'length'
])('Automation')(Automation)
)
);
// ------------------ NEST OF KEYS -------------------
// keyTree :: [String] -> String ->
// Object -> Tree String
const keyTree = except =>
name => obj => {
const go = (seen, o) =>
Object.getOwnPropertyNames(o)
.sort()
.flatMap(
k => !seen.includes(k) ? [
Node(k)(
go(seen.concat(k), o[k])
)
] : []
);
return Node(name)(
go(except, obj)
);
};
// showOutline :: Tree String -> String
const showOutline = tree => {
const go = indent => x =>
unlines(
[indent + x.root]
.concat(
x.nest.flatMap(
go(' ' + indent)
)
)
);
return go('')(tree);
};
// -------------------- CLIPBOARD --------------------
// copyText :: String -> IO String
const copyText = s => {
const pb = $.NSPasteboard.generalPasteboard;
return (
pb.clearContents,
pb.setStringForType(
$(s),
$.NSPasteboardTypeString
),
s
);
};
// ---------------- GENERIC FUNCTIONS ----------------
// Node :: a -> [Tree a] -> Tree a
const Node = v =>
// Constructor for a Tree node which connects a
// value of some kind to a list of zero or
// more child trees.
xs => ({
type: 'Node',
root: v,
nest: xs || []
});
// unlines :: [String] -> String
const unlines = xs =>
xs.join('\n');
// MAIN ---
return main();
})();