Re: Choose from list
Re: Choose from list
- Subject: Re: Choose from list
- From: Arthur J Knapp <email@hidden>
- Date: Mon, 06 May 2002 13:27:58 -0400
>
Date: Sun, 05 May 2002 19:21:09 -0400
>
Subject: Choose from list
>
From: Stephen Swift <email@hidden>
>
I love choose from list because it offers more options instead of the 3
>
button alternative. However, my problem arises when the script has to
>
determine what option the user selected and what course of action to take
>
from there.
>
... (*Speed Freak Alert*) ...
Well, that caught my attention... ;-)
>
... Sure, there is the classic if then
>
statement but is it the most efficient, the fastest? Could I instead use
>
some sort of repeat loop or something?
One of my old "back-burner" projects is a script object wrapper for
working with the "choose from list" dialog. Since it seems unlikely that
I will ever complete this project, I might as well give away one of my
secret techniques for quickly locating the user's selection. ;-)
on StrIndex(i)
set s to return
repeat i times
set s to s & space
end repeat
end StrIndex
set a to {"Item 1" & StrIndex(1), "Item 2" & StrIndex(2),
"Item 3" & StrIndex(3), "Item 4" & StrIndex(4)}
choose from list a
set s to item 1 of result --> cfl returns a list
set x to length of paragraph -1 of s --> offset of selected item in a
>
PS: Upon writing this e-mail I came across another annoyance of mine. How
>
come I have to use a repeat loop to determine what item number my value is?
>
Is there a command that I don't know about that does something like this:
>
>
Item "Dialog Box 1" of the_list
>
(and yes I know AppleScript lists don't work that way... But it would be
>
nice.)
Several osaxen offer list-index commands. You can check out this:
<
http://osaxen.com/index.php>
Several scripters have also implemented vanilla search techniques that
are faster than a simple traversal. Here is a basic search technique:
(* Binary "Contains" Search:
*)
on BCSearch(a, v)
tell a to if it contains {v} then
set l to 1
set r to length
repeat until item l = v
set m to (l + r) div 2
if items l thru m contains {v} then
set r to m
else
set l to m + 1
end if
end repeat
return l
end if
return 0
end BCSearch
In certain situations, we can take advantage of the specific type of
data that we are searching. For instance, every item of your example
list is a string of the same length:
set a to {"Dialog Box 1", "Dialog Box 2", "Dialog Box 3", "Dialog Box 4"}
set m to 12 --> length of "Dialog Box #"
set s to "Dialog Box 2"
set x to (offset of s in ("" & a)) mod (m - 1) --> 2
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www2.linkedresources.com/tools/carthandle.html>
on error number -128
end try
}
_______________________________________________
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.