效率篇——AppleScript入门2

AppleScript循环:

(*
 *  1. repeat 死循环
 *)
set num to 0
display dialog num

-- 最简单循环,是死循环
repeat
	set num to num + 1
	display dialog num
	if num ≥ 10 then
		exit repeat
	end if
end repeat

 

(*
   2.限定次数的循环 repeat n times  其中n 可以是变量,可以是常量
*)

-- 创建全局变量 num
set num to 0
global num

set n to 6
-- 限定循环n次
repeat n times
	run dialogScript
end repeat

-- 创建自定义脚本
script dialogScript
	set num to num + 1
	display dialog num
end script

 

(*
   3.依次打印 0 ~ 9
*)

set num to 0

-- 直到型循环
(*
repeat until num ≥ 10
	display dialog num
	set num to num + 1
end repeat
*)

-- 当型循环
repeat while num < 10
	display dialog num
	set num to num + 1
end repeat

 

-- 变量循环 
-- 优势:可以调整跨度,by num 可以省略,默认是 1
repeat with i from 0 to 10 by 3
	display dialog i
end repeat

 

-- List循环 
set students to {"张三", "李四", "王五"}

-- 注意这里的i是指针,想要获取内容要用 contents of i 取得
repeat with i in students
	display dialog contents of i
	set (contents of i) to "小明"
end repeat

display dialog "" & students

 

posted @ 2015-04-12 18:02  过目不忘  阅读(459)  评论(0编辑  收藏  举报