Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
@m1b so while Space key can trigger all, Return will always and only capture "ok"?
Copy link to clipboard
Copied
Yes return will trigger "ok" button. Space will trigger the active control.
Copy link to clipboard
Copied
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())
Copy link to clipboard
Copied
@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...
Copy link to clipboard
Copied
ahh I had misunderstood. disregard my comment then!
Copy link to clipboard
Copied
While btnCurrent is blue, Space will return "0" and Return will return "1"
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
@Silly-V You're right. But how do I set this dynamically, when I choose a different butten e.g. with Tab?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
@Silly-V You're great, that works fine! Thanx a lot. Neither Perplexity nor ChatGPt were able to solve this! ;D
Copy link to clipboard
Copied
@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;
}
Copy link to clipboard
Copied
@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". ...
Copy link to clipboard
Copied
@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();
Copy link to clipboard
Copied
think
btn_ok2.active = true; // Preselect button 2 as initially activewill solve it! Great
Copy link to clipboard
Copied
No it does not ... this way Return button will keep fokus on activated ... so it simply does not work
Copy link to clipboard
Copied
@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?
Copy link to clipboard
Copied
@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 ...
Copy link to clipboard
Copied
> 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.
Copy link to clipboard
Copied
@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?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
@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!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now