Using Dialog Toolkit with JavaScript

Although the Dialog Toolkit Plus dictionary shows JavaScript form, I have been unable to use the toolkit with Javascript. I hope it’s just that I’ve messed up the syntax in some way, since I haven’t found any working examples of Dialog Toolkit used with JavaScript. I’d really appreciate input from anyone who has gotten this to work.

Here’s what I have tried, commented to show what does and does not work. Copied from Script Editor.

// All of this seems to work, in that I get no error messages and the values seem plausible
var dtp = Library('Dialog Toolkit Plus');
var res = dtp.createCheckbox('do it',{bottom:0, maxWidth:100});
var check1 = res[0], theTop = res[1];
res = dtp.createField('',{placeholderText:'find',bottom:(theTop+8), fieldWidth:260});
var field1 = res[0]; theTop = res[1];
res = dtp.createField('',{placeholderText:'replace',bottom:(theTop+8), fieldWidth:260});
var field2 = res[0]; theTop = res[1];

// Works and gives a number that varies with label length (just used for testing)
var len = dtp.maxWidthForLabels(['long label','even longer label'],{controlSize:'regular size'});

// Now it gets messy 
// Tested each line alone (by uncommenting it). Comment above line shows what happened.

// OK. displays an empty dialog with two buttons and the expected text
//dtp.displayEnhancedAlert('test dialog 1',{message:'no controls',buttons:['Cancel','OK'], accViewWidth:300, accViewHeight:200, accViewControls:[]});

// Error: -[NSAppleEventDescriptor superview]: unrecognized selector sent to instance 0x6000024283e0
//dtp.displayEnhancedAlert('test dialog 2',{message:'just a checkbox',buttons:['Cancel','OK'], accViewWidth:300, accViewHeight:200, accViewControls: [check1]}); 

// Error: -[NSAppleEventDescriptor superview]: unrecognized selector sent to instance 0x608002227280
//dtp.displayEnhancedAlert('test dialog 3',{message:'just a field',buttons:['Cancel','OK'], accViewWidth:300, accViewHeight:200, accViewControls: [field1]}); 

// Error: -[NSAppleEventDescriptor superview]: unrecognized selector sent to instance 0x60800222ed40
//dtp.displayEnhancedAlert('test dialog 4',{message:'two fields',buttons:['Cancel','OK'], accViewWidth:300, accViewHeight:200, accViewControls: [field1,field2]}); 

// Error: Parameter to -setSubviews: contained one or more duplicate entries
// (If I look at [field1,field2,check1], none seem to be duplicates)
//dtp.displayEnhancedAlert('test dialog 5',{message:'two fields and checkbox',buttons:['Cancel','OK'], accViewWidth:300, accViewHeight:200, accViewControls: [field1,field2,check1]}); 

// Enhanced window doesn't work at all
res = dtp.createButtons(['Cancel','OK'],{defaultButton:'OK'}); // works
var btns = res[0];
// Error: item 1 of reverse of {«class ocid» id «data optr0000000060A3150080600000» of «script», «class ocid» id «data optr00000000A032150080600000» of «script»} doesn’t understand the “frame” message.
// the above error mssage happens whether accViewControls array is [] or [check1]
//dtp.displayEnhancedWindow('test window',{buttonList:btns,accViewWidth:600,accViewHeight:400, accViewControls:[]});

// Checking values of controls created by the toolkit: 
//check1 // Library("Dialog Toolkit Plus").elementsForClassCode("ocid").byId("'optr'($6061150080600000$)")
//field1 // Library("Dialog Toolkit Plus").elementsForClassCode("ocid").byId("'optr'($10AF161BFB7F0000$)")
//field2 // Library("Dialog Toolkit Plus").elementsForClassCode("ocid").byId("'optr'($E09EEA18FB7F0000$)")
//len // 107.6240234375
//btns // [Library("Dialog Toolkit Plus").elementsForClassCode("ocid").byId("'optr'($60A3150000600000$)"), Library("Dialog Toolkit Plus").elementsForClassCode("ocid").byId("'optr'($6098150040600000$)")]

The good news is that you appear to be doing nothing wrong — the bad news is that something that is resolved fine when called from AppleScript is not resolving when called from JXA. It looks like you’re out of luck.

Thanks for the quick response. Guess I’ll have to find some other way to interact with the user.

There’s a simple solution for a modal dialog that returns user-entered values. A script written in AppleScript can have a function showDialog() that sets up the dialog. A JXA script can refer to it as a library and call showDialog(), receiving the user input.

This doesn’t let me keep a window open and do things (in JXA) in response to user actions, but it’s better than nothing.

FWIW, Dialog Toolkit doesn’t let you do that either.