Re: Choose from list
Re: Choose from list
- Subject: Re: Choose from list
- From: has <email@hidden>
- Date: Tue, 7 May 2002 01:10:05 +0100
Matthew Stuckwisch wrote:
>
> 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?
Sure:
======================================================================
on getParagraphOffset(theString, findString)
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to get (return & findString
[NO-BREAK]& return)
set tempString to (return & theString & return)'s first text item
set AppleScript's text item delimiters to return
set theOffset to count tempString's text items
set AppleScript's text item delimiters to oldTID
if theOffset is greater than (count theString's paragraphs) then
[NO-BREAK]return 0
theOffset
end getParagraphOffset
======================================================================
Returns the paragraph index of the first matching paragraph, or 0 if no
match is found.
As regards the original question: I would most likely use the loop method
myself because it's simplest. I doubt the time taken is significant: it'll
almost certainly take longer for the user to make their selection than for
the script to work out what that selection was.
One flaw in Paul's handler: it won't return anything if no match is found -
this would cause an unexpected error in general use. Here's a tweaked copy
that returns 0 if no match is found (i.e. in keeping with the behaviour of
Standard Additions' "offset" command):
======================================================================
on UniqueIndex(aList, anItem)
repeat with i from 1 to (count aList)
if anItem = item i of aList then return i
end repeat
return 0
end UniqueIndex
======================================================================
BTW, the TID-based method is case-sensitive [1]; the loop-based routine
follows AS's considering/ignoring rules.
HTH
has
[1] Case-insensitive, tid-based routines are possible, but are rather more
complex to write and slower to execute.
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
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.