My ExtendScript dialog for After Effects uses two buttons for input. I need a way to return the entered value rather than relying on individual button events.
function getInputValue(promptText, heading) {
var dlg = new Window('dialog', heading, [150,150,350,250]);
dlg.promptLabel = dlg.add('statictext', [20,10,220,30], promptText);
dlg.inputBox = dlg.add('edittext', [20,30,220,50], '');
dlg.submitBtn = dlg.add('button', [20,60,110,80], 'Submit');
dlg.abortBtn = dlg.add('button', [120,60,210,80], 'Cancel');
dlg.submitBtn.onClick = function() {
if(dlg.inputBox.text !== '') {
var answer = dlg.inputBox.text;
dlg.close();
return answer;
} else {
alert('Please enter a value');
}
};
dlg.abortBtn.onClick = function() {
dlg.close();
return false;
};
return dlg.show();
}
i ended up storing the input in an external var within the dialog. closing the window then lets me access it. this method doesn’t mess with immediate return values from the button events and feels more straightforwad in my ae scripts.
I encountered a similar issue where returning a variable directly from a button click didn’t yield the expected result. Instead, I used a method that stores the user input in a variable outside the button event callbacks. This way, after the dialog is closed, I refer back to that variable. It’s important to remember that dlg.show returns a status code, not the text value from the input field. Adjusting the structure to store the data externally or using a callback upon dialog closure helped resolve this challenge.
After working with similar scenarios, I discovered that wrapping the dialog operations in a promise was quite effective. In this arrangement, the promise is resolved with the desired input value or rejected on cancelation. Using this technique allowed me to wait for the dialog response and then process the input in a sequential manner. The asynchronous handling provides clarity, ensuring that I can access the input value after the dialog is closed without relying on immediate returns from button callbacks. It considerably simplified my workflow.
In my experience, overcoming the immediate return limitation in dialog events entails using external closures or handlers to capture the input. I solved the problem by establishing a variable in a higher scope that is assigned a new value when the submit button is clicked. While the dialog’s show method only returns a status code, inspecting this variable after the window is closed provides the user input reliably. This technique ensures that the code remains straightforward and avoids the pitfalls of trying to directly return a value from a button event.