編寫基礎(chǔ)
- 定義變量用set...to... 獲取變量用get
set value to 5
get value
- 計算
set a to 3
set b to 5
set c to ab
-- 常用的運算符有:+产上、-、、/、^、 對的血淌,加減乘除乘方
- 注釋用--
-- 這是注釋
- 用tell告訴應用程序怎么做end tell 結(jié)束這個過程
-- 清空廢紙簍
tell application "Finder"
empty the trash
end tell
- 打開腳本編輯器,文件->打開字典->選擇應用,可以查看各種屬性和命令
- AppleScript沒有結(jié)構(gòu)體,所以很難做復雜的工程,定義函數(shù)并調(diào)用
-- 聲明
on sayHello ()
say "hello"
end sayHello
-- 調(diào)用
sayHello()
字符串操作
- 拼接 使用 & 拼接字符串
-- 字符串與字符串拼接
set str to "1234" & "4567"
-- 字符串與列表拼接
set str to "hello" & {"world"}
-- 列表與列表拼接
set str to {"hello"} & {"world"}
- 查看字符串長度 the length of 用來獲取字符串的長度
set theLength to the length of "Neal"
- 類型轉(zhuǎn)換
-- 字符串與數(shù)字類型轉(zhuǎn)換
set strToNumber to "16" as number
set numToStr to 12 as string
-- 字符串與列表轉(zhuǎn)換
set strList to "111" as list
- 分割
-- 使用itemized關(guān)鍵字可以將字符串分割成字符并組成新的列表
set itemized to every character of strAndStr
-- 通過AppleScript's text item delimiters 指定分割符,通過every text item of實現(xiàn)分割
set AppleScript's text item delimiters to " "
set listAfterDelimiter to every text item of strAndStr
列表
- 基本操作
-- 聲明列表
set firstList to {11, 230, "black", 789, 453, "world", 111}
-- 更改列表指定位置元素
set item 2 of firstList to "hello"
set the third item of firstList to "234"
-- 隨機獲取列表中一個元素
set randomX to some item of firstList
-- 獲取列表最后一個元素
set lastItem to the last item of firstList
-- 獲取列表第一個元素
set firstItem to the first item of firstList
-- 負數(shù)表示從列表尾端獲取數(shù)據(jù)
set temp to item -2 of firstList
-- 獲取列表2到4位返回列表
set shortList to items 2 through 4 of firstList
-- 逆向獲取子列表
set reversedList to reverse of firstList
-- 獲取列表數(shù)量
set listCount to the count of firstList
-- 為列表末尾添加數(shù)據(jù)
set the end of longList to 10
Record
- 基本操作
-- 聲明
set tempRecord to {name: "張三豐", age: 100, desc: "太極"}
-- 更改
set tempName to the name of tempRecord
-- 使用value創(chuàng)建新的Record
set newRecord to {newName: name of tempRecord}
-- 數(shù)量
set recordCount to the count of tempRecord
條件語句
if 條件 then
else if 條件 then
else
end if
-- 條件判斷 英文好點是可以蒙出來的
-- 以...開頭
begins with 或starts with
-- 不以。。压昼。開頭
does not start with
-- 以求冷。。窍霞。結(jié)束
ends with
-- 相等
is equal to
-- 在匠题。。但金。之前韭山。比較兩字符的ascii碼
comes before
-- 在。冷溃。钱磅。之后。比較兩字符的ascii碼
comes after
-- 在似枕。盖淡。。之中
is in
-- 不在凿歼。褪迟。。中
is not in
-- 包含
contains
-- 不包含
does not contain
循環(huán)語句
- 1
set sum to 0
set i to 0
repeat 100 times
set i to i + 1
set sum to sum + i
end repeat
- 2
repeat with counter from 0 to 10 by 1
display dialog counter
end repeat
- 3
repeat while counter = 10
display dialog counter as string
set counter to counter + 2
end repeat
- 4
repeat until counter = 10
display dialog counter as string
set counter to counter + 2
end repeat
- 5
set aList to {1, 2, 3, 4}
repeat with anItem in aList
display dialog anItem as string
end repeat
參考
https://www.aliyun.com/jiaocheng/363028.html?spm=5176.100033.1.19.46de67eeTLYpbB
https://www.aliyun.com/jiaocheng/363027.html?spm=5176.100033.1.19.7a156d1fl5Frpv