Trying to dynamically create controls in enhanced window

#1

I need a dialog with a variable number of checkboxes. The number varies with the number of items in a list. For example, on one occasion, there might be 5 items in the list and so five checkboxes. On another occasion there might be 15 items in the list and so 15 checkboxes. The user would check the desired boxes and processing would then be done on the choice.

Is there a way of doing that ? I have tried code like this (the variable myNum can be any integer):

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "DialogToolkitPlus" version "1.0.1" -- Yosemite (10.10) or later
set myNum to 5
set Checkboxes to {}
repeat with k from 1 to myNum
	set end of Checkboxes to ""
end repeat
set name_list to {"first", "second", "third", "fourth", "fifth"}
set diag_Title to "Test checkboxes"
set accViewWidth to 400
set accViewInset to 0
set {theButtons, minWidth} to create buttons {"Cancel", "Process"} button keys {"c", "p"} default button 2
if minWidth > accViewWidth then set accViewWidth to minWidth
set {theRule, theTop} to create rule 10 rule width accViewWidth
repeat with j from 1 to myNum
	set {item j of Checkboxes, theTop} to create checkbox (item j of name_list) left inset 0 bottom (theTop + 5) max width 250
end repeat
set collectvariables to {}
repeat with z from 1 to myNum
	copy item z of Checkboxes to end of collectvariables
end repeat
set allControls to {theRule, collectvariables}
set {button_returned, controls_results} to display enhanced window diag_Title button list theButtons acc view width minWidth acc view height theTop acc view controls allControls

I’m getting this error:

error "-[__NSArrayM superview]: unrecognized selector sent to instance 0x608000e5c710" number -10000

I can’t figure out where the problem is. Perhaps it’s in the process of aggregating the checkbox variables into “allControls”. But, I can’t figure out another way of doing it.

Thanks.

0 Likes

(Shane Stanley) #2

The immediate problem is caused by the second-last line, I suspect — rather than creating a list of all controls you are creating a list containing the rule plus another list. But you can shorten things elsewhere, too:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use script "DialogToolkitPlus" version "1.0.1" -- Yosemite (10.10) or later

set Checkboxes to {}
set name_list to {"first", "second", "third", "fourth", "fifth"}
set diag_Title to "Test checkboxes"
set accViewWidth to 400
set accViewInset to 0
set {theButtons, minWidth} to create buttons {"Cancel", "Process"} button keys {"c", "p"} default button 2
if minWidth > accViewWidth then set accViewWidth to minWidth
set {theRule, theTop} to create rule 10 rule width accViewWidth
repeat with j from 1 to count of name_list
	set {aCheckbox, theTop} to create checkbox (item j of name_list) left inset 0 bottom (theTop + 5) max width 250
	set end of Checkboxes to aCheckbox
end repeat
set allControls to {theRule} & Checkboxes
set {button_returned, controls_results} to display enhanced window diag_Title button list theButtons acc view width minWidth acc view height theTop acc view controls allControls
0 Likes

#3

Wonderful. Many thanks. I logged the contents of “allControls” but, they looked fine - I couldn’t see what was out of place.

But, I would never have thought of “set end of Checkboxes to aCheckbox”. It just hasn’t occurred to me that the end of something can be pointed to like that. Now I’m finding “set end of” in lots of places, such as the Language Guide.

Cheers.

0 Likes