API 2 open file

I can’t seem to get the open file command to work in API 2. The code below was working fine in API 1 but no longer works. It shows the open file dialogue but when a file is selected nothing happens.

What needs to be changed to get files to open again, and is there a place where there is updated documentation for all the things that need to be changed to get API 1 code to work again?

async function openExistingTemplate() {

   const fs = require("uxp").storage.localFileSystem;
   const file = await fs.getFileForOpening();

   if (file) {
     await app.open(file);
  } else {
    return;
 }
}

Needs to go behind executeAsModal – see https://www.adobe.io/photoshop/uxp/2022/ps_reference/media/executeasmodal/

I am aware that we require executeAsModal now, but I still cannot get a simple function like opening a document to work. My script hangs or waits indefinitely with no error, so I’m clearly doing it wrong.
Can someone please help we with the briefest of scripts of the exact code I need to open a file called ‘template1.psd’ sitting in the plugin root folder.
Thanks in advance.

Hi @Symo470 , this code works for me, does it work for you?:

const app = require("photoshop").app;
const fs = require("uxp").storage.localFileSystem;

function executeAsModal_selectFileAndOpen() {
  async function openExistingTemplate() {
    const file = await fs.getFileForOpening();
    console.log(file);
    if (file) {
      await app.open(file);
    } else {
      return;
    }
  }

  try {
    require("photoshop").core.executeAsModal(openExistingTemplate);
  } catch (e) {
    if (e.number == 9) {
      showAlert(
        "executeAsModal was rejected (some other plugin is currently inside a modal scope)"
      );
    } else {
      console.log("exception!");
      // This case is hit if the targetFunction throws an exception
    }
  }
}

Hey @AndresLP appreciate your response.
No, it does not work. Even the errors aren’t being logged on the console. So, I am thinking my environment is not set up correctly. Could I have an error in the manifest.json? What else could it be?
Thanks in advance.

Hi @Symo470 - what manifest version are you on? If its v5+, then you’ll need to explicitly declare which file extensions the plugin can open on your behalf.

Hi @Symo470 , first thing I would check is that you have included in the manifest.json an allowed value in the “localFileSystem” property of the “requiredPermissions” object. If not, just set it as “fullAccess” and check if that works for now. Something like this:

  "requiredPermissions": {
    "localFileSystem": "fullAccess"
},

About this:

Even the errors aren’t being logged on the console.

Add a “try … catch” statement where your code is breaking and do a console.log of the error, that should show in the debugger console.

More info:

https://developer.adobe.com/photoshop/uxp/2022/guides/uxp_guide/uxp-misc/manifest-v5/#local-filesystem

1 Like