Writing plugins and image filters for Acorn

Download sample code and header files: SampleAcornPlugins.zip

Visit the Acorn wiki, which has more examples and information: http://flyingmeat.com/wikka/AcornPlugins

Acorn provides a plugin interface for developers, in both Objective-C and Python (using the PyObjC bridge). Acorn also uses any Core Image Units that are installed on the computer, which is also an option if you want to provide filtering services to Acorn.

Here's an example python plugin that will add a gray border around the current bitmap layer. Just place it in a text file named "GrayBorder.py" and place it in your ~/Library/Application Support/Acorn/Plug-Ins/ folder, and restart Acorn.

import objc
from Foundation import *
from AppKit import *

ACScriptSuperMenuTitle = None
ACScriptMenuTitle = "Add Gray Border"

CIImage = objc.lookUpClass('CIImage')

def main(image):
    
    nsimg = image.NSImage()
    nsimg.lockFocus()
    NSColor.grayColor().set()
    path = NSBezierPath.bezierPathWithRect_(NSMakeRect(.5, .5, nsimg.size().width - 1,
                                                       nsimg.size().height - 1))
    path.stroke()
    nsimg.unlockFocus()
    return CIImage.imageWithData_(nsimg.TIFFRepresentation())
And here is another example, which takes the current bitmap layer and converts it to grayscale using CoreImage. It also setups a keyboard shortcut, using Control-Command-G:
import objc

ACScriptSuperMenuTitle = "Color"
ACScriptMenuTitle = "Make Grayscale (python)"
ACShortcutKey = 'g'
ACShortcutMask = "command control"

CIColor  = objc.lookUpClass('CIColor')
CIFilter = objc.lookUpClass('CIFilter')

def main(image):
    
    color = CIColor.colorWithRed_green_blue_(0.5, 0.5, 0.5)
    
    filter = CIFilter.filterWithName_('CIColorMonochrome')
    filter.setDefaults()
    
    filter.setValue_forKey_(image, 'inputImage')
    filter.setValue_forKey_(color, 'inputColor')
    filter.setValue_forKey_(1, 'inputIntensity')
    
    return filter.valueForKey_('outputImage')
You can also write scripts to do something other than filter an image. For example- here's a script that'll save the current image as a tiff, and it also sets the ACIsAction flag to true, so it'll show up in the File->Actions menu.
import objc, os
from Foundation import *
from AppKit import *

ACScriptSuperMenuTitle = None
ACScriptMenuTitle = "Quick save as TIFF"
ACShortcutKey = 'i'
ACShortcutMask = "command control"
ACIsAction = True

def main(image):
    
    doc      = NSDocumentController.sharedDocumentController().currentDocument()
    data     = doc.dataRepresentationOfType_('public.tiff')
    filePath = doc.fileName().stringByDeletingPathExtension() + ".tiff"
    
    # this will overwrite any file that may already be there
    data.writeToFile_atomically_(filePath, True)
    
    return None

The documentation for the plugin interface is found in the header file "ACPlugin.h", located in the sample plugin code above. If you would like to do more with the plugin API, write support@flyingmeat.com and let us know.