Skip to content

Calling Generator Plugins from ExtendScript

John Peterson edited this page Oct 29, 2013 · 5 revisions

To invoke a generator plugin from a Photoshop ExtendScript (JSX) script, the simplest method is to invoke the Generator plugin's menu method. On the ExtendScript (Photoshop) side, this looks like:

var generatorDesc = new ActionDescriptor();
generatorDesc.putString (app.stringIDToTypeID ("name"), "test");
// Example of additional parameter passed to the node.js code:
generatorDesc.putString (app.stringIDToTypeID ("sampleAttribute"), "moreInfo" );
var returnDesc = executeAction (app.stringIDToTypeID ("generateAssets"), generatorDesc, DialogModes.NO);

This sample invokes the generator plugin test. An additional parameter (under the tag sampleAttribute) is passed into the plugin. On the Generator side, the node.js code for handling the event looks like this:

function handleGeneratorMenuClicked(event) {
    // Ignore changes to other menus
    var menu = event.generatorMenuChanged;
    if (!menu || menu.name !== MENU_ID) {
        return;
    }

    var startingMenuState = _generator.getMenuState(menu.name);
    console.log("Menu event %s, starting state %s", stringify(event), stringify(startingMenuState));
    
    // Additional parameter passed in from the ExtendScript side:
    var sampleAttr = event.generatorMenuChanged.sampleAttribute;
    sendJavascript("alert('Got a menu event to respond to, with sample attribute: " + sampleAttr + "');");
}

Note the reference to event.generatorMenuChanged.sampleAttribute to retrieve the additional attribute passed from the script. A complete set of the ExtendScript and corresponding Generator plugin is here.