Calling Multiple Functions From A Single Button

Thanks to @ddbell I am now gathering information on how to migrate some plugins over to API 2.

I wanted to ask a question about calling multiple functions from a single button.

Here is a scenario I have…
On the plugin, I have 5 buttons, each doing their own thing.
Here is an example of the code inside one of the buttons

document.getElementById('button1').addEventListener('click', async () => {

    await exeModal(test1, {
        "commandName": "Doing things now" //<= this shows on progress bar I think

    async function test1(executionContext) {

        let hostControl = executionContext.hostControl;

        // Get an ID for a target document
        let documentID = await app.activeDocument._id;

        // Suspend history state on the target document
        let suspensionID = await hostControl.suspendHistory({
            "historyStateInfo": {
                "name": "Test 1",
                "target": [{
                    _ref: "document",
                    _id: documentID

        /*Do stuff here*/
        // My BatchPlay Goes here...

        await hostControl.resumeHistory(suspensionID);


My Question

From another button, if I wanted to run the BatchPlay stuff in all of the 5 buttons would I have to copy all the 5 BatchPlay’s into the button which is going to run them all.

In API 1 I used to simply do something like this…


I don’t really understand what you are asking. Each button needs it’s own event listener. Then you just put whatever you want inside each event listener. It looks like you are doing it correctly.

I will try and rephrase the question as it can be difficult to put it in a post sometimes…

//this is from a button called runAll.
/*Do stuff here*/
        // My BatchPlay Goes here...
      // How can I call the BatchPlay in button1 from here
     // How can I call the BatchPlay in button2 from here
     // How can I call the BatchPlay in button3 from here

    // Or do I have to place all the BatchPlay's from button 1,2 3 here
   // which would mean the BatchPlays from button 1,2,3 are duplicated ?

        await hostControl.resumeHistory(suspensionID);

There are probably a dozen ways to do it. I have 5 or 6 different methods I use for the executeAsModal.

You can obviously create a different function for each button.

If you need one big function that just plays a bit differently per button then you could have the event listener functions set variables prior to running the executeAsModal. Then they would each set the variable differently but then run the same larger function. You would need to put the shared function outside of the event listener function so that all event listeners could use the shared function. If you leave it nested then only the one event listener can use it. In the “do stuff here” section have a switch or if/thens to play different batchPlays based on the variable.

This has clarified my thoughts. I shall now think of the best way to approach what I want. I was trying to eliminate duplicating code really so it might be better to have individual functions which are available from anywhere