簡體   English   中英

如何制作 Mac 終端彈出/警報? 蘋果文字?

[英]How do I make a Mac Terminal pop-up/alert? Applescript?

我希望能夠讓我的程序顯示警報、通知,無論顯示我的自定義文本。 這是怎么做的? 另外,是否可以使用多個設置變量的按鈕來制作一個?

類似於批處理: echo msgbox""<a.vbs&a.vbs

使用osascript 例如:

osascript -e 'tell app "Finder" to display dialog "Hello World"' 

用您想要的任何應用程序替換“Finder”。 請注意,如果該應用程序是后台的,該對話框也會出現在后台。 要始終顯示在前台,請使用“系統事件”作為應用程序:

osascript -e 'tell app "System Events" to display dialog "Hello World"'

閱讀有關Mac OS X 提示的更多信息

使用該命令從終端觸發通知中心通知。

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

如果您使用任何具有通知中心的 Mac OS X 版本,您可以使用終端通知程序gem。 首先安裝它(您可能需要sudo ):

gem install terminal-notifier

然后簡單地:

terminal-notifier -message "Hello, this is my message" -title "Message Title"

另請參閱此 OS X Daily post

向通知添加子標題標題聲音

使用AppleScript

display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"

使用終端/bashosascript

osascript -e 'display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"'

可以顯示警報而不是通知

不采用副標題,也不采用強硬的聲音。

使用AppleScript

display alert "Alert title" message "Your message text line here."

使用終端/bashosascript

osascript -e 'display alert "Alert title" message "Your message text line here."'

bash 中添加一行用於在警報行之后播放聲音

afplay /System/Library/Sounds/Hero.aiff

AppleScript 中添加同一行,讓shell 腳本完成工作:

do shell script ("afplay /System/Library/Sounds/Hero.aiff")

可從此處選擇的 macOS 內置聲音列表。

轉述自一篇關於終端和 Applescript 通知的方便文章。

如果答案為空,這會將焦點恢復到前一個應用程序並退出腳本。

a=$(osascript -e 'try
tell app "SystemUIServer"
set answer to text returned of (display dialog "" default answer "")
end
end
activate app (path to frontmost application as text)
answer' | tr '\r' ' ')
[[ -z "$a" ]] && exit

如果您告訴 System Events 顯示對話框,如果它之前沒有運行,將會有一個小的延遲。

有關顯示對話框的文檔,請在 AppleScript 編輯器中打開 Standard Additions 詞典或參閱AppleScript 語言指南

還有我的 15 美分。 mac 終端等的單襯線只需將 MIN= 設置為任何內容和一條消息

MIN=15 && for i in $(seq $(($MIN*60)) -1 1); do echo "$i, "; sleep 1; done; echo -e "\n\nMac Finder should show a popup" afplay /System/Library/Sounds/Funk.aiff; osascript -e 'tell app "Finder" to display dialog "Look away. Rest your eyes"'

組合更多命令的靈感獎勵示例; 這也會讓 mac 在收到消息后進入待機睡眠狀態:) 然后需要 sudo 登錄,乘以兩個小時的 60*2 也是如此

sudo su
clear; echo "\n\nPreparing for a sleep when timers done \n"; MIN=60*2 && for i in $(seq $(($MIN*60)) -1 1); do printf "\r%02d:%02d:%02d" $((i/3600)) $(( (i/60)%60)) $((i%60)); sleep 1; done; echo "\n\n Time to sleep  zzZZ";  afplay /System/Library/Sounds/Funk.aiff; osascript -e 'tell app "Finder" to display dialog "Time to sleep zzZZ"'; shutdown -h +1 -s

簡單通知

osascript -e 'display notification "hello world!"'

帶標題的通知

osascript -e 'display notification "hello world!" with title "This is the title"'

通知並發出聲音

osascript -e 'display notification "hello world!" with title "Greeting" sound name "Submarine"'

帶變量的通知

osascript -e 'display notification "'"$TR_TORRENT_NAME has finished downloading!"'" with title " ✔ Transmission-daemon"'

學分: https : //code-maven.com/display-notification-from-the-mac-command-line

我制作了一個腳本來解決這個問題 為此,您不需要任何額外的軟件。 安裝:
brew install akashaggarwal7/tools/tsay
用法:
sleep 5; tsay

隨意貢獻!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM