Acorn Acorn 4

Scripting Acorn


See also: Example AppleScript and JSTalk Scripts


You can script in Acorn using a couple of different languages- AppleScript, Python, or JSTalk (which is pretty much JavaScript with some extra goodies).  You can edit both Python and JSTalk scripts using TextEdit- but you might have an easier time using something like Apple's Xcode (which is free), BBEdit, SubEthaEdit, or even TextMate.  These text editors offer niceties such as syntax coloring and line numbers, making it easier to write and debug your scripts.


JSTalk also comes with an editor you can use for free, and can be downloaded from http://jstalk.org/.  Since JavaScript is a very popular language, it will be used in the examples below.  However, you can find examples for Python on the Writing PlugIns page.


If you don't know JavaScript, there are lots and lots of tutorials out on the web you should check out.


Scripting Acorn with AppleScript can be done from the AppleScript Editor, located in your Applications folder.



Scripting Acorn from JSTalk Editor


In the examples below, a file named "jimi.jpg" will be located in the Pictures folder.  Here is a script that can be run from JSTalk Editor for opening an image and resizing it to 200 pixels wide:


var acorn = JSTalk.application("Acorn");

var doc = acorn.open_("/Users/gus/Pictures/Jimi.jpg");

doc.scaleImageToWidth(200);


The first line grabs a reference to Acorn, the application.  It tells Acorn to open up a document, located at /Users/gus/Pictures/Jimi.jpg.  The "open" function opens the document, and then on the final line resizes the image to be 200 pixels wide.  Pretty simple!


Here is another example, which is a bit more complicated:


var acorn = JSTalk.application("Acorn");

var filePath = "/Users/gus/Pictures/Jimi.jpg";

var doc = acorn.open_("/Users/gus/Pictures/Jimi.jpg");


var size = doc.canvasSize()

var newWidth = size.width / 2;


doc.scaleImageToWidth(newWidth);


doc.dataRepresentationOfType("public.png").writeToFile(filePath + ".png");


doc.close();



The first couple of lines look familiar, but then adds something a little different.  Instead of resizing the image to 200 pixels wide, it resizes to half the width.  On line 5, notice the size of the image.  Then on line 6, notice a variable named "newWidth" that is half the width of the current size.  The image is then scaled on line 8, a copy of the image is saved as a PNG image, then written to a file.  Finally the document is closed.


Important:  If the document is closed without saving it first, the changes are lost.  To save the document before closing it, add:


doc.saveDocument(null);


So what else can be done with the document?  The official list of things are located in the file "ACPlugin.h", located in the Acorn SDK (https://github.com/ccgus/AcornSDK)- but here are some of them:


doc.layers() - returns an array of the layers.

doc.currentLayer() - return the current layer.

doc.cropToRect(NSRect) - crop a rect to a particular dimension.

doc.scaleImageToHeight(newHeight); - similar to scaleImageToWidth.

doc.canvasSize() - get the current size.

doc.setCanvasSize(NSSize) - set a new size.


Acorn documents inherit from NSDocument, so you can call all the methods located in NSDocument as well.



Writing plugins for Acorn in JSTalk


The next example will also use JSTalk Editor, so don't quit it just yet.  In fact, make a new document in JSTalk Editor and paste this code in:


function main(image) {

    var acorn = NSApplication.sharedApplication();

    var doc = acorn.orderedDocuments()[0];

    var size = doc.canvasSize()

    var newWidth = size.width / 2;

    doc.scaleImageToWidth(newWidth);

}


Save it to file named "Resize to 50%.jstalk", in your ~/Library/Application Support/Acorn/Plug-Ins/ folder.  Then quit and restart Acorn, and open an image up.

Note: if you are using Acorn from the Mac App Store, then you'll need to place your plugins in your ~/Library/Containers/com.flyingmeat.ACorn/Data/Library/Application\ Support/Acorn/Plug-Ins folder.


Once you have done that, choose the menu item Filter ▸ Resize to 50%.  Your image should then resize to 50% of its original size.


So what is going on here?


Well, first off- whenever you save a file in your Acorn Plug-Ins folder, and it has the file extension "jstalk" - it will show up in the Filter menu.  Then, when the plugin is called, Acorn looks specifically for the function named "main" in your image, and then passes it a copy of the current layer's image.  Nothing was done directly with the image.  Instead Acorn was asked for the current document, and then resized it like in the previous script.


Here is another example.  This can go in a new file named "Resize Layer to 50%.jstalk", in the same Plug-Ins folder as before (and yes, you'll need to quit and restart Acorn for the plugin to show up):


function main(image) {

    var newImage = image.imageByApplyingTransform(CGAffineTransformMakeScale(.5, .5));

    return newImage;

}


In this example, something is actually done with our new image (which is a CIImage by the way).  A new image is made by applying a transform to the original image, which scales it by 50%.  Return to that image.  You'll notice that when calling this function your layer has been replaced with a new layer, which is 50% of the original size.  However, your overall image size has not changed because you didn't change the document's size.


Before this plugin:


Pasted Graphic 1.tiff


And after:

Pasted Graphic.tiff





Scripting Acorn with JSTalk and Automator


If you download the JSTalk package (available from http://jstalk.org/ ), it comes with an Automator plugin.  When you combine the two, you can then make an action which will take files that you pass to it, and then do various things like resizing them:


function run(input, parameters) {

var acorn = [JSTalk application:"Acorn"];

    

    var idx = 0;

    

    while (idx < input.length()) {

        var filePath = input[idx];

        var doc = [acorn open: filePath];

        [doc scaleImageToHeight:200];

        [[doc dataRepresentationOfType:"public.png"] writeToFile: filePath + ".png"];

        [doc close];

        idx++;

    }

     

return input;

}


The possibilities are endless.


If you would like to see a specific example, or you have a task that you would like to see some example code for contact: support@flyingmeat.com and we'll see if we can come up with a solution for you!




Scripting Acorn with AppleScript and Automator


Here is an example Automator action that will take the images passed to it, and web export them to PNG files.


on run {input, parameters}

repeat with anImage in the input

tell application "Acorn"

set newFile to (POSIX path of anImage) & ".png"

open anImage as alias

tell document 1

web export in newFile as PNG

close

end tell

end tell

end repeat

return input

end run




Note For App Store Users: If you've purchased and downloaded Acorn from the Mac App Store, certain AppleScripts, Automator Actions, and JSTalk scripts may not work for you because of sandboxing restrictions required by Apple.  The direct version of Acorn does not have these issues (and if you have run the App Store version of Acorn at least once, the direct version will run registered, so it's easy enough to switch if you need to).