Re: Choose from list
Re: Choose from list
- Subject: Re: Choose from list
- From: Nigel Garvey <email@hidden>
- Date: Mon, 6 May 2002 13:52:25 +0100
Matthew Stuckwisch wrote on Sun, 5 May 2002 21:57:07 -0500:
>
>> set theList to {"Dialog Box 1", "Dialog Box 2", "Dialog Box 3", "Dialog
>
>> Box
>
>> 4"}
>
>>
>
>> set optionList to {"Good Morning", "Good Afternoon", "Good Evening",
>
>> "Good
>
>> Night"}
>
>>
>
>> set theDialog to (choose from list theList) as string
>
>>
>
>> set x to my UniqueIndex(theList, theDialog)
>
>>
>
>> display dialog (item x of optionList)
>
>>
>
>> on UniqueIndex(aList, anItem)
>
>> repeat with i from 1 to (count aList)
>
>> if anItem = item i of aList then
>
>> return i
>
>> end if
>
>> end repeat
>
>> end UniqueIndex
>
>
>
> Thanks Paul. Much more elegant. I presume that there is no other option
>
> to
>
> get what item number a certain value is in a list besides a repeat loop.
>
> That would make the script even better - but I guess I'm just dreaming. :
>
> -)
>
>
Hmmm....does anyone know if it's possible to get offsets in terms of
>
paragraphs, instead of characters? If that's possible you could convert
>
the list into a return delimited string and then find the offset of the
>
chosen item.
One simple approach would be:
----
set listStr to "Dialog Box 1
Dialog Box 2
Dialog Box 3
Dialog Box 4"
set theDialog to (choose from list paragraphs of listStr)
if theDialog is false then error number -128
-- theDialog, a single-item list, is automatically coerced for 'offset'
set chrOffset to the offset of theDialog in listStr
set listIndex to (count paragraphs of text 1 thru chrOffset of listStr)
display dialog item listIndex of {"Good Morning", "Good Afternoon",
"Good Evening", "GoodNight"}
----
The caveat here is that if one of the later paragraphs happens to be a
substring of an earlier one, the earlier option will be carried out. To
prevent this, you can take the returns at each end of the paragraph into
account - which makes the script slightly less pretty:
----
set listStr to return & "Dialog Box 1
Dialog Box 2
Dialog Box 3
Dialog Box 4" & return -- note the added returns
set theDialog to (choose from list paragraphs 2 thru -2 of listStr)
if theDialog is false then error number -128
-- theDialog is coerced to string in the concatenation with the first
return
set chrOffset to the offset of (return & theDialog & return) in listStr
set listIndex to (count paragraphs of text 1 thru chrOffset of listStr)
- 1
display dialog item listIndex of {"Good Morning", "Good Afternoon",
"Good Evening", "GoodNight"}
----
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.