# Opening all images in a PDF via Batch-play

**URL:** <https://forums.creativeclouddeveloper.com/t/opening-all-images-in-a-pdf-via-batch-play/7044>\
**Category:** UXP Plugin API\
**Tags:** batch-play\
**Created:** [October 31, 2023, 12:32pm UTC](https://forums.creativeclouddeveloper.com/t/opening-all-images-in-a-pdf-via-batch-play/7044 "2023-10-31T12:32:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![LorenzoF](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@LorenzoF](https://forums.creativeclouddeveloper.com/u/LorenzoF)\
**Post date:** [October 31, 2023, 12:32pm UTC](https://forums.creativeclouddeveloper.com/t/opening-all-images-in-a-pdf-via-batch-play/7044/1 "2023-10-31T12:32:55Z")

</div>

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):

```auto
// ...
  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.

```auto
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:

 ![Screenshot 2023-10-31 at 12.26.11](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/b/b2cfce5ea3b3badedf9713202067710cfc5cf648.png)

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?
