Opening all images in a PDF via Batch-play

Hi everyone,

First time for me posting here, I just started delving into the Photoshop UXP documentation. I decided to get my feet wet by creating a simple plugin that:

1- takes a PDF and opens all images (not pages)
2- converts the PDF images to AdobeRGB
3- saves the image as JPEG in a folder specified by the user

Problem is I do not understand how to have batchplay opening all the pdf images properly. Currently I am doing it quite roughly with a simple for loop (could’ve used a while loop there, doesn’t matter really for my problem):

// ...
  for (i = 1; i <= 100; i++) {
    let result = await openPDFpage(i, token, batchPlay)
    if (result[0]._obj === "error") {
      break
    }
    await convertToAdobeRGB(batchPlay)
    let newFileToken = await getSaveFileToken(i, folderEntry)
    await saveJpeg(newFileToken, batchPlay)
    app.activeDocument.closeWithoutSaving()
  }
// ...

where in openPDFpage() I simply have my batchplay with the “pageNumber” property increasing for each iteration, as to open all the images until I get a “error” string in the batchplay result (because I’ve run out of images to open) and the for loop above breaks.

async function openPDFpage(pageNum, token, batchPlay) {
  let result;
  try {
    result = await batchPlay(
      [{
        "_obj": "open",
        "as": {
          "_obj": "PDFGenericFormat",
          "crop": {
            "_enum": "cropTo",
            "_value": "trimBox"
          },
          "mode": {
            "_enum": "colorSpace",
            "_value": "RGBColor"
          },
          "depth": 8,
          "antiAlias": true,
          "suppressWarnings": false,
          "reverse": true,
          "selection": {
            "_enum": "pdfSelection",
            "_value": "image"
          },
          "pageNumber": pageNum
        },
        "null": {
          "_path": token,
          "_kind": "local"
        },
        "_isCommand": true,
        "_options": {
          "dialogOptions": "dontDisplay"
        }
      }],
      {
        "synchronousExecution": false
      }
    )
  } catch (e) {
    console.log(e)
  }

  return result
}

This trick works and the images are correctly converted and saved. The problem is when I reach the last iteration and batchplay tries to open an image with pageNumber “out of bounds”, Photoshop simply brings up the PDF open dialog box:

I assume this is the default behaviour of batchplay when trying to access an invalid pageNumber. I would like to avoid this dialog box from showing at the end of the process. I guess the best way would be to somehow know beforehand how many images are in the pdf, or maybe some other approach I’m not considering. Can someone help me out?