AppleScript Programming/Advanced Code List/Repeat

With command repeat you can create repetative behavior, that often will shorten the code a lot, if done correctly.

Its basic format is this:

repeat 10 times
	say "Hello."
end repeat

And it can be used to do a specific task for every item you drop into a script that has been turned into a program.

on open (ListItem)
	repeat with thisItem in ListItem
		-- bunch of kick*** code
	end repeat
end open

One example of actual script that utilizes repeat-function can be found here.

While, here is one example of code that will wait until a page in Safari is fully loaded:

tell application "Safari" to activate -- Brings Safari frontmost application.
repeat
	tell application "Safari"
		if source of document in window frontmost contains "</html>" then
			tell application "Script Editor" to activate
			exit repeat -- This will quit the loop.
		else
			delay 1 -- Will wait for 1 second.
		end if
	end tell
end repeat

And a useful source can be found from here: www.macobserver.com

The entire series of AppleScript columns at Mac Observer are here www.macobserver.com/tips/applescript/