三、AppleScript的循环与逻辑分支

条件语句

if true then
    -- do something
end if 

if false then
    -- do something
end if 

例:

set a to 20
set b to 30

if a = b then
    set c to 10
else
    set c to 20
end if
2144458-c8cec7e1700037ba.png
0ACA655C-E5B7-47B8-B354-5D509CB6FDA4.png

如何判断是否为“真”(true)

数的比较运算符
= is (or, is equal to) 等于
> is greater than 大于
< is greater than 小于
>= is greater than or equal to 大于等于
<= is less than or equal to 小于等于

并支持反义符号,如不大于,is not greater than。不等于 /= 或 is not

字符串的比较运算符
begins with (or, starts with)  以……开头
ends with                    以……结尾
is equal to                  一致
comes before                 在……之前
comes after                  在……之前
is in                       在……之中
contains                     包含
反义运算符
does not start with 不以……开头
does not contain    不以……结尾
is not in          不在……之内
等等。

比较运算符 "comes before" 和 "comes after" 对字符串逐字母比较,区分大小写

if "Jack" comes after "Rose" then
    set a to 123
else
    set a to 456
end if
2144458-e1eef68ef4b6ab84.png
25BB544A-B9F9-4A72-883C-A32CCD8EE95B.png
忽略空格
set stringA to "Ja c k"
set stringB to "Jack"
ignoring white space
    if stringA = stringB then beep
end ignoring
列表的比较运算符
begins with 以……开头
ends with   以……结尾
is equal to 一致
is in       在……之中
contains    包含

例子

set listA to {"a", "b", "c"}
if "a" is in listA then
    set c to 123
else
    set c to 456
end if
2144458-1d5c9dd0301cc13a.png
3762713E-DE96-4DBF-8CBC-47A229660B11.png
记录的比较运算符
is equal to(也可以使用 =)    一致
contains                  包含
set recordA to {name:"Jack", age:20}
-- name of recordA is "Jack"
if recordA contains {name:"Jack"} then
    set c to 123
end if
2144458-10b10deb6892654d.png
3B4530DD-9373-4716-854A-48893B344678.png

布尔数据的 与、或、非

and
set x to true
set y to true
set z to (x and y)
2144458-4a1d31cb17d21338.png
48C6CD84-163D-47BC-8C3B-047191B94E94.png
or
set x to true
set y to false
set z to (x or y)
2144458-87dd2f9ed7187f60.png
C160CC9A-BA90-44DC-BE26-BBEE12083375.png
not
set x to not true
set y to false
if x = y then
    set z to 123
end if
2144458-253aea06bcf5dc45.png
DC3E75D4-9DAE-448D-A239-C8D56A217967.png

循环

repeat 重复 重复次数必须是整数,例

set repetitions to 2
-- repeat 2 times
repeat repetitions times
    say "Hello world!"
end repeat

满足条件后重复执行下一步

set isRun to false
-- until 与 while 判断结果相反
repeat while isRun is false
    say isRun
end repeat

从1读到5,步长默认为1

repeat with i from 1 to 5
    say i
end repeat

by 2 设置步长为2,如下

repeat with i from 1 to 5 by 2
    say i
end repeat
repeat with aItem in itemList

end repeat

跳出循环, exit repeat 相当于break

猜你喜欢

转载自blog.csdn.net/weixin_34367845/article/details/87417704