• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Impossible "If"?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Impossible "If"?


  • Subject: Re: Impossible "If"?
  • From: Andy Bachorski <email@hidden>
  • Date: Fri, 11 May 2001 10:00:25 -0700

On Thu, 10 May 2001 23:51:26, Ehsan Saffari <email@hidden> wrote:
>
> Why don't the ifs ever come true?
>
> set x to {3, 7, 11}
> repeat with h in x
> display dialog h
> if h = 3 then
> --do this
> else if h = 7 then
> --do that
> else if h = 11 then
> --do other
> end if
> end repeat

The more complete answer is that in the above script, for each iteration of
the repeat loop, variable h will contain a reference to each successive item
in the list x. As was stated in other replies, you can use the 'contents
of' operator to get at the value h is a reference to.

The confusion with this form of repeat loop is that sometimes it works
(because AppleScript is able to coerce the reference to the required value
type) and sometimes it doesn't.

For this reason, I've adopted the following convention when I need to
iterate over the contents of a list.

set theList to {3, 7, 11}
set listCount to count theList

repeat with i from 1 to listCount
set h to item i of theList

display dialog h
if h = 3 then
--do this
else if h = 7 then
--do that
else if h = 11 then
--do other
end if
end repeat

This way I'm always dealing with values, and don't have to worry about
getting the contents of a reference to the value I'm after.

As an aside, another form of repeat I've seen is

set theList to {3, 7, 11}
repeat with i from 1 to count of theList
set anItem to item i of theList
-- do something
end

The above repeat statement will cause the count command to be executed for
each iteration of the repeat statement. Not (too) big of a issue in the
above case where the list is a local variable. But consider the following

tell app "Finder"
repeat with i from 1 to count of files in someFolder
set anItem to file i of someFolder
-- do something
end repeat
end tell

In this script, a count command will be sent to the Finder for each
iteration of the repeat loop. Depending on how many items are in the
folder, this could significantly slow down the script. Better to count once
before the repeat.

Then again, counting each time through could allow you to do something
interesting. Or it could cause problems.

andy b

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Andy Bachorski Core Technology
Grizzled Wizard Adobe Systems Incorporated
email@hidden 345 Park Avenue
408.536.3013 San Jose CA 110
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


  • Follow-Ups:
    • [OFF] Re: Impossible "If"?
      • From: Emmanuel <email@hidden>
    • Re: Impossible "If"?
      • From: Paul Berkowitz <email@hidden>
  • Prev by Date: Applescripting an export from Palm for FIleMaker
  • Next by Date: Re: File signatures
  • Previous by thread: Re: Impossible "If"?
  • Next by thread: Re: Impossible "If"?
  • Index(es):
    • Date
    • Thread