一.基本數(shù)據(jù)類型
- Boolean(布爾型) 僅僅包含兩個(gè)值: True和False
- Number(數(shù)字型)
- Integer(整型)
- Real(實(shí)數(shù),小數(shù))
如:1,2点晴,1.0,1.1悯周,3.14粒督,-1.56 - Text(文本型) 和 String(字符串型),類型是一致
- Date(日期型) 如 date "2009年8月30日星期日 下午12:31:34"。此格式的具體形式由“系統(tǒng)偏好設(shè)置-語言與文本”的相關(guān)設(shè)置決定禽翼。
- Constant(常量型) 如 yes, no, ask 這些常量可以是已經(jīng)被AppleScript預(yù)定義的屠橄,也可以是用戶定義的不可變變量。這種 類型的數(shù)據(jù)一經(jīng)確定不可更改闰挡。此外可以認(rèn)為所有關(guān)鍵字都是常量型的數(shù)據(jù)
- List (列表型) 如:{1,2,3}锐墙,{{1,2},{a,b,c},},{1,1.9, "text"}
列表型數(shù)據(jù)由{}包裹长酗,一個(gè)列表中可以再包含列表,形成多維列表茉继,列表里的具體數(shù) 據(jù)可以是同類型的蚀乔。 - Record (記錄型) 如:{firstName:"iDoraemon", lastName:"Nathan"} 記錄就是帶有名稱的列表烁竭。記錄中的每一項(xiàng)都有名稱(標(biāo)識(shí)符)。我們可以認(rèn)為L(zhǎng)ist是每個(gè)數(shù)據(jù)都是匿名的Record吉挣。Record也可以進(jìn)一步包含另一個(gè)Record派撕。
此例中,包含兩個(gè)Text型數(shù)據(jù) "iDoraemon"和 "Nathan"睬魂,它們的標(biāo)識(shí)符分別是firstName和lastName腥刹。通過of關(guān)鍵字可以得到想要的數(shù)據(jù):firstName of {firstName:"iDoraemon", lastName:"Nathan"} 上面這行代碼返回 "iDoraemon"衔峰。 - 確定數(shù)據(jù)類型-Class of 要確定一個(gè)數(shù)據(jù)到底是什么類型的,使用Class of語句
class of "string" 此代碼得到結(jié)果text蛙粘。
二.強(qiáng)制數(shù)據(jù)類型轉(zhuǎn)換
as 關(guān)鍵字
用as關(guān)鍵字即可實(shí)現(xiàn)強(qiáng)制類型轉(zhuǎn)換垫卤。用法:數(shù)據(jù) as 類型 請(qǐng)注意,強(qiáng)制類型轉(zhuǎn)換有時(shí)會(huì)丟失精度舔痕。
--文本類型轉(zhuǎn)數(shù)字類型
"1.99" as real --得到Real類型的1.99评抚,而原來的數(shù)據(jù)是Text(因?yàn)閹в幸?hào))。
"1.99" as integer --得到Integer類型的2伯复,精度丟失!
"1" as real --得到Real類型的1.0慨代,自動(dòng)提升精度!
--轉(zhuǎn)換成List類型
"text" as list --得到{"text"}
1.99 as list --得到{1.99}
{a:1, b:2} as list --得到{1, 2},精度丟失(標(biāo)識(shí)符丟失)!
--錯(cuò)誤舉例
"text2" as number 1 as record --錯(cuò)誤!Text中包含了無法轉(zhuǎn)換成數(shù)字的字符啸如。
{1, 2} as record --錯(cuò)誤!無法獲得標(biāo)識(shí)符
三.運(yùn)算符
- 邏輯運(yùn)算符
and(邏輯與)侍匙,or(邏輯或)和not(邏輯非)。 三個(gè)對(duì)參與運(yùn)算的數(shù)據(jù)要求均為Boolean型叮雳,not為單目運(yùn)算符想暗。 -
& 運(yùn)算符 簡(jiǎn)單來說這個(gè)是合并運(yùn)算符。支持任何類型數(shù)據(jù)帘不。
記住“&”運(yùn)算符的三個(gè)規(guī)則:
“&”左邊的數(shù)據(jù)類型為Text(文本型)時(shí)说莫,結(jié)果為Text;存在報(bào)錯(cuò)可能
“&”左邊的數(shù)據(jù)類型為Record(記錄型)時(shí),結(jié)果為Record;存在報(bào)錯(cuò)可能
“&”左邊的數(shù)據(jù)類型為其他時(shí)寞焙,結(jié)果為L(zhǎng)ist類型
Text" & 1 --結(jié)果:"Text1" (Text類型)
1 & "Text" --結(jié)果:{1, "Text"} (List類型)
{name:"a"} & "b" --結(jié)果:{name:"a", ?class ktxt?:"b"} (Record類型储狭,第二個(gè)元素匿名)
3 & {name:"a"} --結(jié)果:{3, "a"} (List類型互婿,且丟失標(biāo)識(shí)符)
{1, 2, 3} & {4, 5, 6} --結(jié)果:{1,2,3,4,5,6} (List類型-一維的)
{1, 2, 3} & 4 & 5 & 6 --結(jié)果:同上
{a:1, b:2} & {c:3} --結(jié)果:{a:1, b:2, c:3} (Record類型-一維的)
--錯(cuò)誤舉例
"Text" & {name:"a"} --錯(cuò)誤!無法將Record類型數(shù)據(jù)轉(zhuǎn)為文本
{name:"a"} & 3 --錯(cuò)誤!無法將Integer轉(zhuǎn)換為Record
注釋
--行尾注釋
(" ")塊注釋
變量賦值
tell application "Finder"
set myResult to (name of folders 2 through 3 of desktop)
end tell
5.全局變量和局部變量
在定義變量之前,增加“Global name”(全局)或者“Local name”(局部)語句 可以強(qiáng)制控制變量的生存空間晶密,其中name為變量名稱擒悬。
set message to "最先定義的" -- 定義局部變量
run aNewScript --運(yùn)行腳本
displayA("作為形式參數(shù)定義的") --激活事件處理器displayA,并傳遞參數(shù)
--腳本對(duì)象定義開始--
script aNewScript
set message to "在腳本對(duì)象里定義的" --重定義局部變量 message (重載)
display dialog message
end script
-- 事件處理器定義開始
on displayA(message) --displayA是事件處理器的名稱
display dialog message --顯示一個(gè)包含內(nèi)容message的對(duì)話框
end displayA
數(shù)據(jù)共享
基本數(shù)據(jù)類型的共享機(jī)制(不含Record 和 list)---值傳遞
Record 和list -- 值引用 (要想采用值傳遞,需要關(guān)鍵字copy)
set a to 1
set b to a
display dialog "賦值的結(jié)果: a = " & a & ";b = " & b
--值傳遞
set c to {1,2,3,4}
set d to c
-- 值引用
set f to {1,2,3,4}
set h to 1 --需要提醒的是稻艰,使用copy之前必須先定義
copy f to h --給變量h賦值為f的值(List型)
set item 1 of h to 0 --修改List h中的第一個(gè)值為0
屬性
屬性和變量的區(qū)別
屬性和變量的根本區(qū)別在于其穩(wěn)固性(Consistence)懂牧。所謂穩(wěn)固性,即腳本退出后其值是否保持不變尊勿。屬性在腳本退出運(yùn)行后僧凤,仍然記錄下它最后的值,并且下一次運(yùn)行時(shí)可以 被調(diào)出元扔。屬性的另一個(gè)特點(diǎn)是沒有全局和局部之分躯保,所有屬性都是全局的。
property countTime : 0 --請(qǐng)注意澎语,這個(gè)語句不是用來賦值的途事,而是用來初始化一個(gè)屬性的!
set countTime to countTime + 1
display dialog "這是第" & countTime & "運(yùn)行本腳本"
四.流程控制語句
?簡(jiǎn)單形式:tell referenceToObject to statement
?復(fù)合形式:以tell referenceToObject開始,end tell結(jié)尾擅羞。中間包含命令語句尸变。
其中:refenrenceToObject為需要控制的對(duì)象,如一個(gè)應(yīng)用程序减俏、一個(gè)窗口召烂。 statement為命令語句,包含至少一個(gè)命令
tell front window of application "Finder" to close --這是簡(jiǎn)單形式
tell application "Finder"
close front window
end tell
if -- else 控制語句
set mark to 80
if mark is greater than 60 and mark is less than or equal to 80 then
set response to "You passed the exam"
else if mark is greater than 80 then
set response to "Congratulations ! You're smart"
else
set response to "Sorry , you lost it"
end if
display dialog response
循環(huán)控制
--循環(huán)多少次
repeat n times
-- do something
end repeat
--直到循環(huán)
repeat until boolean
end repeat
--當(dāng)型循環(huán)
repeat while boolean
end repeat
控制循環(huán)次數(shù)
repeat with
loopVariable
fromstartValue
tostopValue
bystepValue
end repeat
其中stepValue可省略娃承,省略時(shí)為默認(rèn)為1;loopVariable無需事先定義奏夫。 其執(zhí)行流程如下圖,請(qǐng)注意各個(gè)變量的用途
repeat with a from 1 to 100
set b to a
end repeat
List 類型循環(huán)
repeat with loopValue is list
--do something
end repeat
在循環(huán)體中历筝,loopVariable將依次得到item 1 of list, item 2 of list....這樣的指針(指向 list中的第幾項(xiàng))酗昼。請(qǐng)?zhí)貏e注意是指針!如果要得到list中的具體內(nèi)容,使用contents of loopVariable來獲得漫谷。
set myList to {"a", "b", "c"}
repeat with i in myList
display dialog (contents of i)
set contents of i to "Oh"
end repeat
** Considering/Ignoring語句(用于文本比較)**
此語句可以在比較文本時(shí)仔雷,指定忽略或考慮某一屬性(如大小寫,空格等等)舔示。
considering
attribute1
but ignoringattribute2
--compare texts
end considering
case diacriticals hyphens numeric strings punctuation white space
上面代碼含義是考慮attribute1
但忽略attribute2
其中 but ignoring attribute2可以省略;
considering和ignoring位置可以互換,但是end considering也要相應(yīng)改成end ignoring电抚,
當(dāng)然你可以選擇最簡(jiǎn)略的方式──只輸入end惕稻,讓編譯器自己補(bǔ)上considering/ignoring。 attribute應(yīng)該為下面列表中的任意一個(gè):
set a to "1.10.1"
set b to "1.9.4"
if a is greater than b then
display dialog "a greater than b"
end if
considering numeric strings
if a is greater than b then
display dialog "a greater than b"
end if
end considering
四.基本用戶交互
- 1.簡(jiǎn)單對(duì)話框 和 輸入框
display dialog "這是一個(gè)對(duì)話框" buttons {"好的", "不接受"} default button "好的" with title "測(cè)試標(biāo)題" with icon note giving up after 5
? buttons 緊跟List型參數(shù)蝙叛,指定對(duì)話框擁有的按鈕名稱俺祠,注意最多為三個(gè)
? default button 緊跟text型參數(shù)的某一個(gè)按鈕名稱,設(shè)定默認(rèn)按鈕
? with title 緊跟text ,指定對(duì)話框的標(biāo)題(省略時(shí)無標(biāo)題)
? with icon 緊跟stop/note/caution中的一個(gè)或者file類型的路徑蜘渣,指定顯示的圖標(biāo)
? giving up after 緊跟number型的整數(shù)淌铐,指定在number秒后自動(dòng)消失對(duì)話框。
- 2.帶有輸入框的對(duì)話框
display dialog "帶有輸入框的對(duì)話框" default answer "默認(rèn)回答"
只需要添加default answer (+text)就可使 普通對(duì)話框升級(jí)為輸入框蔫缸。其中text可以用空文 本(直接鍵入兩個(gè)引號(hào));添加hidden answer true命令腿准,可以隱藏輸入文本(輸入密碼時(shí) 用)。此外拾碌,前面介紹的關(guān)于簡(jiǎn)單對(duì)話框的幾 個(gè)參數(shù)仍然可用吐葱。
display dialog "這是一個(gè)對(duì)話框" buttons {"好的", "不接受"} default answer "" default button "好的" with title "測(cè)試標(biāo)題" with icon caution giving up after 5 with hidden answer
- 3.??對(duì)話框
display alert "這是一個(gè)警告" message "警告的信息" as warning 結(jié)果如左下圖。
其中: message參數(shù)指定了補(bǔ)充信息(在對(duì)話框中以小字顯示)校翔,as warning/critical/ informational指定了對(duì)話框的重要性(表面上看起來就是圖標(biāo)不同)弟跑。此外可用簡(jiǎn)單對(duì)話框 中的buttons(指定按鈕), give up after(自動(dòng)超時(shí)消失)參數(shù)。
display alert "這是一個(gè)警告" message "這是主題信息" as critical
- 4.列表選擇對(duì)話框
choose from list {"備選一", "備選二", "備選三"}
with title "這是一個(gè)列表選擇框"
with prompt "請(qǐng)做 出選擇!"
default items {"備選二"}
with empty selection allowed and multiple selections allowed
參數(shù)說明
? 直接參數(shù) 緊跟List類型參數(shù),包含所有備選項(xiàng)
? title 緊跟text ,標(biāo)題
? prompt 提示語
? default items 緊跟list 默認(rèn)選中項(xiàng)目
? empty selection allowed 允許不選
? multiple selections allowed 允許多選
choose from list {"dawei", "libai", "lilei"} with title "名字選擇器" with prompt "請(qǐng)選擇名稱" default items {"lilei"} with empty selection allowed and multiple selections allowed
- 5.文件選擇對(duì)話框
這個(gè)對(duì)話框很類似于一般軟件的另存為對(duì)話框防症。要求用戶指定一個(gè)將來用于保存信息
的文件孟辑,請(qǐng)注意,Choose file name命令并不會(huì)創(chuàng)建文件蔫敲,它的返回值是file類型的饲嗽,包含完 整路徑。完整語法如下
其中prompt指定提示信息燕偶,default name指定默認(rèn)名稱喝噪,default location指定默認(rèn)存儲(chǔ)位置, 需要file類型的參數(shù)指么,三個(gè)參數(shù)均可以省略酝惧。上面這段代碼執(zhí)行結(jié)果如下左圖
choose file name with prompt "指定提示信息" default name "默認(rèn)名稱" default location file "Macintosh HD:Users"
choose file name with prompt "選定指定文件" default name "默認(rèn)名稱" default location file "Macintosh HD:Users"
選取文件夾對(duì)話框
完整語法如下:
其中prompt和default location參數(shù)同Choose File Name;另外invisibles指定顯示隱藏 文件,multiple selections allowed可以多選伯诬,showing package contents顯示包內(nèi)容晚唇,省略時(shí) 則不顯示隱藏文件/不可多選/不顯示包內(nèi)容。
choose folder命令返回值為alias或者是List(由alias構(gòu)成盗似,當(dāng)允許多選時(shí))
選取文件Choose File
選取文件對(duì)話框幾乎和選取文件夾對(duì)話框一樣哩陕,擁有幾乎相同的參數(shù)、語法和返回值
類型赫舒,只是多了一個(gè)of type可選參數(shù)(后加List悍及,list中應(yīng)包含一個(gè)或多個(gè)text,指定允許選
擇的文件類型)
choose file of type {"txt"}
注意:Choose Floder所有的參數(shù)接癌,Choose File命令也可用心赶,這里不再列舉。
choose file of type {"jpg"} with prompt "選取文件" default location file "Macintosh HD:Users"
五.錯(cuò)誤處理
- 1.try 錯(cuò)誤處理
try
display dialog "你確定在桌面上創(chuàng)建 \"AppleScript\" 文件夾嗎?"
tell application "Finder"
make new folder at desktop with properties {name:"AppleScript"}
end tell
on error eText number eNum
if eNum = -48 then
display dialog "發(fā)生文件錯(cuò)誤,錯(cuò)誤內(nèi)容是
" & eText
else if eNum = -128 then
display dialog "您按下了取消按鈕,錯(cuò)誤內(nèi)容是:
" & eText
end if
end try
- 2.時(shí)間超時(shí)
在wait for something中最多只等待x秒缺猛,如果x秒過后缨叫,仍然沒 有得到預(yù)期響應(yīng)就會(huì)拋出超時(shí)錯(cuò)誤椭符。需要說明的是:x是可以大于120的!這樣就突破了 AppleScript默認(rèn)的“耐心”,有時(shí)候也是有用的耻姥。
with timeout of 3 seconds
display dialog "nihao"
end timeout
display dialog "你確實(shí)在三秒內(nèi)做出了響應(yīng)"
六.文件操作
相對(duì)路徑 path to命令
在Mac OS X中销钝,有很多文件夾具有特殊地位,如用戶的文檔文件夾
(Documents)琐簇、系統(tǒng)的資源庫(Library)蒸健、應(yīng)用程序文件夾(Application)等等,這些文 件夾經(jīng)常會(huì)被使用到鸽嫂,而用絕對(duì)路徑來表達(dá)它們的位置顯然會(huì)產(chǎn)生可移植性問題纵装。 AppleScript中提供的path to命令就是用來解決這個(gè)問題的:請(qǐng)看下面的示例代碼。
可以使用path to命令來獲得的文件夾非常多据某,如application support橡娄、applications folder、desktop癣籽、documents folder挽唉、downloads folder、system folder等等(請(qǐng)參閱 AppleScript字典)筷狼。from指定了文件夾所屬的域瓶籽,之所以要指定域是因?yàn)轭愃瀑Y源庫 (Library)文件夾在系統(tǒng)中存在不只一個(gè)(用戶的、系統(tǒng)的埂材、本地的等)塑顺。常用user domain(默認(rèn)缺省值)、system domain和local domain俏险。
POSIX路徑和POSIX file類型
POSIX路徑全稱Portable Operating System Interface of Unix严拒,是IEEE的標(biāo)準(zhǔn)之一。
POSIX路徑(path)只是POSIX中一個(gè)很小的部分竖独,該路徑用斜杠(/)分隔層次──注意 不是windows的反斜杠()裤唠。它具有簡(jiǎn)明性和相對(duì)性的特點(diǎn),也可以用來避免絕對(duì)路徑的致 命缺陷
POSIX path of alias "Macintosh HD:System:Library:CoreServices:Finder.app:" --返回"/System/Library/CoreServices/Finder.app/"
POSIX file "/Users/Nathan/Desktop/example.txt"
--返回file "Macintosh HD:Users:Nathan:Desktop:example.txt"
- 1.文件讀取
set myfile to alias "Macintosh HD:Users:chengguangfa:Desktop:nihao.txt"
read myfile
在這里有必要提一下文件結(jié)尾(end of file)莹痢,如果文件是空的种蘸,在嘗試讀取時(shí),會(huì) 讓AppleScript拋出文件結(jié)尾錯(cuò)誤竞膳,因此航瞭,在讀取文件之前,應(yīng)該養(yǎng)成先確定文件長(zhǎng)度的習(xí) 慣坦辟,AppleScript中使用get eof命令(后直接跟alias類型的參數(shù))沧奴。
- 示例1 --- 創(chuàng)建一個(gè)新的文件夾
tell application "Finder"
make new folder at desktop
end tell
2.創(chuàng)建100個(gè)子文件夾
tell application "Finder"
make new folder at desktop with properties {name:"test"}
-- 循環(huán)
repeat with a from 1 to 100
make new folder at folder "test" of desktop with properties {name:a as string}
end repeat
end tell
- 獲取FInder 文件列表
tell application "Finder"
every file of desktop
files of desktop
every folder of desktop
folders of desktop
name of every file of desktop
end tell
提取符合條件的文件夾
tell application "Finder"
every file of desktop whose name contains "a"
end tell