# File open dialogue opens twice

**URL:** <https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948>\
**Category:** Photoshop\
**Created:** [May 14, 2021, 8:52am UTC](https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948 "2021-05-14T08:52:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![matakus](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matakus](https://forums.creativeclouddeveloper.com/u/matakus)\
**Post date:** [May 14, 2021, 8:52am UTC](https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948/1 "2021-05-14T08:52:10Z")

</div>

Not sure if anyone else is getting the same issue, but when I run this basic function, the File picker opens the selected file, then opens the file picker again.

If I close the second instance, then run the function again, it runs as expected and opens a selected file.

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

```
async function openFile() {
   const file = await fs.getFileForOpening();
      await app.open(file);
      return
}

```

Is this code the correct way to open a file with the picker?

---

<div class="post-metadata">

**Author:** ![tomzag](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/tomzag/32/2464_2.png) [@tomzag](https://forums.creativeclouddeveloper.com/u/tomzag)\
**Post date:** [May 14, 2021, 11:04am UTC](https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948/2 "2021-05-14T11:04:33Z")

</div>

Try it this way:

```auto
async function openFile() {
   const file = await fs.getFileForOpening();
   if (file) {
      await app.open(file);
   } else {
      return;
   }
}

```

The `else` block is for catching the case if the user clicks on ‘Cancel’ in the file dialog.

---

<div class="post-metadata">

**Author:** ![matakus](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matakus](https://forums.creativeclouddeveloper.com/u/matakus)\
**Post date:** [May 15, 2021, 12:05am UTC](https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948/3 "2021-05-15T00:05:06Z")

</div>

Hmm it’s interesting as i’m getting the same behaviour with your code

I have no idea what’s causing the picker to open twice.

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [May 16, 2021, 4:13am UTC](https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948/4 "2021-05-16T04:13:43Z")

</div>

Are you running that from an sp-button? I had the same issue with the folder picker running from sp-buttons. The event listener semmed to run in an infinite loop. Switching to a native HTML button fixed it. I posted about this a few months ago and other had the same problem too.

Since that time, I implemented a fix for the space and enter key focus issue. I then tried this with sp-buttons again for the folder picker and it also solved the folder picker issue too. I think the button was triggering again when the user clicked to select a folder which is why the folder picker kept opening again. The “nofocus” fixed Kerri Shots gave me in this post fixed the issue.

> [@Issue with spacebar or enter key triggering buttons in UXP plugins](http://forums.creativeclouddeveloper.com/t/issue-with-spacebar-or-enter-key-triggering-buttons-in-uxp-plugins/2653/32):
>
> Thanks!!! I just tested the snippet and it works perfectly. Not sure all of what all “blur” does. However, it works as you described without any other affects to the plugin as far as I can tell slight_smile

---

<div class="post-metadata">

**Author:** ![matakus](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matakus](https://forums.creativeclouddeveloper.com/u/matakus)\
**Post date:** [May 20, 2021, 12:45am UTC](https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948/5 "2021-05-20T00:45:29Z")

</div>

That’s it - good find. Adding that code fixes it.

HTML  
`<sp-button nofocus>Hi</sp-button>`

JS

```
Array.from(document.querySelectorAll("[nofocus]:not([data-handler-added])"), el => {
    el.addEventListener("focus", evt => evt.target.blur());
    el.setAttribute("data-handler-added");
});
```

---

<div class="post-metadata">

**Author:** ![AndreasResch](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/andreasresch/32/1423_2.png) [@AndreasResch](https://forums.creativeclouddeveloper.com/u/AndreasResch)\
**Post date:** [June 5, 2021, 2:01pm UTC](https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948/6 "2021-06-05T14:01:31Z")

</div>

Where would be a proper place to place that JS snippet in my JS file? Or is there a fix from Adobe in the meantime?

---

<div class="post-metadata">

**Author:** ![ddbell](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/ddbell/32/1030_2.png) [@ddbell](https://forums.creativeclouddeveloper.com/u/ddbell)\
**Post date:** [June 5, 2021, 9:18pm UTC](https://forums.creativeclouddeveloper.com/t/file-open-dialogue-opens-twice/2948/7 "2021-06-05T21:18:52Z")

</div>

It really shouldn’t matter. I just put it at the bottom.
