Skip to main content
dublove
Legend
Aug 31, 2025 8:12 PM UTC
Answered

How can I make if (!confirm(‘Please confirm!') only affect certain functions in main()?

When using  if (!confirm('Please confirm!'),

Only A1(); and A2(); require confirmation.

A3() no need confirm.
Do I need to move  A3();  outside of  main();?

 

function main(){
    if (!confirm('Please confirm!'))
        return;
A1();
A2();

A3();
}

 

m1b
m1bCorrect answer
Community Expert
Sep 1, 2025 7:22 AM UTC

@dublove do you know why it didn't work? Maybe because you wanted to share variables, but your functions were out of scope? Try putting the functions inside, and at the end of the main function. Like this:

function main() {

    if (confirm('Please confirm')) {
        A1();
        A2();
    }

    A3();

    // script finished

    function A1() {
        // do something here
    };

    function A2() {
        // do something here
    };

    function A3() {
        // do something here
    };

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');

6 replies

brian_p_dts
Community Expert
Community Expert
Aug 31, 2025 9:11 PM UTC

Usually better and more logical to think through what happens in the True case. 

if(confirm("Please confirm") {

A1();

A2(),

}

A3();

Translate
dublove
dubloveAuthor
Legend
Sep 1, 2025 2:23 AM UTC

This has already been tried.
It didn't work.

Because confirm is returned to main(),

anything within `main` will be affected.

Translate
m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
Sep 1, 2025 7:22 AM UTC

@dublove do you know why it didn't work? Maybe because you wanted to share variables, but your functions were out of scope? Try putting the functions inside, and at the end of the main function. Like this:

function main() {

    if (confirm('Please confirm')) {
        A1();
        A2();
    }

    A3();

    // script finished

    function A1() {
        // do something here
    };

    function A2() {
        // do something here
    };

    function A3() {
        // do something here
    };

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Do Script');
Translate