Tuesday, September 27, 2022

#1 2022-08-12 08:03:05 am

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 76
Website

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).

Applescript:

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


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

#2 2022-08-12 08:52:52 am

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 76
Website

Re: Launching a notification via osascript

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

Applescript:

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


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

#3 2022-08-12 10:47:45 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2524

Re: Launching a notification via osascript

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! smile I know, displaying notification in the Terminal was unsolved problem for long time.

Last edited by KniazidisR (2022-08-12 10:58:33 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2022-08-12 11:09:52 am

robertfern
Member
Registered: 2011-11-29
Posts: 195

Re: Launching a notification via osascript

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

Applescript:

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

Last edited by robertfern (2022-08-12 11:12:22 am)

Offline

 

#5 2022-08-12 12:21:56 pm

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 76
Website

Re: Launching a notification via osascript

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


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)