Re: Choose from list
Re: Choose from list
- Subject: Re: Choose from list
- From: "Gary (Lists)" <email@hidden>
- Date: Thu, 21 Jul 2005 05:44:58 -0400
"Timothy Bates" wrote:
> To make it concrete for the purpose of this email:
>
> I might say something like
>
> set a to Choose from list {"dog", "cat", "pet"}
>
> And get back:
> --> a = {"2", "cat"}
>
> So I have in one place both the original index of the item(s) chosen, and
> the values of the items chosen.
Here are the two handlers that I use (that don't trap for any list problems,
but that work for my needs). I mostly just want 'choose from list' to return
the indices, since I already have the list elsewhere (I don't need the items
back again). So, that's all these do.
The first is for multiple selection 'choose from list', the second is for
single choice. They both take a list as their only parameter.
EX:
set L to {"a", "b", "c", "d", "e"}
getChooseIndex(L)
--> 2
--> 0
getChooseIndexMulti(L)
--> {1,3,4}
--> {}
-- =============================================================[0.2]
-- returns: list of indices of selected items, ex. {1,3,5}
-- returns: {} if user cancels
--
to getChooseIndexMulti(theList)
set {x, m} to {{}, (count theList)}
try
-- NEXT LINE MIGHT WRAP, should be single line
set _r to (choose from list theList with multiple selections
allowed)
set i to 0
repeat with k from 1 to (count _r)
repeat with i from (i + 1) to m
if (item k of _r) is item i of theList then
copy i to end of x
exit repeat
end if
end repeat
end repeat
on error
-- cancel, we presume
end try
return x
end getChooseIndexMulti
-- ==================================================[gw][/handlers/]
-- =============================================================[0.2]
-- returns: index of selected item, ex. 2
-- returns: 0 if user cancels
--
to getChooseIndex(theList)
set {i, m} to {0, (count theList)}
try
set _r to (choose from list theList)
set r to item 1 of _r
repeat with i from 1 to m
if r is item i of theList then exit repeat
end repeat
on error
-- cancel, we presume
end try
return i
end getChooseIndex
-- ==================================================[gw][/handlers/]
--
Gary
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden