# Opening files from path

**URL:** <https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783>\
**Category:** UXP Plugin API\
**Tags:** uxp, javascript\
**Created:** [September 7, 2023, 5:39pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783 "2023-09-07T17:39:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![M3mber](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/m3mber/32/3216_2.png) [@M3mber](https://forums.creativeclouddeveloper.com/u/M3mber)\
**Post date:** [September 7, 2023, 5:39pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783/1 "2023-09-07T17:39:12Z")

</div>

Greetings,

I have come across an issue that I don’t know how to solve. I am trying to make a plugin, that will enable me to first select a file, I will get the file path and then open the file in photoshop with that said path. I managed to make a file dialog that gives me the path to the documents I want. The part that I am struggling with is the actual opening of the files from said path. I used alchemist to get some of the code and I can’t understand why parts aren’t working. I am not getting any error. Could it be that I forgot to put something in manifest? or is it just the code?

Here is my code:

```auto
async function runModalFunction() {
    console.log("start");
    await selectFile();
    await executeAsModal(actionCommands, {"commandName": "Action Commands"});
}
async function actionCommands() {
    console.log("actionCommands")
    const result = await batchPlay(
        [
            {
                _obj: "open",
                null: {
                    _path: startFilePath,
                    _kind: "local"
                },
                documentID: 78,
                template: false,
                _isCommand: true,
                _options: {
                    dialogOptions: "dontDisplay"
                }
            }
        ],
        {}
    );
    console.log("end of action commands")
}

```

As for how I get that said path:

```auto
const fs = window.require("uxp").storage.localFileSystem;
    const file = await fs.getFileForOpening();
    const token = await fs.createPersistentToken(file);

    const fileObject = {
        name: file.name,
        path: file.nativePath,
        token: token,
        entry: file,
    };
    const finalPath = fileObject.path;
    console.log(finalPath)
    startFilePath = finalPath;

```

Finally, here are my permissions in manifest:

```auto
"requiredPermissions": {
    "allowCodeGenerationFromStrings": true,
    "launchProcess": {
      "schemes": [
        "http",
        "https"
      ],
      "extensions": [
        ".svg",
        ".png"
      ]
    },
    "localFileSystem" : "request",
    "clipboard": "readAndWrite",
    "webview": {
      "allow": "yes",
      "domains": [
        "https://*.adobe.com",
        "https://*.google.com"
      ]
    }
  },

```

---

<div class="post-metadata">

**Author:** ![dotproduct](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dotproduct](https://forums.creativeclouddeveloper.com/u/dotproduct)\
**Post date:** [September 7, 2023, 10:01pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783/2 "2023-09-07T22:01:54Z")

</div>

Is there a particular reason you use batchPlay to open a file instead of the API? It’s 10+ lines of code with batchPlay vs. two lines using the API:

```auto
// Open a file given entry (example from the documentation)
let entry = await require('uxp').storage.localFileSystem.getFileForOpening()
const document = await app.open(entry);

```

---

<div class="post-metadata">

**Author:** ![sttk3](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@sttk3](https://forums.creativeclouddeveloper.com/u/sttk3)\
**Post date:** [September 7, 2023, 10:39pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783/3 "2023-09-07T22:39:56Z")

</div>

My recollection is that you need to specify `"interactive": true` when using the file select dialog in executeAsModal.

```auto
await executeAsModal(actionCommands, {"commandName": "Action Commands", "interactive": true});

```

#### Additional edits:

- As dotproduct pointed out, selectFile was outside its executeAsModal. The `"interactive": true` is for its higher-level executeAsModal.
- Is the `window.` necessary here?

```auto
const fs = window.require("uxp").storage.localFileSystem;

```

---

<div class="post-metadata">

**Author:** ![dotproduct](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dotproduct](https://forums.creativeclouddeveloper.com/u/dotproduct)\
**Post date:** [September 7, 2023, 10:50pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783/4 "2023-09-07T22:50:20Z")

</div>

From what I can tell from @M3mber’s code it looks like he’s doing the file selection **before** running executeAsModal. Again, apart from the fact that you can achieve all of this with 2 lines of code ( 😉), @M3mber, can you confirm that when calling your function actionCommands(), your variable startFilePath contains something reasonable? I don’t see where you declare this, though it looks like a global variable. Might be a good idea to wrap your batchPlay call in a try/catch block to get some debug info.

---

<div class="post-metadata">

**Author:** ![M3mber](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/m3mber/32/3216_2.png) [@M3mber](https://forums.creativeclouddeveloper.com/u/M3mber)\
**Post date:** [September 8, 2023, 7:08pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783/5 "2023-09-08T19:08:18Z")

</div>

So, the main reason for me using batchplay is because of my lack of understanding. My main objective is to iterate over files in a directory one by one after a click of a button. Open them as well. Now, batchplay seemed more intuitive with the paths. I struggle to understand how to edit the returning object Promise\<File|Array\>. If you could give some pointers on that then that would be great!

---

<div class="post-metadata">

**Author:** ![M3mber](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/m3mber/32/3216_2.png) [@M3mber](https://forums.creativeclouddeveloper.com/u/M3mber)\
**Post date:** [September 8, 2023, 7:10pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783/6 "2023-09-08T19:10:12Z")

</div>

startPath contains a file path that was created from the entry of getFileOpening(). As can be seen here in this code:

```auto
const fs = window.require("uxp").storage.localFileSystem;
    const file = await fs.getFileForOpening();
    const token = await fs.createPersistentToken(file);

    const fileObject = {
        name: file.name,
        path: file.nativePath,
        token: token,
        entry: file,
    };
    const finalPath = fileObject.path;
    console.log(finalPath)
    startFilePath = finalPath;

```

---

<div class="post-metadata">

**Author:** ![dotproduct](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dotproduct](https://forums.creativeclouddeveloper.com/u/dotproduct)\
**Post date:** [September 8, 2023, 7:52pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783/7 "2023-09-08T19:52:55Z")

</div>

With the danger of sounding like a broken record ( 😁), I would still strongly encourage you to use the UXP API for this. Opening a folder of files is something that can be easily done with just a few lines of code. In the age of UXP the idea would be to use the API as much as possible (at least in my humble opinion 😉). It’s more readable, more maintainable and easier to debug. You can still use BatchPlay for functionality that isn’t easily available in the API. I’d suggest looking at the docs (in particular about file [system access](https://developer.adobe.com/photoshop/uxp/2022/uxp-api/reference-js/Modules/uxp/Persistent%20File%20Storage/)). It goes over ways to access an entire folder and how to access its files. Then it’s as simple as iterating over these files.

I’m not sure what you mean by batchPlay being more intuitive with the paths. I would humbly claim the opposite. Actually ‘\_path’ in the batchPlay’s descriptor needs to be a token (which is a bit misleading). Maybe you can try passing that instead of the native path.

---

<div class="post-metadata">

**Author:** ![DavideBarranca](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/davidebarranca/32/1523_2.png) [@DavideBarranca](https://forums.creativeclouddeveloper.com/u/DavideBarranca)\
**Post date:** [September 8, 2023, 8:01pm UTC](https://forums.creativeclouddeveloper.com/t/opening-files-from-path/6783/8 "2023-09-08T20:01:00Z")

</div>

> With the danger of sounding like a broken record ( 😁), I would still strongly encourage you to use the UXP API for this.

👆🏻💯%
