Choose from list, multiple selections allowed... sometimes

What I want is a dialog which will allow our worker bees to choose from a list of available server volumes to mount. This works okay if I only put in the names of the individual volumes. However, I would also like to have in the list the option to select “All volumes” or “All A volumes” or “All B volumes”. When using multiple selections allowed, this allows the user to not only select individual volumes, but also “all” or “All B”, etc. Is there a way to make it work so that when “All volumes” is selected, then multiple selections are not allowed? Or “A” & “B”, but not individual volumes (which would already be included in “A” & “B”? If this isn’t possible, how do I handle the choice of “A”, “B”, plus individual volumes? Or “A” plus individual volumes or “B” plus individual volumes, or “All volumes” plus “A” volumes, plus “B” volumes, plus others… this is as far as I got before getting too confused…

set volumeList to {"All A volumes", "All B volumes", "All volumes", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "B1", "B2", "B3"}
set volumeChoice to (choose from list volumeList with prompt "Which volumes would you like to mount?" with multiple selections allowed) as list

if volumeChoice contains {"All volumes"} then set volumeChoice to items 4 through (count volumeList) of volumeList
if volumeChoice contains {"All A volumes" and "All B volumes"} then set volumeChoice to items 4 through (count volumeList) of volumeList

--if volumeChoice contains {"All A volumes" but not "All B volumes" or "All volumes"} then set volumeChoice to items 4 through 10 of volumeList
--if volumeChoice contains {"All B volumes" but not "All A... volumes" or "All volumes"} then set volumeChoice to items 11 through 13 of volumeList

I spent an hour searching the forum, but didn’t find anything like this. Please help!

Jonathan

Hi Jonathan,

I would do it this way


set Alist to {"A1", "A2", "A3", "A4", "A5", "A6", "A7"}
set Blist to {"B1", "B2", "B3"}
set volumeList to {"All A volumes", "All B volumes", "All volumes"} & Alist & Blist
set volumeChoice to (choose from list volumeList with prompt "Which volumes would you like to mount?" with multiple selections allowed) -- this is a list anyway
if volumeChoice is false then error number -128
set chosenVolumes to {}
if volumeChoice contains {"All volumes"} then
	set chosenVolumes to Alist & Blist
else if volumeChoice contains {"All A volumes"} or volumeChoice contains {"All B volumes"} then
	if volumeChoice contains {"All A volumes"} then copy Alist to chosenVolumes
	if volumeChoice contains {"All B volumes"} then set chosenVolumes to chosenVolumes & Blist
else
	copy volumeChoice to chosenVolumes
end if
chosenVolumes

Thanks, Stephan! This is exactly the kind of thing I was trying to come up with. What would you suggest in the event the user selects “All A volumes” and B2? Or “All volumes” and A3 and A5 and B1? Currently, it won’t add these together into the chosenVolumes list. Note that if “All B volumes” and B2 are selected, this includes duplicate information. Can you add (copy to) the list only on the condition that the item name is not already in the list?

Jon

Next try, this should catch all cases


set Alist to {"A1", "A2", "A3", "A4", "A5", "A6", "A7"}
set Blist to {"B1", "B2", "B3"}
set volumeList to {"All A volumes", "All B volumes", "All volumes"} & Alist & Blist
set volumeChoice to (choose from list volumeList with prompt "Which volumes would you like to mount?" with multiple selections allowed) -- this is a list anyway
if volumeChoice is false then error number -128
set {chosenAll, chosenA, chosenB} to {{}, {}, {}}
repeat with i in volumeChoice
	if i begins with "All" then
		set end of chosenAll to contents of i
	else if i begins with "A" then
		set end of chosenA to contents of i
	else
		set end of chosenB to contents of i
	end if
end repeat

if chosenAll contains {"All volumes"} or (chosenAll contains {"All A volumes"} and chosenAll contains {"All B volumes"}) then
	set chosenVolumes to Alist & Blist
else if chosenAll contains {"All A volumes"} then
	set chosenVolumes to Alist & chosenB
else if chosenAll contains {"All B volumes"} then
	set chosenVolumes to Blist & chosenA
end if
chosenVolumes

I had to read through it a dozen times before I figured out why it works, but I got it. It works perfectly. I bow before your exalted Apple-scripted-ness. Thanks!

One more piece is needed… It works great if multiple selections are made which include an “All” option, but if only one or more specific volumes are chosen, not including any “All” options, nothing is returned. It seems to me that the line

		set end of chosenB to contents of i

should add these items into the list, but it doesn’t.

Also, the variable “ChosenVolumes” retains the value from the previous run if no new value is written to it. So, if I select “All B volumes” on one run, then the next time choose A2, the returned value is B1, B2, B3. To deal with this, I added

set chosenVolumes to {}

at the beginning on the script.

Thanks again for all your help!
Jon