Launching a notification via osascript

I found a way to launch a notification from osascript. Initially nothing seemed to work. Here’s what worked for me (in Sierra 10.12.6).

do shell script "osascript -e 'tell application \"System Events\" to {activate, display notification with title \"TITLE\"  subtitle \"SubTitle\" sound name \"Sosumi\"} ' &>/dev/null &"

Putting the actions into a bracketed list and beginning with the “activate” command, seems to be necessary. There does seem to be a slight lag before compliance. I wasn’t even aware it could be formatted like that!

#NOTE: the trailing devnull hides the error; “36:128: execution error: No result was returned from some part of this expression. (-2763)”

Additionally, syncing speech with the alert can be done this way . . .

set line1 to "Hello world."
do shell script "osascript -e 'tell application \"System Events\" to {activate, display notification \"line3\" with title \"" & line1 & "\"  subtitle \"line2\" , say \"" & line1 & "\"} ' &>/dev/null &"

Your osascript in Terminal doesn’t work for me. Please explain in more detail how this solution is more useful than usual, if it cannot be applied in the Terminal.

You can apply tell me instead of telling System Events, as well.
.
.
UPDATE:
It worked! (in the Terminal).

In this form:

osascript -e 'tell me to {activate, display notification with title “TITLE” subtitle “SubTitle” sound name “Sosumi”} ’ &>/dev/null &

Thanks for that! :slight_smile: I know, displaying notification in the Terminal was unsolved problem for long time.

Here is a similar script I use to show an alert without stopping the script

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set text item delimiters to  " -e "  -- or return
do shell script "osascript -e " & ({"'tell application \"System Events\"'", "'display alert \"TITLE\" message \"SubTitle\"'", "'end tell'"} as text) & " &>/dev/null &"
repeat 5 times
	beep 2
	delay 2
end repeat

You will see the beeps continue even while the dialog is still up

Interesting variations! :slight_smile: (My use was to run in a background loop to monitor other activities.)