# Is This The Way You Do It For API 2?

**URL:** <https://forums.creativeclouddeveloper.com/t/is-this-the-way-you-do-it-for-api-2/4047>\
**Category:** UXP Plugin API\
**Created:** [December 17, 2021, 5:00pm UTC](https://forums.creativeclouddeveloper.com/t/is-this-the-way-you-do-it-for-api-2/4047 "2021-12-17T17:00:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![IanBarber](https://avatars.discourse-cdn.com/v4/letter/i/94ad74/32.png) [@IanBarber](https://forums.creativeclouddeveloper.com/u/IanBarber)\
**Post date:** [December 17, 2021, 5:00pm UTC](https://forums.creativeclouddeveloper.com/t/is-this-the-way-you-do-it-for-api-2/4047/1 "2021-12-17T17:00:49Z")

</div>

Scenario:  
Button click which then runs batchPlay Code

```auto
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);
  
}

});

```

---

<div class="post-metadata">

**Author:** ![pdophoto](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pdophoto/32/1304_2.png) [@pdophoto](https://forums.creativeclouddeveloper.com/u/pdophoto)\
**Post date:** [December 19, 2021, 7:23pm UTC](https://forums.creativeclouddeveloper.com/t/is-this-the-way-you-do-it-for-api-2/4047/2 "2021-12-19T19:23:59Z")

</div>

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

```auto
//##################################################

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()});

```

---

<div class="post-metadata">

**Author:** ![Trasatti](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@Trasatti](https://forums.creativeclouddeveloper.com/u/Trasatti)\
**Post date:** [May 1, 2022, 10:17pm UTC](https://forums.creativeclouddeveloper.com/t/is-this-the-way-you-do-it-for-api-2/4047/3 "2022-05-01T22:17:26Z")

</div>

> [@pdophoto](#):
>
> `await suspendHistory("2D Gradient", doIt_1);`

Thank you @pdophoto , it works perfectly

---

<div class="post-metadata">

**Author:** ![photonic](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/photonic/32/2513_2.png) [@photonic](https://forums.creativeclouddeveloper.com/u/photonic)\
**Post date:** [December 10, 2022, 11:57pm UTC](https://forums.creativeclouddeveloper.com/t/is-this-the-way-you-do-it-for-api-2/4047/4 "2022-12-10T23:57:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mohamad](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/mohamad/32/2334_2.png) [@Mohamad](https://forums.creativeclouddeveloper.com/u/Mohamad)\
**Post date:** [February 24, 2023, 1:32pm UTC](https://forums.creativeclouddeveloper.com/t/is-this-the-way-you-do-it-for-api-2/4047/5 "2023-02-24T13:32:33Z")

</div>

Hello. Can you help me with her job, her job as my extra pc is not responding.  
…  
Regards 🙏  
Mohamed Fathy.
