This is a helper library for AppleScript in JavaScript that works on macOS 10.10 and above. To
properly use this, a bundler must be used such as Webpack. It is recommended to install
jxa-types as well.
This repository also demonstrates how to write tests mocking the JXA environment. See the *.test.ts
files in src.
import { FileManager } from 'jxa-lib';
const fm = new FileManager();
let attr;
try {
attr = fm.attributesOfItem('/some-file');
} catch (e) {
// Instead of having to pass &error (Ref object) like in Objective-C,
// an exception is thrown
console.log('Maybe /some-file does not exist?');
}
// attr type is FileAttributes or undefined, which does not have prefixes removed
if (attr) {
console.log(attr.NSFileGroupOwnerAccountID); // string
console.log(attr.NSFileModificationDate); // Date object
}
You do not have to use ObjC.import() because all modules will do this on their own.
import { stdlib, string } from 'jxa-lib';
const size = 32;
const buf = stdlib.malloc(size); // returns Ref<number>
string.memset(buf, 0, size);
for (let i = 0, c = 'a'.charCodeAt(0); i < size; i++, c++) {
buf[i] = c;
}
const asciiC = 'c'.charCodeAt(0);
const asciiD = 'd'.charCodeAt(0);
// memchr() returns Ref<number> or Ref to NULL
const result = string.memchr(buf, asciiC, size);
if (result[0]) {
// this will be asciiC or null/undefined
console.log(result[0] === asciiC); // true
console.log(result[1] === asciiD); // true
// Getting result[30] or above is not defined behaviour
}
stdlib.free(result);
// Do not print the result of the last expression
stdlib.exit(0);