Skip to content

Calling Generator Plugins from ExtendScript

Joel Brandt edited this page Jul 24, 2014 · 5 revisions

To invoke a Generator plug-in from a Photoshop ExtendScript (JSX) script (e.g. from the ExtendScript Toolkit or in an HTML5 panel), the simplest method is to trigger a Generator menu event. On the ExtendScript (Photoshop) side, this can be done with the following code:

var generatorDesc = new ActionDescriptor();
generatorDesc.putString (app.stringIDToTypeID ("name"), "my-menu-name");
// 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);

Executing this code will do the following:

  1. If Generator is not currently enabled, it will enable Generator and start the built-in Generator process
  2. Once Generator is launched, it will trigger a menu event with a menu name of "my-menu-name".
  3. It also passes an additional parameter (under the tag sampleAttribute) to Generator.

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 !== "my-menu-name") {
        return;
    }

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

// handleGeneratorMenuClicked needs to be bound to "generatorMenuChanged events" with
//     self.onPhotoshopEvent("generatorMenuChanged", handleGeneratorMenuClicked)

Note the reference to event.generatorMenuChanged.sampleAttribute to retrieve the additional attribute passed from the script.