AppleScript 雖然是一種腳本語言,但在我看來是最接近天然語言和最不具有計算機特質的編程語言了。即便沒有計算機基礎,在閱讀基礎文檔和幾個樣例腳本以後,立刻就能動手寫出實用的腳本工具。ios
我感受不少使用 Mac 系統的同窗可能都沒意識到,與本身天天做伴的 Mac 系統上還有這麼一個強大高效的腳本語言,能夠從各個方面提高本身的工做效率。接下來的文章和你們分享我使用 AppleScript 的兩個場景。shell
在開始以前,先簡單歸納下 AppleScript 的應用環境。咱們的 Mac 系統從應用的角度來看,其實就是一堆 App 的集合,系統自帶的 App(Mail,Safari,Terminal 等)和安裝的第三方 App(Firefox,Chrome,Outlook,iTerm 等),這些主流的 App 其實都向系統暴露了一些實用接口,用來將一些高頻操做自動化。誰來調用這些接口呢?AppleScript。AppleScript 能夠將這些 App 和 App 內部的數據,都看成對象來訪問,甚至能夠將不一樣 App 串聯,自動化以後造成一個 workflow。編程
如何編寫 AppleScript 呢?Mac 自帶腳本編輯和運行工具,經過 Spotlight Search 搜索 Script Editor 便可。運行 Script Editor 以後,經過菜單 File -> Open Dictionary 便可打開以下圖所示一個文檔,裏面列出來全部支持 AppleScript 的 App,以及各個 App 所支持的接口調用。數組
applescript00.pngxcode
提高工做效率,避免重複勞動瀏覽器
我最近在研究如何下降 App 的 Crash 率,天天都要實時監控是否有新的 crash 發生。全部可能嚴重的 crash 警報都經過郵件發送到我郵箱,一旦收到警報我須要將郵件中的 crash id 複製出來,去另外一個網頁工具裏查詢。天天早上看着一大堆警報,若是要將全部的 crash id 手動複製出來,一個個貼入網頁裏查詢,操做很繁瑣。AppleScript 能夠輕鬆將這個流程自動化,一鍵搞定。步驟以下:app
郵件分類編程語言
郵件都是保存在 Microsoft Outlook 中,我首先設置一個 rule,將全部郵件標題包含 Trending Crash:xxx 字樣的郵件都存入一個子文件夾:iOS-Crash。工具
遍歷郵件url
再經過 AppleScript 遍歷 iOS-Crash 目錄下全部文件:
1
2
3
4
5
|
tell application
"Microsoft Outlook"
set
theMessages to messages of folder
"iOS-Crash"
of
default
account
repeat
with
theMessage
in
theMessages
end repeat
end tell
|
上面這段腳本讀起來是否是一目瞭然?就像是在和 siri 聊天同樣,告訴 siri 遍歷某個目錄下的所有郵件。
提取 Crash ID
AppleScript 的另外一個強大之處是能夠和系統自帶的各種經常使用命令行工具(好比 grep,sed,awk 等)交互,這意味着對文本和文件的操做能夠遊刃有餘。接下來我要經過 sed 工具來提取郵件中的 Crash ID:
1
2
3
4
5
6
7
8
9
10
11
|
tell application
"Microsoft Outlook"
set
theMessages to messages of folder
"iOS-Crash"
of
default
account
set
crash_id_set to {}
repeat
with
theMessage
in
theMessages
set
msgContent to plain text content of theMessage
tell me to
set
crash_id to
do
shell script
"echo "
& quoted form of msgContent &
" | sed -E -n 's_.* crash_id:(.+)}_\\1_1p'"
if
crash_id
is
not
in
crash_id_set and the length of crash_id >
0
then
set
crash_id_set to crash_id_set & crash_id
end
if
end repeat
end tell
|
關鍵代碼是這一行:
1
|
tell me to
set
crash_id to
do
shell script
"echo "
& quoted form of msgContent &
" | sed -E -n 's_.* crash_id:(.+)}_\\1_1p'"
|
AppleScript 用 tell xxx 的方式來切換腳步運行環境,好比
1
|
tell application
"Microsoft Outlook"
|
是切換到 Outlook 的進程中。
1
|
tell me to
|
是切換到當前用戶的運行環境,由於咱們要執行命令行腳步,須要更高級權限,因此要切換到當前用戶進程。
接下來經過 echo 將郵件的內容傳遞給 sed,並提取出 crash-id,將值傳回 AppleScript 中的便利 crash_id,放入數組中。
拼裝 url 並在瀏覽器中打開
這是最後一步,將上面提取結果在瀏覽器中打開:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
tell application
"Microsoft Outlook"
set
theMessages to messages of folder
"iOS-Crash"
of
default
account
set
crash_id_set to {}
set
param to
""
repeat
with
theMessage
in
theMessages
set
msgContent to plain text content of theMessage
tell me to
set
crash_id to
do
shell script
"echo "
& quoted form of msgContent &
" | sed -E -n 's_.* crash_id:(.+)}_\\1_1p'"
if
crash_id
is
not
in
crash_id_set and the length of crash_id >
0
then
set
crash_id_set to crash_id_set & crash_id
end
if
end repeat
repeat
with
crash_id
in
crash_id_set
set
param to param &
"%22"
& crash_id
end repeat
tell me to
do
shell script
"cat ~/Documents/AppleScripts/ios_crash_url | sed -n -E s_crash_ids_"
& quoted form of param &
"_p | xargs open "
end tell
|
url 原始信息保存在文件 ios_crash_url 中,使用 sed 作簡單替換以後,將 url 傳遞個 open 命令便可。
最後添加個命令 alias,就能夠作到一鍵完成了。
1
|
alias ioscrash=
'osascript /Users/fenggao/Documents/AppleScripts/outlook_ios_crash.scpt'
|
代碼重構
我還使用過 AppleScript 來重構 Objective C 代碼,原理很簡單,將 Xcode 中選中的代碼以 text 的形式傳遞給 AppleScript,再經過 AppleScript 傳遞給命令行來操做。或者將 Xcode 當前打開的類文件 path 經過 AppleScript 傳遞給命令行工具,接下來就是基礎的文件操做了,各種工具任由你選,好比咱們可使用本地編譯好的 clang 來分析類文件,來進行鍼對 Objective C 語法特徵的文本修改。當咱們有大量的代碼文件須要修改,並且修改的規則遵循某個相同的 pattern 時,使用腳本能起到事半功倍的效果。
經過 osascript 命令執行 AppleScript 是方式之一,另外一種方式是經過 Service。每一個 App 在菜單裏都有 Services 一項。咱們能夠經過 Automator 來添加每一個 App 都能使用的 Service。
咱們能夠把一個 Service 想象成一個 workflow,而一個 workflow 能夠包含若干個 action,執行 AppleScript 就能夠是其中的一個 action。
首先經過 Spotlight Search 啓動 Automator,啓動以後選擇建立 Service。以後能夠看到因此支持的 action,選擇 Run AppleScript 並拖動到右側的 workflow 區域,便可執行某個 AppleScript 了。固然也能夠拖動 Run Shell Script 到 workflow 區域,各個 action 之間能夠經過 stdin 傳遞數據。以下圖所示:
applescript01.png
全部建立保存以後的 service 都會自動保存到 ~/Library/Services/ 目錄下,一旦保存咱們就能夠在應用的 services 目錄下看到咱們的目標,好比我保存 xcode-text.workflow 以後。我在 Xcode 中選擇某些代碼,右鍵就能夠看到我所建立的 service 了,如圖:
applescript02.png
關鍵字:automator->service->action->applescript
總結
AppleScript 的應用場景很普遍,且很容易上手。一些 Mac App 的核心功能甚至都是利用 AppleScript 來編寫的。好比 Mac 上的剪貼板工具,就是經過 AppleScript 來操做其餘應用的當前編輯文本,來實現歷史查找和插入功能。工具的強大與否在於使用之人如何用之,工具都是越用越稱手。