File.read is failing when trying to read rendered artboards

#1

Issue

I’m writing a plugin where I need to bake (create renditions of) the available artboards and
do some additional steps using the generated files.

I’m using application.createRenditions() and it all woks and generates the baked artboards.
The images are stored in the getTemporaryFolder().

Refer to the attached screenshot and log to see that the marked log line from the following code snippet is being printed and all meta information about the file seems correct.

The issue is that the next line calling file.read is failing silently:

  • no errors in the console;
  • there are no following log lines for the other files;
...
application.createRenditions(bakeTasks)
.then(() => {
    console.log('Baking done!');
    let filePromises =[];
    artboardArray.map(board => {
        filePromises.push(
            folder.getEntry(board.name + '.jpg')
        );
    });
    Promise.all(filePromises)
    .then(bakedFiles => {
        console.log('Opened files: ' + bakedFiles.length);
        let readPromises = [];
        bakedFiles.map(bakedFile => {
            // ########
            console.log(bakedFile.toString()); // this log gets printed
            // ########
            readPromises.push(
                bakedFile.read({format: formats.binary})
            );
        });
        ...
    }); 
});

Steps to reproduce

  1. application.createRenditions(…);
  2. file.read({format: formats.binary});

Expected Behavior

I was expecting to get the file content.

Actual Behavior

The execution seems to have just stopped without any error or warning.

Log

Files prepared: 3
Baking 3 artboards.
Baking done!
Opened files: 3
{“name”:“screenA.jpg”,“type”:“file”,“nativePath”:"/var/folders/7d/_vxltf1s3tdg_1f70mzny80m0000gn/T/D1E81694-5CB4-4AA7-8B13-6FABDC01B416/xxxxxx/screenA.jpg"}

0 Likes

#2

Hi @valado welcome to the community!
as per our documentation, file read will return a promise, so you will have to await like:

const data = await myNovel.read({format: formats.binary}); // 'data' is an ArrayBuffer
console.log("File is " + data.byteLength + " bytes long.");
0 Likes

#3

You may not see any console errors when thing go wrong if you’re not linking your Promises together in a chain (definitely some like readPromises and the Promise.all() result are not) and returning that entire chain to XD. It might be easier, as Steve mentioned, to use the await syntax instead of juggling Promises manually.

I think you can simplify this code a bunch though and that may make it easier to figure out what’s going wrong. The createRenditions() call gives you File objects for the results, so you don’t need those getEntry() calls. Fyi, you also don’t need to use both map() and push() – pick one or the other.

The “How to export a rendition” sample code may be helpful to look at. But at a quick glance, I think you could do something like this:

return application.createRenditions(bakeTasks)
.then(results => {
    console.log("Baking done!");
    let readPromises = results.map(result => {
        console.log("Reading " + result.outputFile.nativePath);
        return result.outputFile.read({format: formats.binary});
    };
    return Promise.all(readPromises);
}).then(readResults => {
    readResults.forEach(arrayBuffer => {
        console.log("File is " + arrayBuffer.byteLength + " bytes");
    });
});
0 Likes