Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

What's the correct way to prevent a dialog from opening more than once?

Explorer ,
Sep 22, 2025 Sep 22, 2025

I have an ExtendScript, which has this

/*
<javascriptresource>
<name>My Script</name>
<category>scriptexport</category>
<menu>export</menu>
<about>Some description</about>
<eventid>Some ID</eventid>
</javascriptresource>
*/

When I go to File > Export > MyScript, it opens a window/dialog, which allows the user to provide some inputs.

Now, I just realise that, if I click on File > Export > MyScript multiple times, it will keep opening new windows.

Is it possible to prevent this, i.e. at most 1 window should be opened at a time? It’s probably possible but is there an easy way to do it? If not, what do you recommend?

TOPICS
Actions and scripting
99
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Sep 22, 2025 Sep 22, 2025

It's not clear to me how this should be programmed.

 

Where are the inputs stored? Default runtime variables? Special environment/session variables? Writing to a file or metadata?

 

You could write a value to a variable and then increment the value each script run, then compare if the variable is > 1 etc. Using an environment variable this would work until Photoshop has been exited.

 

Another approach could be to run the script via a keyboard modifier to access the dialog, otherwise it would run "headless".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2025 Sep 23, 2025

I don't understand why this is the default behaviour, if the script runs synchronoyously. Shouldn't this be considered a bug? I know scripts don't necessarily have UIs, but, if they do, why would we want to allow people to open it more than once?

 

I tried to use an environment variable and it seems to be possible to solve the problem in this way, but can't environment variables also be set outside of the script? So, this could screw up the logic. But maybe it's possible to implement this correctly. Do you know of any script that used this approach? Just to double-check. I would be very surprised if I am the first one that ever wanted to prevent this.

 

I don't know what you mean by the last suggestion. I think people use the term headless in a misleading way when it comes to photoshop. Anyway, if you're just suggesting to use another approach to open the UI that will not behave like this but people can still open it multiple times by clicking on the menu item, then this does not really solve the problem.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2025 Sep 23, 2025

Is the window for the input an integral part of the script interface, such as Image Processor? Or is it just there to populate some persistent settings for use in the script without needing to be used until one of the parameters change?

 

At this point there would appear to be more questions than answers.

 

Consider what Adobe did with Export preferences which control Quick Export vs. Export As.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2025 Sep 23, 2025

Yes, the window is needed to provide arguments to the script, which thus behaves differently depending on the arguments. The users are not necessarily technical people. In principle, I could also just provide the parameters in a different way (e.g. in a file) without a window, but that is simply not an option for me. I'll not solve a problem by introducing another.

 

I was hoping that I was simplify not aware of an option that would automatically prevent this. I can't believe that Adobe did not think of this issue. Why would we ever want to open more than one window when a script runs synchrononously? It would be great if this got solved directly by Adobe. Messing with environment variables might "work", but this looks ugly and not right.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 23, 2025 Sep 23, 2025

Yes, you can prevent multiple windows by implementing a singleton pattern. Here's a simple approach:

javascript
// Global flag to track if window is open
var myScriptWindowOpen = false;

function showDialog() {
    if (myScriptWindowOpen) {
        // Bring existing window to front instead of creating new one
        return;
    }
    
    myScriptWindowOpen = true;
    
    // Create your window here
    var win = new Window('window', 'My Script');
    
    // When window closes, reset the flag
    win.onClose = function() {
        myScriptWindowOpen = false;
    };
    
    win.show();
}

Alternative approach: Store a reference to the open window and use window.find() to bring it to front if it already exists.

The key is maintaining a global variable that tracks the window state across script executions.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 23, 2025 Sep 23, 2025

Please post more information, like... the rest of the script? What are you trying to accomplish? Do you close the dialog after using the inputs?

There are ways to set variables to prevent a second window from opening, but I'm unclear about how you even get to this point.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2025 Sep 23, 2025

Aren't the variables initialised independently for each script execution? Why would a global variable work? When we click the menu to open the UI, it basically executes all the code again. Unless there's a way to maintain the state from one execution to the other, using a global variable does not work.

I don't need to paste all the code and I will not obviously. Yes, I close the window once the user selects to execute the script or cancel it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 23, 2025 Sep 23, 2025

You are asking a question about something that is broken for you but works for others. I have authored dozens of scripts but I can't help you without knowing what you are doing. I will say this, you are doing something wrong.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 23, 2025 Sep 23, 2025
LATEST

Dude, I'm not doing anything wrong in this case I think. Even the scripts that come with Photoshop can be opened multiple times. Just try it. What are you talking about? If that doesn't happen to you, then definitely it's not my script. It must be Photoshop.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines