Loading random image

Hey all,

I’m working on a plugin that randomly loads a texture from a preset sub-folder in my plugin-folder. It’s all working correctly, but every now and then I get an error that says ‘file with an ID of (insert random number) could not be loaded’. It happens for images that in other sessions load perfectly.

Anyone know how I can get it to assign a random ID? The strange thing is the number it mentions in the alert does not correspond to the ID I get when I manually drag and place the texture myself. I also tried assigning an _ID myself but that doesn’t seem to change anything either.

Here’s the code:

const texturePickerValue = document.getElementById('texturePicker').value;
const pluginFolder = await fs.getPluginFolder();
const texturesFolder = await pluginFolder.getEntry('textures/textures/' + texturePickerValue);
const entries = await texturesFolder.getEntries({ types: ["jpg", "png", "jpeg", "gif"] });

if (entries.length === 0) {
    alert('No images found in textures folder.');
    return;
}

const randomIndex = Math.floor(Math.random() * entries.length);
const randomImageEntry = entries[randomIndex];

let token = fs.createSessionToken(randomImageEntry);
let result = await require('photoshop').action.batchPlay([{
    _obj: "placeEvent",
    null: {
        _path: token, // Hidden file-path, randomized in randomImageEntry above
        _kind: "local",
    },