Skip to main content

JXA Run

Deno module for running JavaScript for Automation (JXA), mostly ported from the Node package @jxa/run.

Contents

Prerequisites

  • macOS 10.10 or later
  • Deno

Usage

// script.js
import { run } from "https://deno.land/x/jxa_run/mod.ts"

const result = await run((name, emoji) =>
  `Hello ${name} ${emoji}!`, "Deno", "🦕");
console.log(result)
$ deno run --allow-run script.js
Hello Deno 🦕!

For TypeScript specific usage, see types/.

Importing

From deno.land/x:

// Always import the latest release
import { run } from "https://deno.land/x/jxa_run/mod.ts"
// Import a specific version
import { run } from "https://deno.land/x/jxa_run@{tag}/mod.ts"

From nest.land:

import { run } from "https://x.nest.land/jxa-run@{tag}/mod.ts"

API

run(jxaFunction, …args)

Return a Promise giving the output of jxaFunction on args.

jxaFunction

Type: (...args: any[]) => any

…args

Type: any[]

Note: jxaFunction does not have access to variables outside its scope. For example, running the code below will result in an error.

const wolf = "🐺"
run(() => console.log(wolf))
// ReferenceError: Can't find variable: wolf

runJXACode(jxaCode)

jxaCode

Type: string

Example:

const result = await runJXACode(
  `Application("System Events").currentUser().name()`
  );
console.log(result) 
// [username]

Differences from @jxa/run

The main difference is in error handling. If there is an error in JXA execution, @jxa/run (the Node package) will log the error message to console without throwing any errors. On the other hand, jxa-run (this Deno module) will throw an Error.

Testing

Running make will perform the tests in test.ts.