• 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:loops
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re:loops


  • Subject: Re:loops
  • From: Ed Stockly <email@hidden>
  • Date: Thu, 14 Feb 2002 08:59:36 -0800

> can someone explain loops to me? I see them here, I see them there, I see
them in my O'Reilly Applescripting book, I even use them when other people
write them for me... but I do not understand how they work and therefore
cannot write my own without worrying.

There are several forms of repeat loop, each designed to work in a slightly
different way. The most basic is simply:

repeat 3 times
--stuff
end repeat

In this example the script inside the loop would execute the number of times
specified, then go on to the rest of the script.


repeat
--stuff
--if someCondition is true then exit repeat
end if

In this example the loop would repeat forever (an infinite loop) until a
particular condition is true then it would exit the loop and go on to the
next line of AppleScript


repeat with x from 1 to 10
--stuff using x
end repeat

In this case inside the loop a local variable is created (x). The first
time the loop executes the value of that local variable is 1, the second
time 2 ad so on up to 10 after the 10th execution the script goes on to
whatever follows the loop. (Note, you can use any valid variable name in
place of x)

repeat with i in aListOfItems
--stuff using i
end repeat

This is the example you asked about. It is very similar to the previous
version. In this case inside the loop a local variable is created (i). The
first time the loop executes the value of that local variable is whatever is
contained in the first item of aListOfItmes. At the second execution the
second item in the list is used ad so on until each item of the list has
been used, then the script goes on.]

for example, run this script

repeat with thisItem in {"a","b",5}
display dialog "The value of the variable thisItem is: "thisItem
end

In the example you provided this ( every line of document 1 ) actually sends
a commad to bb edit and gets a result in the form of a list. (Be careful
though with these kinds of commands. In Quark if there is only one line it
wouldn't return a list). Each line of the document is an item of the list
that and in your repeat loop the value of the local variable "i" is set to
each line of the document, one by one.

There are a few other repeat variations but those four are what most
scripters use most of the time.

Also, this list is the best place to get answers to questions just like this
one and you'll always find someone willing to help. (Just as you'll always
find someone willing to send you elsewhere.)

HTHAGL

ES




>
> The examples I see are usually ( I quote from the veritable BBedit 6.0 manual)
>
> repeat with i in every line of document 1
> --do stuff here
> end repeat
>
> what is 'i'? Why should that have anything to do with repeats? Is it just a
> character? is it a Roman '1'? does it matter? I cannot seem to find an answer
> to
> this...!
>
> (background: I am writing an AS that will look through my book draft, check in
> Filemaker that the book in question is listed there, mark it as such, then go
> back to the draft in BBedit to look for the next one. I have everythign
> working
> apart from the repeat, which I don't understand. Error handling is simple: FM
> gets upset if it can't find the work in question. I am presuming that when i
> do
> get this loop written, it will still suspend the AS so I can sort out the
> omission. Any comments on this also welcome and gratefully received).


When on the net visit: <http://justapplescript.weblogs.com/>
_______________________________________________
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.

  • Follow-Ups:
    • Re:loops
      • From: Michelle Steiner <email@hidden>
  • Prev by Date: Re: John Delacour (was: monthNumber)
  • Next by Date: "display dialog" time out?
  • Previous by thread: Re: loops
  • Next by thread: Re:loops
  • Index(es):
    • Date
    • Thread