{ overwrite: true} not working for .createFile method. Why?

From what I can tell, I’ve followed the documentation exactly on how this should be done. Here’s an example from the Adobe documentation:

const newFile = await folder.createFile("examples.txt", { overwrite: true });
newFile.write("Hello, world!");

https://developer.adobe.com/xd/uxp/develop/reference/uxp/module/storage/#module-storage-folder-createfile

And here’s how I’ve written it in my code:

async function saveDocumentAsJPEGToFolder(current_image) {

    // note: users are only prompted once to use the folder-picker, at the start of each batch operation. the below happens in the background.
    const entry = await output_folder_to_use.createFile(`${app.activeDocument.title}_${current_image.name}.jpeg`, { overwrite: true });
    const outputFileToken = await fs.createSessionToken(entry);

    const save_image_as_JPEG = {
        _obj: "save",
        as: {
            _obj: "JPEG",
            extendedQuality: image_save_quality,
            matteColor: {
            _enum: "matteColor",
            _value: "none",
            },
        },
        copy: true,
        in: {
            _path: outputFileToken,
            _kind: "local"        
        }
    };

    try {
        result = await app.batchPlay([save_image_as_JPEG]);
    } catch (error) {
        console.error(`Error during image save`, error);
    }
}

However when there are images with identical names already saved into this folder? It does NOT overwrite them with the new images. Instead, the old images remain in there, and the new ones that should overwrite them never get saved into there.

Any clue why this is happening? Have I written something wrong in the code, or is there some additional parameter I need to specify in the batchPlay code to ensure the overwrite event occurs properly?

Thanks!

…It now appears to be working fine and having the intended effect. HOWEVER, there appears to be a bit of a delay, sometimes taking several seconds for the updated images to appear in the output folder.

I THINK this behavior is caused by either one of two things:

  1. Photoshop having a bit of a delay in completing the Save operation. This would cause me to therefore check the output folder prematurely, before the save operations are completed, and conclude “it didn’t overwrite the file!” – when, the operation was still in progress. ie, I’m impatient and moving fast, and the operations just weren’t finished yet.

  2. Windows potentially caching file info, for items in a given folder, and/or having a delay in updating the file info after an overwrite. For example, in testing saving at JPEG quality 12, then 1, perhaps it was storing the old file info and outputting that for the identical filenames, and maybe it takes a few seconds to update it.

Not sure which of these is the correct explanation. If I had to guess, probably the former. Also note, I’m not explicitly running this save operation via executeAsModal (it still lets me run it all the same to save the images – maybe because save operations execute as modal by default without requiring you to explicitly specify it?)

You could change PS settings to make saving a synchronous operation. Or you could listen for an event that tells that saving was finished and then continue.

1 Like