Acorn Acorn 7

Example AppleScript and CocoaScript Scripts


Below you'll find lots of examples on controlling Acorn from AppleScript and Cocoa Script (a JavaScript to Cocoa bridge).



Open an image:


AppleScript:

tell application "Acorn"

    open "srv:Users:gus:Pictures:MyImage.acorn"

end tell


Cocoa Script:

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

acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");




Open and save an image as another type:


AppleScript:

tell application "Acorn"

    open "srv:Users:gus:Pictures:MyImage.acorn"

    tell document "MyImage.acorn" to save as PNG in "srv:Users:gus:Pictures:MyImage.png"

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.dataOfType("public.png").writeToFile("/Volumes/srv/Users/gus/Pictures/MyImage.png")




Making a new image:

(new in Acorn 3.2)


Cocoa Script:

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

var doc = acorn.makeDocumentWithSize(NSMakeSize(200, 100));




Resize the canvas:


AppleScript:

tell application "Acorn"

    tell document 1

        resize canvas width 1000 height 1000 anchor position middle left

    end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.setCanvasSize_usingAnchor(NSMakeSize(1000, 1000), "top left");





Crop an image:


AppleScript:

tell application "Acorn"

    open "srv:Users:gus:Pictures:MyImage.acorn"

    tell document "MyImage.acorn"

        -- crop to a rectangle, located at 100,100

        -- and with a width and height of 400

        crop rect {100, 100, 400, 400}

    end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.cropToRect(NSMakeRect(100, 100, 400, 400));





Resize an image, and then undo:


AppleScript:

tell application "Acorn"

    open "srv:Users:gus:Pictures:MyImage.acorn"

    tell document "MyImage.acorn"

        resize image width 200

        activate

        undo

    end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.scaleImageToWidth(200)

acorn.activate();

doc.undo()






Change the resolution of an image:


AppleScript:

tell application "Acorn"

    open "srv:Users:gus:Pictures:MyImage.acorn"

    tell document "MyImage.acorn"

        resize image resolution 144

    end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.setDpi(NSMakeSize(144, 144));




Find the name of the first layer:


AppleScript:

tell application "Acorn"

    open "srv:Users:gus:Pictures:MyImage.acorn"

    tell document "MyImage.acorn"

        name of layer 1

    end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

var name = doc.layers()[0].layerName();




Find the width and height of the image:


AppleScript:

tell application "Acorn"

    open "srv:Users:gus:Pictures:MyImage.acorn"

    set w to width of document 1

    set h to height of document 1

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

var size = doc.canvasSize()

print("The size is: " + size.width + " x " + size.height);




Duplicating a document, resize it, and save it as a PNG:


AppleScript:

tell application "Acorn"

    open "srv:Users:gus:Pictures:MyImage.acorn"

    duplicate document 1 with properties {name:"New Document"}

    

    tell document "New Document"

        resize image width 200

        save as PNG in "tmp:foo.png"

    end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

var newDoc = doc.duplicate()

newDoc.scaleImageToWidth(200)

newDoc.dataOfType("public.png").writeToFile("/tmp/foo.png")






Get all the names of the top level layers:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

set layerNames to ""

repeat with l in layers in document 1

set layerNames to (layerNames & name of l & ", ")

end repeat

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

for (i = 0; i < doc.layers().count(); i++) {

    var layer = doc.layers()[i];

    print(layer.layerName());

}




Trim a document


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

trim document 1

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.trim();




Set the blend modes for a layer:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document "MyImage.acorn"

set l to {pass through, color blend, color burn, color dodge, darken, destination atop, destination out, destination over, destination in, exclusion, hard light, hue, lighten, luminosity, multiply, normal, overlay, plus darker, plus lighter, saturation, screen, soft light, source atop, source in, source out, xor}

repeat with loopVar in l

set blend mode of layer 4 to loopVar

delay 1

end repeat

end tell

end tell


Cocoa Script:

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

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

var layer = doc.layers()[1];


var blendModes = ["clear", "color blend", "color burn", "color dodge", "copy", "darken", "destination atop", "destination out", "destination over", "difference", "exclusion", "hard light", "hue", "lighten", "luminosity", "multiply", "normal", "overlay", "pass through", "plus darker", "plus lighter", "saturation", "screen", "soft light", "source atop", "source in", "source out", "xor"];


for (var idx in blendModes) {

    print(blendModes[idx]);

    layer.setBlendMode(blendModes[idx]);

}



Duplicating a layer:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document "MyImage.acorn"

duplicate layer 1

end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

var layer = doc.layers()[0];

var newLayer = layer.duplicate();

// then do something with it.

newLayer.callFilterNamed("CIColorInvert");




Web Export:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document "MyImage.acorn"

trim -- cut down on any extra whitespace

web export in "tmp:foo.png" as PNG

web export in "tmp:foo.jpeg" as JPEG quality 25

web export in "tmp:foo.jp2" as JPEG 2000 quality 25

web export in "tmp:foo.gif" as GIF

undo -- undo our trim.  Or we could just close the doc

end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");


doc.trim() // cut down on any extra whitespace


doc.webExportWithOptions({'uti': 'public.png', 'file': '/tmp/foo.png'});

doc.webExportWithOptions({'uti': 'public.jpeg', 'quality': 25, 'file': '/tmp/foo.jpeg'});

doc.webExportWithOptions({'uti': 'public.jpeg-2000', 'quality': 25, 'progressive': true, 'file': '/tmp/foo.jp2'});

doc.webExportWithOptions({'uti': 'com.compuserve.gif', 'file': '/tmp/foo.gif'});

doc.undo(); // undo our trim.  Or we could just close the doc


Copy Merged:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document "MyImage.acorn"

copy merged

end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.copyMerged()



How to tell if a document is around or not:


AppleScript:

tell application "Acorn"

if exists document 1 then

-- do whatever

end if

end tell


Cocoa Script:

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

var docs = acorn.orderedDocuments();

if (docs.length() > 0) {

    // do whatever

}






Filling a layer:

-- choose a color

set aColor to choose color


tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document 1

-- fill the current layer with our selected color

fill with color aColor

delay 1

undo

-- let's do it again, but by adding a 50% alpha to the color list, so it'll be transparent

set aColor to aColor & (65535 * 0.5)

fill with color aColor

end tell

end tell





Flipping the canvas:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document 1

flip canvas direction horizontal

-- we could also say "vertical" here.

end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.flipCanvasWithDirection("vertical");




Having fun with selections:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document 1

select all

delay 1

deselect

select rect {100, 100, 400, 400}

invert selection

end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.selectAll();

doc.deselect();

doc.selectFrame(NSMakeRect(100, 100, 400, 400));

doc.invertSelection();


Merge visible layers:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document 1

merge visible layers

end tell

end tell


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.mergeVisibleLayers()



Setting a preference:

In this example, we show how to set the registration name and number for our copy of Acorn.


AppleScript:

tell application "Acorn"

set preference key "registrationName" value "August Mueller"

set preference key "registrationNumber" value "FMAC20-XXXXXXXXX"

quit

end tell


Cocoa Script:

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

acorn.setPreference_forKey("1", "drawMarchingAntsSelection");





Calling a filter:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document 1 to do filter name "CIColorInvert"

tell document 1 to do filter name "New Image With Curved Drop Shadow"

end tell


Cocoa Script:

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

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

var layer = doc.layers()[0];

doc.callFilterNamed("CIColorInvert");

doc.callFilterNamed("New Image With Curved Drop Shadow");




Selecting a layer:


Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

var layer = doc.layers()[0];

doc.setSelectedLayer(layer);




Calling Auto Levels:


AppleScript:

tell application "Acorn"

tell document 1

auto levels

end tell

end tell


Target a specific layer with AppleScript:


tell application "Acorn"

tell document 1

tell layer 2

auto levels

end tell

end tell

end tell




Cocoa Script:

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

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

doc.autoLevels();


// or target a specific layer:

doc.undo();

var layer = doc.layers()[1];

layer.autoLevels()





Making an Oval Selection:


AppleScript:

tell application "Acorn"

open "srv:Users:gus:Pictures:MyImage.acorn"

tell document 1

deselect

select oval rect {100, 100, 400, 600}

close without saving

end tell

end tell




Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

doc.deselect();

doc.selectOval(NSMakeRect(100, 100, 400, 400));

doc.close();




Add an image as a layer:

Cocoa Script:

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

var doc = acorn.open("/Volumes/srv/Users/gus/Pictures/MyImage.acorn");

var layer = doc.layers()[0];

doc.setSelectedLayer(layer);


// 0 puts it at the bottom.  1 or higher would put it on top..

var newLayer = doc.baseGroup().insertLayerWithImagePath_atIndex("/Volumes/srv/Users/gus/Pictures/AnotherImage.png", 3)




Note For App Store Users: If you've purchased Acorn from the Mac App Store, certain AppleScripts, Automator Actions, and CocoaScript scripts may not work for you because of sandboxing restrictions required by Apple.  The direct version of Acorn does not have these issues.