三、AppleScript的循環(huán)與邏輯分支

條件語句

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
0ACA655C-E5B7-47B8-B354-5D509CB6FDA4.png

如何判斷是否為“真”(true)

數(shù)的比較運算符
= 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                    以……結(jié)尾
is equal to                  一致
comes before                 在……之前
comes after                  在……之前
is in                       在……之中
contains                     包含
反義運算符
does not start with 不以……開頭
does not contain    不以……結(jié)尾
is not in          不在……之內(nèi)
等等。

比較運算符 "comes before" 和 "comes after" 對字符串逐字母比較,區(qū)分大小寫

if "Jack" comes after "Rose" then
    set a to 123
else
    set a to 456
end if
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   以……結(jié)尾
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
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
3B4530DD-9373-4716-854A-48893B344678.png

布爾數(shù)據(jù)的 與、或、非

and
set x to true
set y to true
set z to (x and y)
48C6CD84-163D-47BC-8C3B-047191B94E94.png
or
set x to true
set y to false
set z to (x or y)
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
DC3E75D4-9DAE-448D-A239-C8D56A217967.png

循環(huán)

repeat 重復(fù) 重復(fù)次數(shù)必須是整數(shù),例

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

滿足條件后重復(fù)執(zhí)行下一步

set isRun to false
-- until 與 while 判斷結(jié)果相反
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 設(shè)置步長為2,如下

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

end repeat

跳出循環(huán), exit repeat 相當(dāng)于break

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容