Opening PDF files with UXP

Hi there,

First off. I’m a beginner learning by doing and reading this awesome forum. Not sure if this question is in the right place. I’m working on a plugin and most of what I want to achieve has been accomplished. There’s just one thing that I can’t seem to get right.

I’m trying to figure out how to open a PDF file with basically the “PDFOpenOptions” using UXP. I’ve tried the entry.open way but this results in the PDF being opened with the latest settings. Resulting in wrong aspect ratios and resolutions.

I figured I could use batchPlay to insert the right values but this doesn’t work. I’ve used Alchemist to get the open object. I’ve made a function, but when it gets to the open part everything stops. I probably am doing something wrong.

async function batchOpenCanvas(name,res,width,height,path,id) {
  console.log("Are we here?");
  const batchPlay = require('photoshop').action.batchPlay;
  const result = await batchPlay(
  [
    {
      "_obj": "open",
      "as": {
           "_obj": "PDFGenericFormat",
           "name": name,
           "crop": {
              "_enum": "cropTo",
              "_value": "trimBox"
           },
           "resolution": {
              "_unit": "densityUnit",
              "_value": res
           },
           "mode": {
              "_enum": "colorSpace",
              "_value": "RGBColor"
           },
           "depth": 8,
           "antiAlias": true,
           "width": {
              "_unit": "pixelsUnit",
              "_value": width
           },
           "height": {
              "_unit": "pixelsUnit",
              "_value": height
           },
           "constrainProportions": true,
           "suppressWarnings": true,
           "reverse": true,
           "selection": {
              "_enum": "pdfSelection",
              "_value": "page"
           },
           "pageNumber": 1
        },
        "null": {
           "_path": path,
           "_kind": "local"
        },
        "documentID": id,
        "_isCommand": true,
        "_options": {
           "dialogOptions": "dontDisplay"
        }
    }
  ],{
    "synchronousExecution": false,
    "modalBehavior": "fail"
 });
 console.log("Did we make it?");
 return result;
}

Thanks in advance!

Some questions to narrow down the error:

  • Did you log what’s in result? BatchPlay errors are written inside the returned object
  • Did you try to leave out "documentID": id
  • Are you passing a path or a token?
1 Like

Hi Simon,

Thanks for taking the time to answer my question.

  1. I logged the result, but still nothing’s logged after the first console.log. Is there maybe another way to catch the error from a batchPlay that I don’t know about?

  2. I left out the id, but still nothing’s logged after the first console.log. I created a function that looks at all the doc id’s and finds a different number. I figured as long as it’s not the same as other open documents it would be fine.

  3. I’m passing on a path that I retrived from the localFileSystem via entry.nativePath. As far as I know that is the local path needed to retrive the document. I’ve read a thread where you did something with tokens. I’ll give that a go.

Update: I passed a token instead of a path and that caused a really tiny version of the PDF to open. So I’m a step closer to a solution.

Thanks!

Ok, I scrambled the input values what caused the tiny PDF. It’s working now! Just nothing is logged after the fact so I guess the batchPlay breaks the function?

  1. You can use try/catch to catch any javascript errors maybe. BatchPlay itself doesn’t throw errors usually, instead it should return the error message. But if there’s a syntax error or similar, it might not even execute in the first place.
  2. IDs will be assigned by Photoshop, so I’m pretty sure you don’t need to do all of that. The ID you manually assigned probably has zero impact and is just ignored.
  3. File-access in UXP always requires a token :wink:

Yes, seems like your batchPlay is still breaking somewhere in between.

1 Like

Thanks again Simon,

  1. try/catch gives me nada. It’s still opening the file so I’m not complaining.
  2. You’re right. Me tossing around ID’s has absolutely no effect on the outcome. It works just fine without a document ID.
  3. Thanks for the lesson! :slight_smile:

Still have to figure out why it’s breaking though.

Update: It seems the whole figuring out an ID and passing it on to the function was the reason for the function breaking. Omitting that made it work perfectly.

You are a life saver, Herr Henke!

You’re welcome! Sometimes less code works better😃

1 Like