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

"Return" will not be captured with Script UI

Community Beginner ,
Nov 11, 2025 Nov 11, 2025

Thsi Script exports PDF files to a given folder without saving the current AI-file.

When one of the dialoge windows is open, I can navigate through the options and choose them with space-key. So the button gets blue higlighted but the predefined keeps its white outline. But hitting "Return" an the blue colored button, will not work at all. It will activate the white outlined button.
Space will activate the blue higlighted instead, like one would expect it from "Retrun", too.

Any idea how to capture "Return" when choosing e.g. "Abbrechen" in this dialogue?

function askDocumentChoice() {
    var dlg = new Window("dialog", "Welche Dokumente speichern?");

    dlg.add("Group {orientation:'row', alignment:['fill','top']}").add("statictext", undefined, "Sollen alle geöffneten Dokumente gespeichert werden?");
    dlg.add("Group {orientation:'row', alignment:['fill','top']}").add("statictext", undefined, "(oder nur das aktuelle?)");

    var buttonRow = dlg.add("Group {orientation:'row', alignment:['right','top']}");

    var btnCurrent = buttonRow.add("button", undefined, "Nur aktuelles");
    var btnAll = buttonRow.add("button", undefined, "Alle", { name: 'ok' });
    var btnCancel = buttonRow.add("button", undefined, "Abbrechen", { name: 'cancel' });

    btnCurrent.onClick = function() { dlg.close(0); };
    btnAll.onClick = function() { dlg.close(1); };
    btnCancel.onClick = function() { dlg.close(-1); };

    dlg.defaultElement = btnAll;
    dlg.cancelElement = btnCancel;

    dlg.center();
    return dlg;
}
TOPICS
Scripting
307
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 Beginner ,
Nov 11, 2025 Nov 11, 2025

Sorry for the wrong grammatic, but I cannot edit my post anymore... So I just wanted to add I navigate with Tab-key through the different buttons.

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

Bildschirmfoto 2025-11-11 um 16.57.33.jpg

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 ,
Nov 11, 2025 Nov 11, 2025

Hi @0771 the Return key will trigger the onClick handler for the button whose `name` === "ok".

Similarly, the Escape key will trigger the button named "cancel".

 

Does that answer your question or have I misunderstood the point?

-Mark

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

@m1b so while Space key can trigger all, Return will always and only capture "ok"? 

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 ,
Nov 11, 2025 Nov 11, 2025

Yes return will trigger "ok" button. Space will trigger the active control. 

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
Contributor ,
Nov 11, 2025 Nov 11, 2025

I don't see your dlg.show() line anywhere, but writing it to say "return dlg.show()" will return the number values passed through the .close() method.

function askDocumentChoice() {
    var dlg = new Window("dialog", "Welche Dokumente speichern?");

    dlg.add("Group {orientation:'row', alignment:['fill','top']}").add("statictext", undefined, "Sollen alle geöffneten Dokumente gespeichert werden?");
    dlg.add("Group {orientation:'row', alignment:['fill','top']}").add("statictext", undefined, "(oder nur das aktuelle?)");

    var buttonRow = dlg.add("Group {orientation:'row', alignment:['right','top']}");

    var btnCurrent = buttonRow.add("button", undefined, "Nur aktuelles");
    var btnAll = buttonRow.add("button", undefined, "Alle", { name: 'ok' });
    var btnCancel = buttonRow.add("button", undefined, "Abbrechen", { name: 'cancel' });

    btnCurrent.onClick = function() { return dlg.close(0); };
    btnAll.onClick = function() { return dlg.close(1); };
    btnCancel.onClick = function() { return dlg.close(4); };

    dlg.defaultElement = btnAll;
    dlg.cancelElement = btnCancel;

    return dlg.show()
}

alert(askDocumentChoice())
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 Beginner ,
Nov 11, 2025 Nov 11, 2025

@RobOctopus the script works fine it's all about the Return trigger here. I would expect Return to trigger the same as Space key. But it seems this is not the case...

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
Contributor ,
Nov 11, 2025 Nov 11, 2025

ahh I had misunderstood. disregard my comment then!

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

While btnCurrent is blue, Space will return "0" and Return will return "1"

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
Valorous Hero ,
Nov 11, 2025 Nov 11, 2025
dlg.defaultElement = btnAll;

Have you tried setting this to a different button dynamically?

I think the issue is, the default element works on Return - so when you press return , it always clicks your Ok button.

Space probably presses your current highlighted button, while return always goes to Ok button regardless of which button you have focused right now.

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

@Silly-V You're right. But how do I set this dynamically, when I choose a different butten e.g. with Tab?

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
Valorous Hero ,
Nov 11, 2025 Nov 11, 2025

I think in your case you could do this: not have defaultElement as you have custom closing methods.

#target illustrator
function test(){
    var w = new Window("dialog");
    var btn_ok = w.add("button", undefined, "Ok");
    var btn_ok2 = w.add("button", undefined, "Ok-2");
    var resultAction = "";
    btn_ok.onActivate = function () {
        resultAction = "Action from Button 'Ok '";
    };
    btn_ok2.onActivate = function () {
        resultAction = "Action from Button 'Ok-2'";
    };
    btn_ok.onClick = function () { w.close(); };
    btn_ok2.onClick = function () { w.close(); };
    w.show();
    alert(resultAction);
};
test();

Here the .onActivate is being used to switch a variable - it works both with Space and Return.

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

@Silly-V You're great, that works fine! Thanx a lot. Neither Perplexity nor ChatGPt were able to solve this! ;D

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

@Silly-V  I changed my script to your setting, but I realized, when ever I hit Return key, I get the result "null" but it should be true. Do you o´know what to do?

function askSimpleOverwrite(fileName) {
    var dlg = new Window("dialog", "Datei existiert bereits");
    dlg.orientation = "column";
    dlg.alignChildren = "center";

    var msg1 = 'Datei "' + fileName + '" existiert bereits.';
    var msg2 = 'Soll sie überschrieben werden?';

    var txt1 = dlg.add("statictext", undefined, msg1);
    txt1.justify = "center";

    var txt2 = dlg.add("statictext", undefined, msg2);
    txt2.justify = "center";

    var btnGroup = dlg.add("group");
    btnGroup.alignment = "center";

    var yesBtn = btnGroup.add("button", undefined, "Ja", { name: "ok" });
    var noBtn = btnGroup.add("button", undefined, "Nein", { name: "no" });
    var cancelBtn = btnGroup.add("button", undefined, "Abbrechen", { name: "cancel" });

    var result = null; // null means cancel

    yesBtn.onActivate = function() { result = true; dlg.close(); };
    noBtn.onActivate = function() { result = false; dlg.close(); };
    cancelBtn.onActivate = function() { result = null; dlg.close(); };

    yesBtn.onClick = function() { dlg.close(); };
    noBtn.onClick = function() { dlg.close(); };
    cancelBtn.onClick = function() { dlg.close(); };

    dlg.defaultElement = yesBtn;
    // dlg.cancelElement = cancelBtn;

    dlg.addEventListener('keydown', function(k) {
        if (k.keyName === 'Escape') { result = null;
            dlg.close(); }
    });

    dlg.center();
    dlg.show();
    alert(result);
    return result;
}
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 Beginner ,
Nov 11, 2025 Nov 11, 2025

@Silly-V  When I test your script I only get a valid Return-key value if i choose "OK" Button with Tab. When I just start the script and press Return, I do not get any resultAction, so it actually does not work, too. It was only missleading because everything is named "OK". ...

 

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

@Silly-V Name the result variable "NO" than Return key will deliver "NO" while "OK" is preselected actually ... 

function test(){
    var w = new Window("dialog");
    var btn_ok = w.add("button", undefined, "Ok");
    var btn_ok2 = w.add("button", undefined, "Ok-2");
    var resultAction = "NO";
    btn_ok.onActivate = function () {
        resultAction = "Action from Button 'Ok '";
    };
    btn_ok2.onActivate = function () {
        resultAction = "Action from Button 'Ok-2'";
    };
    btn_ok.onClick = function () { w.close(); };
    btn_ok2.onClick = function () { w.close(); };
    w.show();
    alert(resultAction);
};
test();
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 Beginner ,
Nov 11, 2025 Nov 11, 2025

think 

    btn_ok2.active = true; // Preselect button 2 as initially active

will solve it! Great

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

No it does not ... this way Return button will keep fokus on activated ... so it simply does not work

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 ,
Nov 11, 2025 Nov 11, 2025

@0771 I'm not understanding what you want to happen. Do you want to be able to override the normal behaviour which is that pressing Return triggers the designated "OK" button (the window's defaultElement)? In other words do you want pressing Return to work like pressing Space?

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 Beginner ,
Nov 11, 2025 Nov 11, 2025

@m1b Right, as a user Space is no common way to navigate or at least to confirm dialogues. I thinnk this is for nerds only ;D so Return key or Enter key is usually the expected way to confirm an activated button. That's why I want it to wark like Space ... 

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 ,
Nov 11, 2025 Nov 11, 2025

> Space is no common way to navigate

 

It's actually common in web browsers for Space key to "click" the active form element, eg. a checkbox. Return key is handled separately and might "submit" the form no matter which element was active. Note that Space doesn't "confirm" the dialog—it triggers the click handler, if any, for the active control, if any.

 

So that's why I added the last comment on my earlier post about violating user expectations.

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 ,
Nov 11, 2025 Nov 11, 2025

@0771 does this help at all... it's a formulation of your script that keeps track of the active button, and when the user presses Return (and triggers btnAll's onClick handler) it then re-routes to the active button's onClick handler. See if it always gives you the result you want.

 

(function () {

    // this is your main script

    var result = askDocumentChoice();

    if (2 === result)
        alert('Der Benutzer wählte „Abbrechen“.');

    else if (1 === result)
        alert('Der Benutzer wählte „Alle“.');

    else if (0 === result)
        alert('Der Benutzer wählte „Nur aktuelles“.');

})();

function askDocumentChoice() {

    var activeButton;

    var dlg = new Window("dialog", "Welche Dokumente speichern?");

    dlg.add("Group {orientation:'row', alignment:['fill','top']}").add("statictext", undefined, "Sollen alle geöffneten Dokumente gespeichert werden?");
    dlg.add("Group {orientation:'row', alignment:['fill','top']}").add("statictext", undefined, "(oder nur das aktuelle?)");

    var buttonRow = dlg.add("Group {orientation:'row', alignment:['right','top']}");

    var btnCurrent = buttonRow.add("button", undefined, "Nur aktuelles");
    var btnAll = buttonRow.add("button", undefined, "Alle", { name: 'ok' });
    var btnCancel = buttonRow.add("button", undefined, "Abbrechen", { name: 'cancel' });

    // set listeners on buttons to keep track of the active button
    for (var i = 0, btn; i < buttonRow.children.length; i++) {

        btn = buttonRow.children[i];

        btn.onActivate = function () {
            activeButton = this;
        };

        btn.onDeactivate = function () {
            activeButton = null;
        };

    }

    btnCancel.onClick = function () { dlg.close(2); }; // cancel is always return code 2 - do not change because user may press Escape key
    btnCurrent.onClick = function () { dlg.close(0); };

    btnAll.onClick = function () {

        var btn = activeButton;
        
        // must nullify or we could go in a loop
        activeButton = null;
        
        // handle active button
        if (btn) {
            return btn.onClick();
        }

        // no active button, so go with default for btnAll
        dlg.close(1);

    };

    dlg.center();

    return dlg.show();

};

 

P.S. I don't agree with what the above script actually does. As a user myself, I would be surprised (and dismayed!) that pressing Return activated the active button and not the default (clearly outlined) button. But perhaps you have a reason for violating user expectations?

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 Beginner ,
Nov 11, 2025 Nov 11, 2025
Yes I have a reason, as the button is not outlined when it is highlighted blue, this is no UI conformity. And this is absolutely confusing. If Return triggers the outlined value it should always be outlined, rigth?
But there isn’t any unified UI in Illustrator anyway …
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 Beginner ,
Nov 12, 2025 Nov 12, 2025

@m1b Tried your approach but it actually will not capture any Return event other than the predefined value. Even if you place an event handler, it will not work. I accept that. It works like you described it and so it is. Thanx for your help!

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