關於AppleScript腳本語言的一些使用總結

1. 暗黑模式切換

tell application "System Events"
	tell appearance preferences
		set dark mode to not dark mode
	end tell
end tell

2. 發送郵件

--設置參數
set recipientName to "xxx" --收件人
set recipientAddress to "[email protected]" --收件地址
set mailSubject to "使用AppleScript腳本自動化發郵件" --郵件主題
set mailContent to "這是一封來自AppleScript發出的測試郵件,請勿回覆!!!" --郵件內容

--執行發送郵件操作
tell application "Mail"
	
	--創建信息
	set theMessage to make new outgoing message with properties {subject:mailSubject, content:mailContent, visible:true}
	
	--發送信息
	tell theMessage
		make new to recipient with properties {name:recipientName, address:recipientAddress}

		send
		
	end tell
end tell

3. 彈窗相關

一個按鈕默認樣式
一個按鈕
一個按鈕加空格格式化樣式

兩個按鈕樣式

三個按鈕樣式

沒有icon樣式

反正這個dialog感覺就很安卓

a. 普通彈窗

display dialog "這是內容" with title "這是標題" --默認帶上取消和確認按鈕
--設置一個OK按鈕以及默認選中
display dialog "這是內容" with title "這是標題" buttons "OK" default button "OK"
--效果同上
display dialog "這是內容" with title "這是標題" buttons "OK" default button 1
-- 自定義多個按鈕 (最多允許使用三個按鈕。)
display dialog "這是內容" with title "這是標題" buttons {"OK","Cancel","HAHA"} default button "OK"

或者alert 這個就比較iOS

-- 與 dialog 類似佈局上有所不同,按鈕是居中縱向排列
display alert "hahhaha" buttons {"OK", "NO", "YES"} default button 2

b. 帶圖標的彈窗

--可以指定對話框的圖標,icon 圖標可以指定 note (普通) /stop (危險) /caution (警告) 三種類型 或者指向文件路徑
display dialog "這是內容" with title "這是標題" buttons {"No", "Yes"} default button "Yes" with icon caution

-- 自定義圖標 注意圖片格式應該爲.icns格式的 可以去應用xx.app/contens/resources下面去找
set fileName to choose file "Select a Folder"
display dialog "這是內容" with title "這是標題" buttons {"No", "Yes"} default button "Yes" with icon file fileName

-- 指定路徑 桌面路徑 + 文件名
display dialog "這是內容" with title "這是標題" buttons {"No", "Yes"} default button "Yes" with icon file ((path to desktop as text) & "AppIcon.icns")

-- 或者這樣
display dialog "這是內容" with title "這是標題" buttons {"No", "Yes"} default button "Yes" with icon alias ((path to desktop as text) & "AppIcon.icns")
-- 轉化一下
-- set fileName to ((path to desktop as text) & "AppIcon.icns")
set fileName to "Macintosh HD:Users:wangguibin:Desktop:AppIcon.icns"
display dialog "這是內容" with title "這是標題" buttons {"No", "Yes"} default button "Yes" with icon file fileName

-- 直接使用App Store的圖標
set fileName to "Macintosh HD:System:Applications:App Store.app:Contents:Resources:AppIcon.icns"
display dialog "這是內容" with title "這是標題" buttons {"No", "Yes"} default button "Yes" with icon file fileName

c. 彈窗輸入框表單

display dialog "表單" default answer "輸入框內容" buttons {"按鈕1", "按鈕2", "按鈕3"} default button 1 with icon caution
copy the result as list to {text_returned, button_pressed} --返回一個列表{文本,按鈕}

d. 選擇列表彈窗

-- 默認單選 默認不選中的話直接設置 `default items {}` 即可
choose from list {"Shell", "Ruby", "Python", "Applescript", "Javascript", "Perl", "Dart"} with title "日期選擇" with prompt "選擇一門腳本語言" OK button name "學習" cancel button name "放棄" default items {"Python"}

-- 多選
choose from list {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} with title "日期選擇" with prompt "選擇一天或者多天" OK button name "確認選擇" cancel button name "不選" default items {"Monday"} with multiple selections allowed

e. 選擇文件和目錄

選擇文件

-- 選擇文件 獲取文件名 沒有的話不會創建 只是返回一個路徑 
choose file name with prompt "獲取文件名"

選擇目錄

-- 注:其中prompt和default location參數同Choose File Name;另外invisibles指定顯示隱藏 文件,multiple selections allowed可以多選,showing package contents顯示包內容,省略時 則不顯示隱藏文件/不可多選/不顯示包內容
choose folder with prompt "選擇目錄" default location file "Macintosh HD:Users:mac:Desktop" with invisibles, multiple selections allowed and showing package contents

4. 通知

-- 聲音文件都在/System/Library/Sounds 
-- 其中Funk , Glass, Ping 這幾種好聽一些 
display notification "通知內容通知內容通知內容通知內容" with title "通知主標題" subtitle "副標題" sound name "Funk"

5. Shell 調用 AppleScript

適合簡短的腳本語句

#注意單引號shell無法傳參 如需傳參則需要使用雙引號\轉義
osascript -e 'display notification "通知內容通知內容通知內容通知內容" with title "通知主標題" subtitle "副標題" sound name "Funk"'

適合多行腳本,增加可讀性

#簡單粗暴 直接使用重定向包含applescript語句即可
osascript <<EOF
set fileName to "Macintosh HD:System:Applications:App Store.app:Contents:Resources:AppIcon.icns"
display dialog "這是內容" with title "這是標題" buttons {"No", "Yes"} default button "Yes" with icon file fileName
set btn to (button returned of result)
get btn
EOF
# 返回值 "NO"或者"OK"

6. AppleScript 調用 Shell

do shell script + shell腳本語句即可

set shellStr to do shell script "cd ~/Desktop;cat shell_var.sh"
display alert shellStr buttons {"OK"}

總結

AppleScript配合Shell 以及Alfred 感覺能玩出很多花樣來,一些工具確實能提升不少效率和體驗。
我平時玩的一些工具存放在這 https://github.com/WangGuibin/WGBToolsConfigRepository

參考博文文章

applescript-快速入門
我的新玩具-AppleScript(四)
applescript快速入門教程
AppleScript 腳本讓 Mac 唱生日快樂歌

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章