AppleScript 快速入门
AppleScript 快速入門
- AppleScript 快速入門
- 一、讓其他程序執(zhí)行任務(wù)
- 二、數(shù)據(jù)類型
- 三、條件語(yǔ)句
- 四、循環(huán)
- 五、函數(shù)
- 六、用戶交互對(duì)話框
- 七、使用詞典
- 八、操作其他程序的界面
- 九、運(yùn)行參數(shù)
AppleScript 顧名思義是蘋(píng)果開(kāi)發(fā)的一套腳本語(yǔ)言,利用 AppleScript 在 macOS 系統(tǒng)上可以對(duì)其他程序進(jìn)行操作,點(diǎn)擊按鈕、發(fā)送消息、模擬自動(dòng)化執(zhí)行功能,比如可以打開(kāi)瀏覽器,清空回收站等等一些操作,是一個(gè)非常有意思的腳本。說(shuō)好了要快速入門,下面我們開(kāi)始快速學(xué)習(xí)了解它吧。
一、讓其他程序執(zhí)行任務(wù)
在 macOS 上有一個(gè)應(yīng)用叫腳本編輯器,通過(guò) Launchpad 可以搜索到,打開(kāi)腳本編輯器之后,可以看到支持編寫(xiě)和解析 AppleScript 和 JavaScript 兩種腳本,如下圖所示:
AppleScript 的語(yǔ)法和平時(shí)的英語(yǔ)語(yǔ)法很類似,你想讓哪個(gè)程序執(zhí)行操作,就 tell 它,比如你想讓 Finder 清空回收站那就這樣寫(xiě):
tell application "Finder"empty the trash end tell在腳本編輯器上點(diǎn)擊運(yùn)行按鈕就可以看到回收站的內(nèi)容被清空了,或者按快捷鍵 Command + R 也能運(yùn)行,運(yùn)行之前記得回收站得有東西,不然可能會(huì)執(zhí)行失敗。
如果你想讓系統(tǒng)說(shuō)話,可以這樣寫(xiě):
tell application "Finder"say "My name is exchen" end tell哈哈,記得把電腦的聲音打開(kāi),是不是聽(tīng)到說(shuō)話了?不僅支持英文和中文,其他國(guó)家語(yǔ)言,像德語(yǔ)、荷蘭語(yǔ)筆者試過(guò),同樣也可以。
如果你想讓瀏覽器打開(kāi) URL,可以這樣寫(xiě):
set myBlog to "http://www.exchen.net"# 告訴 Chrmoe 瀏覽器打開(kāi) URL tell application "Google Chrome"# 新建一個(gè) chrome 窗口set window1 to make new windowtell window1set currTab to active tab of window1set URL of currTab to myBlogend tell end tell看看 Chrmoe 瀏覽器是不是打開(kāi)了你指定的 URL 了?有意思吧?
上面的測(cè)試代碼都是在腳本編輯器里運(yùn)行的,如何脫離腳本編輯器,直接在系統(tǒng)上運(yùn)行呢?我們可以保存或?qū)С瞿_本,點(diǎn)擊文件菜 -> 存儲(chǔ),可以看到支持的格式有四種,如圖所示:
保存為腳本類型,然后通過(guò) osascript 來(lái)執(zhí)行腳本,如下:
/usr/bin/osascript test1.scpt如果保存為應(yīng)用程序類型,就是一個(gè) .app 的包,直接雙擊打開(kāi)就能運(yùn)行。
二、數(shù)據(jù)類型
AppleScript 的數(shù)據(jù)類型比較簡(jiǎn)單,一般常用的有 number、string、list、record,也就是數(shù)字類型、字符串類型、列表類型、字典類型。
數(shù)字類型的賦值和使用如下:
set num1 to 10 # 給 num1 賦值 set num2 to 20 # 給 num2 賦值 set num3 to num1 + num2 # num1 + num2 賦值給 num3 set num4 to num3 * 2 # num3 * 2 賦值給 num4字符串類型的賦值和使用如下:
set str1 to "exchen.net" set str2 to "hehe" set str3 to str1 & str2字符串與數(shù)字的轉(zhuǎn)換方法如下:
set str3Len to the length of str3 set numToStr to num1 as string set strToNum to "123" as number列表類型其實(shí)就是相當(dāng)于數(shù)組,定義和操作列表類型的方法如下:
set myLists to {1, 2, "str", 4, 5} # 定義列表數(shù)據(jù) set item 3 of myLists to "exchen" #操作第三列的數(shù)據(jù) get myLists # 獲取列表數(shù)據(jù)字典類型的定義和操作方法如下:
set myRecord to {name:"exchen", blog:"http://www.exchen.net", body:"hehe"} # 定義 Record 數(shù)據(jù) set value to the body of myRecord # 從 Record 中獲取 body 數(shù)據(jù)給 value get value三、條件語(yǔ)句
既然是腳本語(yǔ)言,當(dāng)然不能少了 if 和 else 語(yǔ)句,使用方法如下:
set num to 123 if num = 123 thendisplay dialog "等于 123"else if strToNum > 456 thendisplay dialog "大于 456"elsedisplay dialog "不等于 123 也不大于 456" end if通過(guò) contains 方法來(lái)進(jìn)行字符串的比較判斷:
set domainName to "www.exchen.net" if domainName contains "exchen" thendisplay dialog "包含 exchen" elsedisplay dialog "不包含 exchen" end if四、循環(huán)
循環(huán)的寫(xiě)法有好幾種,不過(guò)都是使用 repeat … end repeat,比如循環(huán) 100 次可以這樣寫(xiě):
set num to 10 repeat 100 timesset num to num + 1 end repeatget num類似于 for 循環(huán),就這樣寫(xiě):
set num to 5 repeat with counter from 0 to num by 1display dialog counter end repeat類似于 while 循環(huán),可以這樣寫(xiě):
set num to 0 repeat until num ≥ 10display dialog numset num to num + 3 end repeat五、函數(shù)
如果某些功能有重用性,應(yīng)該要寫(xiě)成函數(shù),AppleScript 也支持定義函數(shù),定義和使用方法如下:
on testFun()set num to 1 end testFuntestFun()函數(shù)當(dāng)然會(huì)有返回值,通過(guò) return 返回值:
on testFun()set num to 1return num end testFunset ret to testFun() get ret另外函數(shù)可能還會(huì)帶參數(shù),帶參數(shù)的方法使用如下:
on testFun(str)display dialog strend testFuntestFun("exchen")函數(shù)有可能會(huì)帶多個(gè)參數(shù),使用方法如下:
on testFun(str1, str2)display dialog str1display dialog str2end testFuntestFun("exchen", "hehe")六、用戶交互對(duì)話框
在前面我們使用過(guò) display dialog 彈出對(duì)話框,如果要指定標(biāo)題通過(guò) with title 關(guān)鍵字,代碼如下:
display dialog "這是內(nèi)容" with title "這是標(biāo)題"指定按鈕的內(nèi)容,可以通過(guò) buttons {“No”, “Yes”},按鈕個(gè)數(shù)最多三個(gè),代碼如下:
display dialog "這是內(nèi)容" with title "這是標(biāo)題" buttons {"No", "Yes"}也可以通過(guò) default button 設(shè)置默認(rèn)選擇的按鈕,代碼如下:
display dialog "這是內(nèi)容" with title "這是標(biāo)題" buttons {"No", "Yes"} default button "Yes"還可以指定對(duì)話框的圖標(biāo),icon 圖標(biāo)可以指定 note/stop/caution 類型,或者指向文件路徑,代碼如下:
display dialog "這是內(nèi)容" with title "這是標(biāo)題" buttons {"No", "Yes"} default button "Yes" with icon note對(duì)話框一般是用于和用戶進(jìn)行交互,通過(guò) button returned 可以獲取用戶點(diǎn)擊了哪個(gè)按鈕,然后進(jìn)行相應(yīng)用操作,代碼如下:
display dialog "這是內(nèi)容" with title "這是標(biāo)題" buttons {"No", "Yes"} default button "Yes" if button returned of result = "Yes" thenelse if button returned of result = "No" thenend if對(duì)話框中也可以帶輸入框,讓用戶進(jìn)行輸入內(nèi)容,代碼如下:
display dialog "請(qǐng)輸入內(nèi)容:" default answer ""帶輸入框的對(duì)話框的效果如下圖:
輸入內(nèi)容之后,通過(guò) text returned 來(lái)獲取輸入框的內(nèi)容:
display dialog "請(qǐng)輸入內(nèi)容:" default answer "" if text returned of result = "exchen" thenget "exchen.net" end if七、使用詞典
在第一節(jié)我們知道了如何在其他程序中執(zhí)行任務(wù),比如讓瀏覽器打開(kāi) URL、清空回收站,如果還想執(zhí)行其他額外更多的功能怎么辦?去哪兒查相應(yīng)的方法名稱?
可以通過(guò)詞典來(lái)找相應(yīng)的方法名稱,將應(yīng)用直接拖到 Dock 上的腳本編輯器圖標(biāo),然后就會(huì)顯示擴(kuò)展的詞典,在這里可以查看該應(yīng)用支持的相應(yīng)方法名稱說(shuō)明,比如 Chrome 的詞典如下圖所示:
有些應(yīng)用沒(méi)有功能擴(kuò)展的詞典,就會(huì)提示打開(kāi)詞典失敗,如下圖所示:
八、操作其他程序的界面
本小節(jié)我們來(lái)試一下操作其他程序來(lái)實(shí)現(xiàn)簡(jiǎn)單的自動(dòng)化,打開(kāi)計(jì)算器,使用 entire contents 顯示出 UI 信息,代碼如下:
tell application "System Events"tell process "Calculator"entire contentsend tell end tell返回 UI 信息如下:
{window 1 of application process "Calculator" of application "System Events", group 1 of window 1 of application process "Calculator" of application "System Events", static text "0" of group 1 of window 1 of application process "Calculator" of application "System Events", group 2 of window 1 of application process "Calculator" of application "System Events", button 1 of group 2 of window 1 of application process "Calculator" of application "System Events", button 2 of group 2 of window 1 of application process "Calculator" of application "System Events", button 3 of group 2 of window 1 of application process "Calculator" of application "System Events", button 4 of group 2 of window 1 of application process "Calculator" of application "System Events", button 5 of group 2 of window 1 of application process "Calculator" of application "System Events", button 6 of group 2 of window 1 of application process "Calculator" of application "System Events", button 7 of group 2 of window 1 of application process "Calculator" of application "System Events", button 8 of group 2 of window 1 of application process "Calculator" of application "System Events", button 9 of group 2 of window 1 of application process "Calculator" of application "System Events", button 10 of group 2 of window 1 of application process "Calculator" of application "System Events", button 11 of group 2 of window 1 of application process "Calculator" of application "System Events", button 12 of group 2 of window 1 of application process "Calculator" of application "System Events", ......column 2 of table 1 of menu item 1 of menu "幫助" of menu bar item "幫助" of menu bar 1 of application process "Calculator" of application "System Events", menu item "計(jì)算器幫助" of menu "幫助" of menu bar item "幫助" of menu bar 1 of application process "Calculator" of application "System Events"}比如我們關(guān)心的是按鈕 9,信息比較多,一時(shí)看不出我們所關(guān)心的按鈕,可以通過(guò) Xcode 自帶的工具 Accessibility Inspector 查看 UI 信息,打開(kāi) Xcode 菜單,在 Open Developer Tool 里可以找到它,打開(kāi)之后點(diǎn)擊捕獲按鈕,找到我們關(guān)心的按鈕,效果如下圖所示:
在 Accessibility Inspector 界面往下拉,可以看到按鈕 9 是在第二組的第四個(gè),所圖所示:
從返回的 UI 信息里可以找到按鈕信息:
button 4 of group 2 of window 1 of application process "Calculator"編寫(xiě)代碼實(shí)現(xiàn)點(diǎn)擊按鈕:
tell application "System Events"tell process "Calculator"entire contentsclick button 7 of group 2 of window 1end tell end tell如果想點(diǎn)擊菜單,在 UI 返回信息里你關(guān)心的菜單,編寫(xiě)代碼如下:
tell application "System Events"tell process "Calculator"click menu item "關(guān)于計(jì)算器" of menu "計(jì)算器" of menu bar item "計(jì)算器" of menu bar 1end tell end tell執(zhí)行之后,就相當(dāng)于點(diǎn)擊了 “關(guān)于計(jì)算器” 菜單,如下圖所示:
九、運(yùn)行參數(shù)
在第一節(jié),我們知道通過(guò) /usr/bin/osascript 能夠執(zhí)行腳本,如果腳本在啟動(dòng)的時(shí)候需要參數(shù)怎么辦?通過(guò) on run 定義好參數(shù),代碼如下:
on run {parameter1, parameter2}display dialog parameter1 end run然后在命令行執(zhí)行的時(shí)候,后面跟參數(shù)執(zhí)行就行了,命令如下:
/usr/bin/osascript test1.scpt "exchen.net" "parameter2"原文地址:http://www.exchen.net/applescript-%e5%bf%ab%e9%80%9f%e5%85%a5%e9%97%a8.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的AppleScript 快速入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 解决 LLVM 错误提示 may onl
- 下一篇: The document “Main.s