Is This The Way You Do It For API 2?

Scenario:
Button click which then runs batchPlay Code

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

   await exeModal(testfunction, {
      "commandName": "Please Wait" // this shows in the progress bar if displayed
   });

async function testfunction(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": "History State Message", // this show  in history
            "target": [{
               _ref: "document",
               _id: documentID
            }]
         }
      });

/* 
Do stuff here. 
It can be batchPlay or any other javascript code
*/


await hostControl.resumeHistory(suspensionID);
  
}

});

Hi Ian,
It does work.
I have made a function since I need it a few times in my plugin and it works perfectly.

//##################################################

const suspendHistory = async (theSuspendName, functionName) => {await exeModal(doStuff, {"commandName": "Please Wait"});

 async function doStuff(executionContext){
    let hostControl = executionContext.hostControl;
		let documentID = await app.activeDocument._id;
    // Suspend history state on the target document
    let suspensionID = await hostControl.suspendHistory({
        "historyStateInfo": {
            "name": theSuspendName,
            "target": [ {_ref: "document", _id: documentID}]
        }
	 });
//	 console.log(functionName);
	 await exeModal( functionName,	 );
	 
   await hostControl.resumeHistory(suspensionID);
	 };
};

const doIt = async () => {
	await suspendHistory("2D Gradient", doIt_1);
};

document.getElementById("btn_DoIt").addEventListener("click", async function () {await doIt()});
2 Likes

Thank you @pdophoto , it works perfectly

This works exactly how I expected, very amazing job thanks for sharing <3 !

Hello. Can you help me with her job, her job as my extra pc is not responding.

Regards :pray:
Mohamed Fathy.