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:
Below’s the part that’s causing issues.
Thanks in advance for every answer!
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);
}