127

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

JShoe
  • 3,186
  • 9
  • 38
  • 61

8 Answers8

239

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.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Anne
  • 26,765
  • 9
  • 65
  • 71
  • Wait, you can abbreviate application as app? – JShoe Apr 07 '11 at 22:23
  • 1
    The AppleScript engine will automatically replace it. Simply paste the line between the quotes in AppleScript Editor, when you hit Run, it replaces app automatically with application before execution. – Anne Apr 07 '11 at 22:27
  • Another typing saver: you don't need "end if", "end repeat", etc., just "end" is fine and AppleScript will insert the second word. – Nicholas Riley Apr 08 '11 at 00:04
  • 11
    If you don't want the "Cancel" button but just want an "OK" button replace {dialog} with {alert}. – Bart B Jul 26 '13 at 02:07
  • Hi Anne, it is possible to open dialog or any other way to send message to other mac connected with LAN using terminal? – iBhavik Sep 09 '13 at 06:27
  • 1
    13havik, well I know it's old topic, but yes, you can. :) However you need to be logged in. e.g by terminal ssh. and then invoke osascript -e 'display dialog "Hello!"' – Alex Reds Jun 10 '14 at 23:53
  • How can I display a dialogue or alert without the buttons? I want to flash a series of text messages and don't want any buttons present at all and would like to control how long the messages are displayed. – adamlogan Oct 14 '15 at 09:57
69

Use this command to trigger the notification center notification from the terminal.

osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
Pradeep Vairamani
  • 4,004
  • 3
  • 36
  • 59
61

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Enrico Carlesso
  • 6,818
  • 4
  • 34
  • 42
  • 5
    This is simply so much better than the old osascript stuff. – Jonny Nov 13 '12 at 11:06
  • This appears to not work in 10.7.5 (Lion), apparently no Notification Center in it. – Norman H Dec 27 '12 at 18:32
  • 6
    `brew install terminal-notifier` also works if you prefer to brew. – gklka May 02 '15 at 17:40
  • 6
    **PSA:** On Mavericks and later this isn't needed, just use osascript's [display notification](https://apple.stackexchange.com/questions/57412/how-can-i-trigger-a-notification-center-notification-from-an-applescript-or-shel), which is mentioned below in Pradeep's answer. – alexchandel Jun 23 '16 at 22:57
13

Adding subtitle, title and a sound to the notification:

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"'

An alert can be displayed instead of a notification

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.

andiOak
  • 356
  • 3
  • 9
7

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.

Lri
  • 26,768
  • 8
  • 84
  • 82
5

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
K. Kilian Lindberg
  • 2,918
  • 23
  • 30
4

Simple Notification

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

Notification with title

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

Notify and make sound

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

Notification with variables

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

tejas n
  • 638
  • 1
  • 8
  • 14
-2

I made a script to solve this which is here. Installation:
brew install akashaggarwal7/tools/tsay
Usage:
sleep 5; tsay

Feel free to contribute!

Akash Agarwal
  • 2,326
  • 1
  • 27
  • 57
  • 17
    "don't need any extra software... just install this" that's extra software. – PRS Apr 28 '17 at 00:39
  • @PRS its a script that runs commands available to the macOS already, not a software. – Akash Agarwal Apr 28 '17 at 13:42
  • 4
    then why do I have to install brew and your script? see what I mean? ;) – PRS May 09 '17 at 06:17
  • however I'll give you credit for introducing me to the say command... I learned something new today! lol – PRS May 09 '17 at 06:19
  • @PRS Glad you came across say command, its quite useful. About your other comment I think if you're a developer brew is pretty common for you to use. And about installing the script, well feel free to open the source of the script and copy/paste it locally in a file and chmod +x it and run it ;) – Akash Agarwal May 14 '17 at 07:32
  • he was just trying to have an easy way for others to run his simple script - not everyone on the internet is a programmer :D – Brad Parks Mar 11 '23 at 03:07