# Read currently opened images

**URL:** <https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323>\
**Category:** Photoshop\
**Created:** [February 21, 2022, 5:18am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323 "2022-02-21T05:18:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Willa](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@Willa](https://forums.creativeclouddeveloper.com/u/Willa)\
**Post date:** [February 21, 2022, 5:18am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/1 "2022-02-21T05:18:45Z")

</div>

My plugin contains lut files (.cube) and I used fs.readfilesync for rendering previews in panel. How to integrate reading files with out  
require(‘uxp’).storage.localFileSystem.getFileForOpening() ?  
My goal is getting preview of currently opened image (also in .raw formats) like it is done in com.adobe.ccx.start extension

---

<div class="post-metadata">

**Author:** ![amandah](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/amandah/32/1730_2.png) [@amandah](https://forums.creativeclouddeveloper.com/u/amandah)\
**Post date:** [February 22, 2022, 8:34pm UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/2 "2022-02-22T20:34:57Z")

</div>

Hi Willa - welcome to the Creative Cloud developer ecosystem! I’m assuming you’re trying to circumvent the file picker part of the workflow, and open a file with a known path _without_ any user interaction. Unfortunately, UXP does not allow third parties to open files by just providing a path and requires user input for any file when you used `getFileForOpening()`.

Instead, if your file is in your **data folder** , you should be able to get a token for it using

```auto
const fs = require('uxp').storage.localFileSystem;
const df = await fs.getDataFolder();
const token = df.getEntry('filename.cube')

```

or **plugin folder** :

```auto
const fs = require('uxp').storage.localFileSystem;
const df = await fs.getPluginFolder();
const token = df.getEntry('filename.cube')

```

Here’s the corresponding [documentation](https://developer.adobe.com/photoshop/uxp/2022/uxp/reference-js/Modules/uxp/Persistent%20File%20Storage/FileSystemProvider/#getpluginfolder). As long as you’re within the data or plugin folder sandbox, you can access/open files in there without user interaction.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Willa](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@Willa](https://forums.creativeclouddeveloper.com/u/Willa)\
**Post date:** [February 27, 2022, 6:39am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/3 "2022-02-27T06:39:21Z")

</div>

Hi, Amandah,

Yes, I definitli trying this hook.

Thanks for clarifying!

---

<div class="post-metadata">

**Author:** ![Blaz](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/blaz/32/2162_2.png) [@Blaz](https://forums.creativeclouddeveloper.com/u/Blaz)\
**Post date:** [May 16, 2022, 11:56pm UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/4 "2022-05-16T23:56:01Z")

</div>

Hey Amandah! This helps heaps! Any idea how you could then pass the `token` object to a batch script?

I’m trying to run a Displacment filter but need to reference a displacement map .PSD saved in the plugin folder. Kinda like below. But I feel like my scripts are not getting the `nativePath` in time.

```auto
async function action1() {
    const fs = require('uxp').storage.localFileSystem;
    const df = await fs.getPluginFolder();
    const token = df.getEntry('Map1.psd');
    const map = await token.nativePath;
    let result;
    let command = [
        {
            "_obj": "displace",
            "horizontalScale": 50,
            "verticalScale": 50,
            "displacementMap": {
                "_enum": "displacementMap",
                "_value": "stretchToFit"
            },
            "undefinedArea": {
                "_enum": "undefinedArea",
                "_value": "repeatEdgePixels"
            },
            "displaceFile": {
                "_path": map,
                "_kind": "local"
            },
            "_isCommand": true,
            "_options": {
                "dialogOptions": "dontDisplay"
            }
        }
    ]
    app.showAlert(token);
    result = await psAction.batchPlay(command, {});
}
async function runModalFunction() {
    await require("photoshop").core.executeAsModal(action1, {"commandName": "Action Commands"});
}
await runModalFunction();

```

---

<div class="post-metadata">

**Author:** ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)\
**Post date:** [May 17, 2022, 6:01am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/5 "2022-05-17T06:01:21Z")

</div>

I think with BP you have to pass the `token` itself instead of `map` path. Eg. `"_path": token`. At least that’s what I saw somewhere here, but actually didn’t have to deal with this myself yet

---

<div class="post-metadata">

**Author:** ![Blaz](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/blaz/32/2162_2.png) [@Blaz](https://forums.creativeclouddeveloper.com/u/Blaz)\
**Post date:** [May 17, 2022, 10:50am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/6 "2022-05-17T10:50:54Z")

</div>

Yeh, think I did try that, I also saw a similar post here somewhere where they passed the `token` object, but couldn’t get it working 😔

---

<div class="post-metadata">

**Author:** ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)\
**Post date:** [May 17, 2022, 7:20pm UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/7 "2022-05-17T19:20:18Z")

</div>

Got to convert the token from `getEntry` to a session token using `createSessionToken`. That should get it working.

But you’ve got one other issue: `getEntry` is asynchronous. So you really need to do this:

```auto
const token = await df.getEntry("Map1.psd");

```

— otherwise you’re just getting the `Promise<Entry>` wrapper, which is _not_ what you want in this case. `nativePath` does not exist on `Promise`, but it does exist on `Entry`.

Also, accessing `nativePath` is synchronous, and so you don’t need `await token.nativePath` – you can drop the `await` there. (It will do no _harm_ to await, but it’s not doing anything useful either.)

---

<div class="post-metadata">

**Author:** ![Blaz](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/blaz/32/2162_2.png) [@Blaz](https://forums.creativeclouddeveloper.com/u/Blaz)\
**Post date:** [May 18, 2022, 1:41am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/8 "2022-05-18T01:41:02Z")

</div>

**OMG thank you so much!**

I’ve literally spent 4 days trying to figure this one out (_when designers try and code ahahahah_)

30min of trying to work it out but now I got it working it makes a lot of sense, with your direction.

For the purposes of others that might learn from this, full code below and my understanding of how it works.

**Full Code**

```auto
// Global constants
const app = require('photoshop').app;
const psAction = require("photoshop").action;
const fs = require('uxp').storage.localFileSystem; //Needed to access file storage

// UI Elements - button to trigger the run action
document.getElementById("runEffect").addEventListener("click", init);

// Main Functions
async function init() {
    // Other functions in here
    async function action1() {
        const df = await fs.getPluginFolder(); //Get DIR to local plugin folder
        const file = await df.getEntry("Map1.psd"); //Get file within plugin DIR via promise? async function
        const fileToken = await fs.createSessionToken(file); //Convert promise to a session token
        let result;
        let command = [ // Add your batchPlay json in here
         
            // Convert Later to Smart Object - needed for most filters and Displace otherwise it will ask you to do it anyways, so may as well do this for the user
            {
                "_obj": "newPlacedLayer"
            },
           
            // Displace batchPlay script
            {
                "_obj": "displace",
                "horizontalScale": 50,
                "verticalScale": 50,
                "displacementMap": {
                    "_enum": "displacementMap",
                    "_value": "stretchToFit"
                },
                "undefinedArea": {
                    "_enum": "undefinedArea",
                    "_value": "repeatEdgePixels"
                },
                "displaceFile": {
                    "_path": fileToken, //Use the fileToken variable here
                    "_kind": "local"
                },
                "_isCommand": true,
                "_options": {
                    "dialogOptions": "dontDisplay" // Don't show any dialogs to the user
                }
            }
        ]
        result = await psAction.batchPlay(command, {}); //Not sure, wait for stuff?

    }
    await require("photoshop").core.executeAsModal(action1, {"commandName": "Action Commands"}); // Run the batchPlay actions
}

```

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [May 18, 2022, 7:24am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/9 "2022-05-18T07:24:03Z")

</div>

> [@Blaz](#):
>
> `result = await psAction.batchPlay(command, {}); //Not sure, wait for stuff?`

You don’t need to explicitly wait for the execution of batchPlay if there’s no code following this line. (It’s async and Photoshop will continue finishing the operation no matter if you wait for it or not)  
Right now you’re assigning it to `result` but don’t do anything with it. If you’d do something like  
`console.log(result)` afterwards though, the `await` needs to be there.

---

<div class="post-metadata">

**Author:** ![Blaz](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/blaz/32/2162_2.png) [@Blaz](https://forums.creativeclouddeveloper.com/u/Blaz)\
**Post date:** [May 19, 2022, 1:18am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/10 "2022-05-19T01:18:57Z")

</div>

Amazing thanks for clarifying mate. Your right I’m not doing anything after it. So could just update that call to?

```auto
psAction.batchPlay(command, {});

```

Would that be correct?

---

<div class="post-metadata">

**Author:** ![simonhenke](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/simonhenke/32/957_2.png) [@simonhenke](https://forums.creativeclouddeveloper.com/u/simonhenke)\
**Post date:** [May 19, 2022, 6:57am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/11 "2022-05-19T06:57:56Z")

</div>

If your script is finished at this point, yes.  
You can also have batchplay execute synchronously by adding `"synchronousExecution": true` to the options (empty brackets at the end). I’ve read somewhere though, that batchPlay will be or has already been split into two separate functions, one being async, the other one sync.

---

<div class="post-metadata">

**Author:** ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)\
**Post date:** [May 19, 2022, 7:46am UTC](https://forums.creativeclouddeveloper.com/t/read-currently-opened-images/4323/12 "2022-05-19T07:46:33Z")

</div>

It’s [in the docs](https://developer.adobe.com/photoshop/uxp/2022/ps_reference/media/photoshopaction/#batchplay), but as the docs aren’t versioned, so I have no idea with which combination of Ps-UXP-Manifest versions it works
