我本來以為之前幾篇文章已經(jīng)把AppleScript的基礎(chǔ)講的差不多了桑谍,但是自己研究過文檔后日杈,發(fā)現(xiàn)還是有很多需要補(bǔ)充的嗤栓,所以又加了一篇。
1.預(yù)定義變量
就是一些特殊的關(guān)鍵字治唤,類似于其他語言中的self棒动,return等,有固定的含義宾添;千萬不要用它來自定義變量船惨。
?result:記錄最近一個(gè)命令執(zhí)行的結(jié)果,如果命令沒有結(jié)果,那么將會得到錯誤
?it:指代最近的一個(gè)tell對象
?me:這指代段腳本柜裸。用法舉例path to me返回本腳本所在絕對路徑
?tab:用于string,一個(gè)制表位
?return:用于string,一個(gè)換行
2.字符串比較:Considering/Ignoring語句
在AppleScript的字符串比較方式中,你可以設(shè)定比較的方式:上面considering和ignoring含義都是清晰的粱锐,一個(gè)用于加上xx特征疙挺,一個(gè)用戶忽略某個(gè)特征;一個(gè)特征就是一個(gè)attribute怜浅。
atrribute應(yīng)該為列表中的任意一個(gè):
case 大小寫
diacriticals 字母變調(diào)符號(如e和e?)
hyphens 連字符(-)
numeric strings 數(shù)字化字符串(默認(rèn)是忽略的),用于比較版本號時(shí)啟用它铐然。
punctuation 標(biāo)點(diǎn)符號(,.?!等等,包括中文標(biāo)點(diǎn))
white space 空格
3.列表選擇對話框
AppleScript是有選擇對話框的,想想也是應(yīng)有之義海雪;下面是一個(gè)最簡單的選擇框:
display alert "這是一個(gè)警告" message "你上學(xué)遲到了" as warning choose from list {"這是第一個(gè)妞", "dsfggf"} with title "選擇框" with prompt "請選擇選項(xiàng)"
選擇框有以下參數(shù):
? 直接參數(shù) 緊跟list類型參數(shù)锦爵,包含所有備選項(xiàng)
? title 緊跟text,指定對話框的標(biāo)題
? prompt 緊跟text,指定提示信息
? default items 緊跟list奥裸,指定默認(rèn)選擇的項(xiàng)目
? empty selection allowed 后緊跟true表示允許不選? multiple selections allowed 后緊跟true表示允許多選
4.文件選擇對話框
選取文件名稱Choose File Name
注:該方法不創(chuàng)建文件险掀,只是返回一個(gè)路徑
choose file name with prompt "指定提示信息"
選取文件夾Choose Folder
choose folder with prompt "指定提示信息" default location file "Macintosh HD:Users" with invisibles, multiple selections allowed and showing package contents
注:其中prompt和default location參數(shù)同Choose File Name;另外invisibles指定顯示隱藏 文件,multiple selections allowed可以多選,showing package contents顯示包內(nèi)容,省略時(shí) 則不顯示隱藏文件/不可多選/不顯示包內(nèi)容
選取文件Choose File
注:除了type其它參數(shù)相同.
choose file of type {"txt"}
5.Alias類型
Alias指向文件的唯一ID,一般都用它操作.
set myAlias2 to alias "Macintosh HD:Users:Nathan:Desktop:exam.txt"
path to 用于返回相對路徑
path to documents folder --返回當(dāng)前用戶的“文檔”文件夾絕對路徑alias
path to library folder from system domain --返回系統(tǒng)的“資源庫”絕對路徑alias
6.文件讀取和寫入
文件讀取用read湾宙,允許直接讀日燎狻;但是寫入文件之前必須先打開文件侠鳄,打開文件是open for access FileName,寫入文件用write...to語句埠啃,最后記得關(guān)閉文件close access filePoint。
set myFile to alias "Macintosh HD:Users:Nathan:Desktop:example.txt" read myFile
set aFile to alias "Macintosh HD:Users:Nathan:Desktop:example.txt" set fp to open for access aFile with write permission write "abc" to fp close access fp
最后寫了一個(gè)文件的小例子:
on createMyTxt() --在桌面上創(chuàng)建一個(gè)文件伟恶,內(nèi)部包含一個(gè)txt文件碴开,并向txt內(nèi)插入文件 make new folder at desktop with properties {name:"star"} make new file at folder "star" of desktop with properties {name:"star.txt"} end createMyTxt --向txt文件內(nèi)寫入內(nèi)容 on writeTextToFile() set txtFile to alias "Macintosh HD:Users:star:Desktop:star:star.txt" set fp to open for access txtFile with write permission write "你好,這是一個(gè)txt文件" to fp close access fp end writeTextToFile createMyTxt() writeTextToFile()
希望能夠加深大家對AppleScript的認(rèn)識!