# Document Save not always fired correctly

**URL:** <https://forums.creativeclouddeveloper.com/t/document-save-not-always-fired-correctly/4561>\
**Category:** UXP Plugin API\
**Created:** [April 22, 2022, 11:21am UTC](https://forums.creativeclouddeveloper.com/t/document-save-not-always-fired-correctly/4561 "2022-04-22T11:21:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ferodra](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@Ferodra](https://forums.creativeclouddeveloper.com/u/Ferodra)\
**Post date:** [April 22, 2022, 11:21am UTC](https://forums.creativeclouddeveloper.com/t/document-save-not-always-fired-correctly/4561/1 "2022-04-22T11:21:34Z")

</div>

Hey Adobe community!

I recently wrote myself a Photoshop UXP plugin, which basically saves the document/canvas to an incrementally named file every X seconds.  
The document save method is called correctly by my code from what i can tell, but it occurs more and more often that it simply doesn’t save anything. Because of that, i’m left with gaps in the incremental file naming and it generally is causing missing files due to that. I unfortunately couldn’t come up with any method to reproduce this bug, but it always leaves out exactly one save before working again. I’m pretty sure this has something to do with the way i handled (or didn’t really handle) the async calls, but i’m not sure about that.

If required, here’s the entire sourcecode of the plugin:

> **[const app = require("photoshop").app;const currentDocument = app.activeDocumen...](https://pastebin.com/rYUkd8rz)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

Below’s the part that’s causing issues.  
Thanks in advance for every answer!

```auto
async function SaveSnapshot() {
	TimeLeft = NextSaveTime - GetTime();

	UpdateTimerText();

	if (!isEnabled || !isDirty || TimeLeft > 0) {return;}
	
	// Prevent new save from being started to avoid missing single files when the saving process takes longer than the timer itself
	clearInterval(IntervalTimer);
	
	CurrentIndex = CurrentIndex + 1;
	const file = await userFolder.createFile(`${DocumentName}_${CurrentIndex}.${FileFormat_Selected.toLowerCase()}`);
	console.log("Writing . . .", `${DocumentName}_${CurrentIndex}.${FileFormat_Selected.toLowerCase()}`, parseInt(SaveQuality));
	
	const Snapshot = await app.activeDocument.save(file, { embedColorProfile: true, quality: parseInt(SaveQuality)});
	
	isDirty = false;
	UpdateIntervalTimer();
}

function UpdateIntervalTimer() {
	if (!isEnabled) {return;}
	
	if (IntervalTimer) {
		clearInterval(IntervalTimer);
		IntervalTimer = null;
	}

	console.log("Scheduled next update for:", (SaveInterval) + GetTime())
	console.log("Save Interval:", SaveInterval);
	NextSaveTime = SaveInterval + GetTime();

	// Start Timer to call snapshot func
	IntervalTimer = setInterval(saveSnapshot, 500);
}

```

---

<div class="post-metadata">

**Author:** ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)\
**Post date:** [April 26, 2022, 10:38pm UTC](https://forums.creativeclouddeveloper.com/t/document-save-not-always-fired-correctly/4561/2 "2022-04-26T22:38:22Z")

</div>

Have you identified any patterns to the break?

One thought:

I don’t know if `save` would work if Ps is in a modal state – such as modifying an image’s crop. I also don’t know if this would raise an exception or not, but it’s worth looking at the developer console logs to see if any errors are getting raised – that could help pinpoint the problem.

---

<div class="post-metadata">

**Author:** ![Ferodra](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@Ferodra](https://forums.creativeclouddeveloper.com/u/Ferodra)\
**Post date:** [April 27, 2022, 4:50pm UTC](https://forums.creativeclouddeveloper.com/t/document-save-not-always-fired-correctly/4561/3 "2022-04-27T16:50:33Z")

</div>

That apparently was the right guess - it simply didn’t do anything when any form of modal is open.  
Thank you so much!

For anyone curious, this is how i fixed it. This essentially gives you a variable to determine the current modal state of photoshop.

In terms of the save method, i noticed that it only works with api version 1. Was pulling my hair out when i upgraded to 2 while debugging this and it suddenly didn’t work anymore

```auto
var isModalOpen = false;

function UpdateModalState(state) {
	if (state == "enter") {
		isModalOpen = true;
	} else {
		isModalOpen = false;
	}
}

listener = (e, d) => {
		if (e == "modalStateChanged") {
			UpdateModalState(d.state._value);
		}
	}
	require('photoshop').action.addNotificationListener([
		{ event: "modalStateChanged" },
	], listener);

```
