I want to be able to have my program display an alert, notice, whatever that displays my custom text. How is this done? Also, is it possible to make one with several buttons that sets a variable?
Similar to batch's: echo msgbox""<a.vbs&a.vbs
欢迎各位兄弟 发布技术文章
这里的技术是共享的
为什么 osascript ,,,,,, su 到另一个帐户下就会有错误提示,
[NSXPCSharedListener endpointForReply:withListenerName:]: an error occurred while attempting to obtain endpoint for listener 'ClientCallsAuxiliary': Connection interrupted
但会继续执行,难道是mac版本太旧了,我这里试的是mac10的版本( OS X Mojave )
来自 https://osxdaily.com/2016/09/06/trigger-alert-dialog-mac-via-command-line/
I want to be able to have my program display an alert, notice, whatever that displays my custom text. How is this done? Also, is it possible to make one with several buttons that sets a variable?
Similar to batch's: echo msgbox""<a.vbs&a.vbs
Use osascript
. For example:
osascript -e 'tell app "Finder" to display dialog "Hello World"'
Replacing “Finder” with whatever app you desire. Note if that app is backgrounded, the dialog will appear in the background too. To always show in the foreground, use “System Events” as the app:
osascript -e 'tell app "System Events" to display dialog "Hello World"'
Read more on Mac OS X Hints.
Use this command to trigger the notification center notification from the terminal.
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
If you're using any Mac OS X version which has Notification Center, you can use the terminal-notifier gem. First install it (you may need sudo
):
gem install terminal-notifier
and then simply:
terminal-notifier -message "Hello, this is my message" -title "Message Title"
See also this OS X Daily post.
With AppleScript:
display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"
With terminal/bash and osascript
:
osascript -e 'display notification "Notification text" with title "Notification Title" subtitle "Notification sub-title" sound name "Submarine"'
Does not take the sub-heading nor the sound tough.
With AppleScript:
display alert "Alert title" message "Your message text line here."
With terminal/bash and osascript
:
osascript -e 'display alert "Alert title" message "Your message text line here."'
Add a line in bash for playing the sound after the alert line:
afplay /System/Library/Sounds/Hero.aiff
Add same line in AppleScript, letting shell script do the work:
do shell script ("afplay /System/Library/Sounds/Hero.aiff")
List of macOS built in sounds to choose from here.
Paraphrased from a handy article on terminal and applescript notifications.
This would restore focus to the previous application and exit the script if the answer was empty.
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
If you told System Events to display the dialog, there would be a small delay if it wasn't running before.
For documentation about display dialog, open the dictionary of Standard Additions in AppleScript Editor or see the AppleScript Language Guide.
And my 15 cent. A one liner for the mac terminal etc just set the MIN= to whatever and a message
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"'
A bonus example for inspiration to combine more commands; this will put a mac put to standby sleep upon the message too :) the sudo login is needed then, a multiplication as the 60*2 for two hours goes aswell
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"'
credits: https://code-maven.com/display-notification-from-the-mac-command-line
I made a script to solve this which is here. Installation:brew install akashaggarwal7/tools/tsay
Usage:sleep 5; tsay
Feel free to contribute!