Skip to content

[AppleScript] 使用 Dialog 製作彈出訊息元件

在使用 AppleScript 撰寫腳本時,若有需要提醒自己或使用者的訊息,可以透過 Dialog 來製作一個簡單的視窗。除此之外,Dialog 也可以設定不同的按鈕,來讓自己或使用者依照需求選擇。


Dialog 的使用方法

顯示訊息

Dialog 最單純的形式,就是『訊息』加上預設的『兩個按鈕』。

set Message to "You have a new message!"
display dialog Message


Output:


在訊息前加上圖示

如果嫌這樣的訊息視窗太過單調,也可以加上圖示(Icon)來讓視窗更豐富些。在 AppleScript 中預設可以調用的圖示有三種,分別為 note、stop、caution

note 圖示

set Message to "You have a new message!"
display dialog Message with icon note


Output:


stop 圖示

set Message to "You have a new message!"
display dialog Message with icon stop


Output:


caution 圖示

set Message to "You have a new message!"
display dialog Message with icon caution


Output:


客製化按鈕、取得按鈕回傳資訊

而有時候我們會需要依照使用者選擇了不同的按鈕而做出不同的腳本功能。這時候我們可以使用以下程式碼取得使用者按下的按鈕資訊。

set Message to "You have a new message!"
set ButtonResult to the button returned of (display dialog Message buttons {"Cancel", "OK"} default button "OK" cancel button "Cancel")

# Button Result
display dialog ButtonResult


Output:


References


Read More

Leave a Reply