AppleScript腳本入門

做爲入門文章,這裏儘可能寫的簡單明瞭一點。何況,AppleScript自己也是一個很是容易的腳本語言。node

介紹

做爲腳本語言,其實就是一些指令的集合。這種語言和英語相似,所以也易讀、易寫、易於理解。
他的做用也就是讓計算機程序之間的溝通成爲可能。web

初試AppleScript

腳本就是一系列的指令。
工具:
clipboard.pngspring

基本操做:

command+k ---> 編譯數組

command+r ---> 運行app

control+點擊編輯區上半部分 ---> 提示函數

認識腳本的編寫

直接上代碼:工具

say "Let's study applescript" using "Fred"
beep 2

上面的代碼就是讓計算機發聲。最後發兩次「咚」的聲音。
省略using關鍵字,就是用默認的朗讀語音。學習

tell application "Finder"
    empty the trash
end tell

上面的代碼是調用Finder程序,去清空回收站裏面的垃圾。測試

tell關鍵字調用程序。end tell 結束調用。ui

### 結束語
是否是感受這個腳本很是簡單且好玩?OK,下面接着一點點來捋一捋他的基本操做吧。

## 處理數字

### 變量
AppleScript也是用變量

set x to 25

這裏,就設置了一個x變量,存儲着25這個值。
注意,變量名是一個詞組成,中間不能有空格。固然,咱們不可以使用AppleScript中的保留字來做爲變量名。固然,不能以數字開頭,可是數字能夠出如今變量名中間,下劃線也是能夠的。

### 計算

set width to 8
set height to 9
set area to width * height

上面的腳本你們應該看一下就懂。

屬於叫作運算符,經常使用的運算符有:+、-、*、/、^、 對的,加減乘除乘方

處理文本

文本也就是咱們常說的字符串

彈窗

clipboard.png

因爲結果區只能顯示最後一行語句的執行結果。因此這裏咱們使用對話框(display dialog)的形式來演示。

注意:AppleScript是一個由有限詞彙構成的一個腳本語言,閱讀你寫的腳本的時候,
判別哪些是指令,哪些不是。對於Mac微機來講是一件很是複雜的一件事。
因此AppleScript須要依賴一些線索幫助其 讀懂腳本中語句行的各個元素的含義。這就是爲何要把字符串放到雙引號裏面。

對於上述,測試以下腳本 :

set stringToBeDispalyed to "hi there"
display dialog "stringToBeDisplayed"
display dialog stringToBeDispalyed

字符串操做

拼接

set nameOfActress to "Neal is "
set actressRating to " very pretty"
set resultingString to nameOfActress & actressRating
display dialog resultingString

& 是用來拼接字符串。

查看字符串長度

set theLength to the length of "Neal"

the length of 用來獲取字符串的長度。這裏順便說一句,字符串中特殊字符是須要轉義的。

類型轉換

經過上面的學習,你應該知道數字和字符串不是一個數據類型,因此將一種數據類型轉換到另外一種數據類型,叫作數據類型轉換。AppleScript數據類型轉換以下:

set strToNumber to "16" as number
set numToStr to 12 as string

list

弱類型語言中的數組

set exampleList to {1,2,3,"hahah",9}

上面的exampleList便是一個列表。

操做

向列表的開頭和結尾處添加

代碼以下:

set addToBegin to {"winter"}
set addToEnd to {"summer", "autumn"}
set currentList to {"spring"}
set modifiedList to addToBegin & currentList & addToEnd

對的,這裏咱們依舊使用 & 來作拼接。

取值

你可使用元素的序號來取代元素,最左邊的index是1,實際上是2,以此類推。可使用這種方式來從列表中取值,也能夠修改類表中值。

set testList to {"Neal", "haha"}
set item 2 of testList to "yang"
get testList

對於第二句,一樣的效果還有:

set the second item of testList to "yang"
set the 2nd item of testList to "yang"

簡直和英語一毛同樣有麼有。可是注意上面這種一字母拼寫的序 數詞最多隻能使用到「tenth」,
以後,就要使用「item 11」的形式。或者寫成 「11th item」的形式。
除了使用序數詞,還可使用「last item」指代列表中最後項目。

因此,當你只操做列表中最後一個值的時候,你沒必要知道列表具體有多少項目。
AppleScript容許你以相反的方向來指代元素,也就是能夠從右向左數。這須要你使用負數,-1 指代最後一個元素,
-2指代倒數第2個元素。例[11]能夠得到和例[10]同樣的結果

set myList to {"neal", "haha"}
set valueOfLastItem to item -1 of myList

下面展現下,如何一次去多個值。

set myList to {"a", "b", "c", "d", "e", "f"}
set shortList to items 2 through 5 of myList

是的,上面就是運行結果爲: {"b", "c", "d", "e"}

reverse

set reversedList to reverse of {2, 3, 4, 6, 7}

{7, 6, 4, 3, 2}

獲取數組長度

set theListLength to the length of {"ds", 1, 2, 3}
set theListLength to the count of {"ds", 1, 2, 3}

the length of 和 the count of 效果是同樣的。就是獲取列表的長度

隨機取值

set x to some item of {1, 2, 3, 4, 5, 7, 7, 6, 5}

隨機返回列表中的任一元素

類型轉換

set myList to {"a"}
set myString to "b"
--set result to myList & (myString as list)
set result to myList & myString

感受能夠對於js學習,弱類型語言均可以自動類型轉換的。

除了經過類型轉換將一個字符串變成一個列表,你還能夠建立一個列表,列表的元素是組成字 符串的每個字母。

set itemized to every character of "Nealyang"

{"N", "e", "a", "l", "y", "a", "n", "g"}

相比於單詞,咱們還能夠把一個句子按照單詞分開。這裏咱們可使用蘋果腳本的去限器( AppleScript’s text item delimiters)實現

首先定義一個字符做爲分割文本的標記,以這個標記 分割出來的元素將被包含在列表裏。

優秀的腳本編寫要求若是蘋果腳本文本去限器的值被更改了,一旦完 成任務還要將它改回原來的值

set myString to "neal's personal website is www.nealyang.cn"
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set myList to every text item of myString
set AppleScript's text item delimiters to oldDelimiters
get myList

{"neal's", "personal", "website", "is", "www.nealyang.cn"}

注意這裏設置完 AppleScript's text item delimiters 後,分割用 every text item of 不是every character of

空格 " "咱們是能夠自定義的。相似js中split

record

理解爲js中的對象吧

set stringToBeDisplayed to "Neal is pretty boy"
set tempVar to display dialog stringToBeDisplayed buttons {"So,so", "Don't know", "yes"}
set theButtonPressed to button returned of tempVar
display dialog "You pressed the following button " & theButtonPressed

上面的 button returned of 就是取值的語句。
由於dialog按鈕按下後會返回以下格式:{"button returned":"xxxx"}

簡單的操做以下:

set test to {neal:"yang"}
    set lala to neal of test

註釋

註釋很簡單

-- 註釋一行

# 註釋一行

(xxx) 註釋多行

條件語句

咱們把「if...then」指令叫作「條件語句」(conditional statement)。請注意,條件語句 還須要一個「end if」收尾。

比較簡單,直接上代碼吧:

set ageEntered to 73
set myAge to 24
if ageEntered is myAge then
    display dialog "You are as old as I am "
    beep
end if
say "this sentence is spoken anyway"

固然,咱們也能夠在 if ... then ... 和 end if 之間加上 else 關鍵字。
若是「if...then」指令和要執行的語句寫在同一行,那麼就再也不須要「end if」

錯誤捕獲

意外的終止是咱們所不但願的。好比,你的腳本須要打開一個文件夾處理其中的文件, 可是這個文件夾已經被刪除了,你會但願腳本容許用戶選擇其它合適的文件夾,而不是意外退出。

你能夠把這些可能引發運行錯
誤的語句放入「try...end try」模塊中

try
    set x to 1 / 0
on error the error_message number the error_number
    display dialog "Error: " & the error_number & "." & the error_message buttons {"OK"}
end try

Error: -2701.1.0 不能被零除

若是你在「on error」指令後面放上一個變量名,那麼錯誤描述信息將被賦給這個變量。若是你 在變量名前面加上「number」字樣,那麼錯誤代碼將被賦給變量。

路徑、文件夾和應用程序

先是硬盤,硬盤下面包含文件夾、應用程序和文件(上圖中沒有顯示出文件和應用程序)。所 有這些元素按照必定的層次組織起來。
這樣咱們就能夠經過路徑(path)這個概念來肯定一個文件 的位置

運行

choose folder

alias "Macintosh HD:Users:Nealyang:Documents:code:code-work:huilianyi:hooks:"

你能夠發現,路徑通常的都符合這樣的格式:
硬盤:文件夾:子文件夾:子文件夾:

tell application "Finder"
    open folder "Macintosh HD:Users:Nealyang:Documents:code:code-work:huilianyi:hooks:"
end tell

注意上面的alias。假如我在桌面上爲「Documents」文件夾中的文件「report.cwk」建立了一個替身。若是從此我 移動「report.cwk」到其它位置,或者把它重命名爲「funny_story.cwk」,雙擊替身依然可以打開這 個文件。這是由於替身並不以「Macintosh HD:users:Documents:report.cwk」的方式記錄文件 「report.cwk」
的存儲位置(和名字),而是記錄這個文件的ID。

爲了不由於文件被移動或更名形成的腳本運行中斷,咱們應當讓腳本記錄文件的ID而不是 「符號連接」的路徑

set thePath to alias "Macintosh HD:Users:Nealyang:Documents:code:code-work:huilianyi:hooks:README.md"

## 重複(循環)

關鍵字 repeat ... end repeat

列出所選文件夾中全部的文件夾名稱

set folderSelected to choose folder "Select a folder"
 tell application "Finder"
    set listOfFolders to every folder of folderSelected
 end tell
 
 set theList to {}
 repeat with aFolder in listOfFolders
    set temp to the name of aFolder
    set theList to theList & temp
 end repeat

## 函數

這個比較簡單,容易理解。

on test(lala)
    display dialog lala
end tst


test("haha")

上面就是定義了一個test函數,而後下面是調用

下面一個return的例子

on largest(a, b)
    if a > b then
        return a
    end if
    return b
end largest

set theLargetst to largest(4, 6)

結束語

如上就是AppleScript大概的基本知識,若是對他感興趣,就趕快動手來寫一寫有意思的Mac腳本吧~

關於如何利用腳本調用操做系統的程序,能夠參考Mac官網的這篇文章

相關文章
相關標籤/搜索