AppleScript Programming/Advanced Code List/Repeat

< AppleScript Programming < Advanced Code List

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/

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.