Hi, I'm Brian Reavis. I design and develop
awesome things for COLOURlovers. I love mountains.

JSX scripting makes controlling Adobe Creative Suite from an extension you’re developing a breeze. Scripting opens up the realm of creating guides, selecting tools, creating layers, running filters, and so on. To get you started, Adobe Extension Builder creates a basic *.jsx file with a sample function in every new project:

function jsxFunction() {
   // ... your code goes here ...
   return '<object><property id="success"><true /></property></object>';
}

To call that function from AS3, it’s easy:

var result:SyncResult = CSXSInterface.instance.evalScript('jsxFunction');

The Workflow

First, install the Scripting Listener plugin. With it installed, whenever you do something in Photoshop/Illustrator/etc, it will write the code needed to perform those steps to ScriptingListenerJS.log on your desktop. Copy the parts you need from there and put them in your *.jsx file, wrapped in a function. Then just call evalScript() with the function name, and you’re good!

Function Arguments

Sometimes you might need to pass a value to a JSX function from AS3—maybe it’s a coordinate, maybe it’s the path of a file, maybe it’s a color. Arguments is where life gets janky. In CS4 products, Continue reading

Adobe AIR provides great references to common locations on a user’s computer that map to the right place depending on the operating system.

It’s missing one, however: File.downloadsDirectory. Here’s what would work:

/**
 * File.downloadsDirectory
 *
 * Returns the appropriate location to place downloaded files.
 * (falls back to the Desktop)
 */ 
public function get downloadsDirectory():File {
   var downloadsDirectory:File;
 
   downloadsDirectory = File.userDirectory.resolvePath('Downloads');
   if (downloadsDirectory.isDirectory) return downloadsDirectory;
 
   downloadsDirectory = File.documentsDirectory.resolvePath('Downloads');
   if (downloadsDirectory.isDirectory) return downloadsDirectory;
 
   return File.desktopDirectory;
}

A few libraries (as3nativealertlib and AirAlert) claim to provide native alert dialogs to Flex/AIR—but each one emulates the UI, which isn’t quite ideal.

A workaround is to use the HTMLLoader class to get totally-native modal alerts laid out by the OS. This works by leveraging Flex/AIR’s integration of Webkit into the runtime. Calling the alert method of the window triggers an alert just as a web browser would.

var htmlInterface:HTMLLoader;
function alert(message:String):void {
   if (!htmlInterface) {
      htmlInterface = new HTMLLoader();
      htmlInterface.loadString('<html><head></head><body></body></html>');
   }
   try { htmlInterface.window.alert(message); }
   catch (e:Error) {}
}

Not exactly the cleanest of solutions… but it works! The only downside is the inability to set the title of the dialog.

After digging through old designs & projects and saying “holy crap, I had totally forgotten about” out loud more than once (even on things I had put 100+ hours into), I have decided to start writing again and keep a thorough log of problems, projects, and ideas that pop up. Writing has always helped defrag’ my head, so I’m going to do it. I have a feeling this was a past new years resolution of mine… but maybe I’ll stick to it this time? We’ll see.

Twitter / Tumblr / Path / Facebook haven’t been cutting it for me. What’s posted there has little value… if those services vanish, not much will be lost other than a window into the scatterbrain—which can be fun, but I need a little balance.

Copyright © 2012 – Brian Reavis. All rights reserved. The views expressed here are my own and do not necessarily reflect the views of COLOURlovers / Creative Market.