New JavaScript Automation Stuff + Docs Coming in 8.6

Hello!

I’ve been working on some new JavaScript stuff for Acorn 8.6, and thought I would post about the first stab at it here so I can get any feedback you all might have. You can download a preview of Acorn 8.6 from the following URL: https://flyingmeat.com/download/latest/

The motivation for this was to help make Acorn more scriptable from 3rd party tools, and to just generally make it more scriptable and inspectable. It’s kind of neat that you can query about a document and get JSON information back.

Acorn also has a new JavaScript API Reference page, which lists out all the supported APIs you can use to automate Acorn.

At any rate, direct from the release notes:

New Automation and JavaScript Things

Lots of interesting JavaScript things this time around.

The AppleScript do JavaScript command will now return a value. So something as simple as:

tell application “Acorn"
    do JavaScript "20+54”
end tell

Will return 74. But using Acorn for math is boring. And AppleScript is kind of boring. The Terminal is where it’s all at these days, so we’ve beefed up Acorn’s shell tool to be able to pipe JavaScript to it. With the new -jsio argument, you can pipe JavaScript from the clipboard and it will run it for you. (You might think this is dumb - but keep reading).

pbpaste | /Applications/Acorn.app/Contents/MacOS/Acorn -jsio

So you can now do something fancy like resizing all open images:

acorn.allDocuments().forEach((doc, index) => {
   doc.setCanvasSize_usingAnchor(NSMakeSize(1000, 1000), "top left"); 
});

In addition, when you use Acorn from the shell, it’ll send STDOUT back to the shell. So if you wanted to list all open images and return it as a JSON object, you would do the following:

var documentPaths = []
acorn.allDocuments().forEach((doc, index) => {
    // fileURL will be null if the file is not saved yet.
    if (doc.fileURL() != null) {
        documentPaths.push(doc.fileURL().path())
    }
});
print(JSON.stringify(documentPaths, null, 2));

This opens up a whole layer of possibilities for automation. You can query Acorn for open images, layers, etc., make some logic decisions, and then act on them.

We’ve also added a new option to disable JavaScript automation under Acorn’s Settings ▸ Advanced tab. So if you want to opt out of it, you can.

And if you want to drive Acorn using a LLM such as Claude, you can now point it to /Applications/Acorn.app/Contents/Resources/skills/acorn-javascript-api/and it’ll figure out the rest. For instance, you could ask:

“Ask Acorn to take a layered screenshot, and hide any layers that aren’t from Safari. Then export each visible layer to /tmp/ as a PNG.”

And if Claude is having a good day, it’ll figure out how to do what you’ve asked.