Creating notifications from scripts

Hi,

First of all hello to everyone. Not only am I brand new to this forum but also to the whole world of Macs. I work for an MSP here in the UK and am keen to get get started with some automation to help out our customers who use Macs as their daily drivers.

I’ve already baked some software installation scripts so we can automate the deployment of our key applications through our RMM product (ConnectWise Automate) but I’d like to put some wrapping around the scripts so that we inform the end user when the particular packages are installed. I managed to make a popup message using a command like:

osascript -e ‘display alert “Hello” with title “Message from Tim”’

Now that worked just fine. However, if I run that through our RMM product, nothing happens. The script completes fine but there’s no message. I’m assuming - coming from a Windows sysadmin perspective - that the script is not truly running as the logged in user but as a service account (whichever account the management agent is running as) and that’s perhaps why the message does not appear on the logged-in user’s desktop.

Does anyone know of a way that I could get a popup to appear? Am I barking up the wrong tree using osascript? Is there another way I could achieve the same goal?

Cheers
Tim.

Hi. Welcome to MacScripter.

It’s not clear to me how you’re running scripts, but just to point out that display alert doesn’t have a with title parameter. I’d guess from the subject of your post that you mean display notification.

Hi,

Ah yes, sorry. I’ve been playing with the commands and got them mixed up.

With regards running the script, we have a management agent installed onto Macs which collects inventory information, allows remote control and so forth. It also allows the running of scripts. What happens is that the agent is sent a shell command and then it executes it somehow. That’s as much as I know in terms of the Mac agent… Maybe not terribly helpful information, I know.

I don’t know what to suggest. On my High Sierra machine, this works fine as a shell command in Terminal:

[format]osascript -e ‘display notification “Hello” with title “Message from Tim”’[/format]

And these AppleScript script equivalents work both in Script Editor and as AppleScript applications:

display notification "Hello" with title "Message from Tim"
do shell script "osascript -e 'display notification \"Hello\" with title \"Message from Tim\"'"

I’d guess your problem’s something to do with either the way the shell command’s being passed to the agent, the way the agent’s handling it, or something earlier in the script which is stopping it before the display notification command’s reached. :confused:

Nigel, I tried your three examples in Sierra(10.12) on a MacPro and the first two worked but the third produced no notification.

Replacing ''display notification" with “display dialog” worked fine in every example including number three.

Strangely, I reproduced the omission by saving the script as an application to the desktop, then calling it to run from Osascript, from Finder, and from Script Editor. Of the three methods, only osascript produced a notification.

Also notable, is that double clicking the app fails to produce a notification, but opening and running it in Script Editor does produce a notification.

I’m wondering if High Sierra handles all without fail, or if anyone else has a similar omission in Sierra? (note: beeps & chimes were just extra audio indicators for proving completion of the script.)


#MAKE A SCRIPT
set mySCRIPTtoSAVE to "
beep
set HEADER to \"HEADER\"
set SubHead to \"SubHead\"
set bottomLine to \"bottomLine\"
set soundName to \"Sosumi\"
(display notification bottomLine with title HEADER subtitle SubHead sound name soundName)
delay 1
say \"Hello!\"
beep"
set appNAME to "myTEST.app"
set desktopPATH to POSIX path of (path to desktop)
set appPATH to desktopPATH & appNAME

#SAVE IT :
do shell script "osacompile -o " & quoted form of appPATH & " -e '" & mySCRIPTtoSAVE & "'"

#RUN IT any of three ways:
do shell script "osascript " & quoted form of appPATH --->YAY! It made a notification!
--tell application "Finder" to open item ((POSIX file appPATH) as text) --->no notification?!
--tell application appNAME to activate --->no notification?!

Model: Mac Pro
AppleScript: 2.9
Browser: Safari 602.1.50
Operating System: macOS 10.14

You should set all needed checkboxes in Display Notifications Preferences for each application which you want to recieve notifications, individually.

Whether it’s Script Editor, Script Debugger or your newly created application, it makes no difference. No distribute notifications should be turned off

But, wait, were is your display notification command in the 2nd and 3rd case?

set theAppFile to path to application "Mail"

tell application "Finder" to open theAppFile
display notification "EVENT:   Mail.app is opened using the Finder"

I tested this code in the Script Debugger. It sends the notification, and not the Finder in the given case

Answer: The notification command is on line 7 of the application “myTEST.app” which is saved to the desktop by osacompile when the script I posted is run. The second and third options can be tried by un-commenting whichever one is being tested so that only one of the three is uncommented.

I tried running your script in Script Editor 2.9 (AppleScript 2.5), and as a saved app, and as an app saved by osascript, but the result each time was “An error of type -10828 has occurred”. I noticed you appear to be using Mohave on a laptop whereas I was attempting to understand an inconsistency on the Sierra operating system using a MacPro desktop machine. (A machine which I often wonder about because it’s hardware was modified by it’s previous owner).

Since the app I’m working on is generated “on the fly” by osacompile, I’m not sure setting preferences for it in Notifications would work for me. In fact, I’ve been dumping the apps into the temporary items folder so that they disappear after one or two uses. I only posted it here as a save-to-desktop app for the convenience of anyone testing it.

Here, running High Sierra, the script:

set theAppFile to path to application "Mail"

tell application "Finder" to open theAppFile
display notification "EVENT:   Mail.app is opened using the Finder"

open" Mail" and issue a notification
when triggered from the Editor or when it’s app-icon is double-clicked.
Never used OsaCompile.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 4 janvier 2020 23:11:40

I triple checked, the settings applied to Script Editor and the script applications urge them to issue a sound. The sound level is the highest one and no sound is issued.

I should note that the display notification shown in the corner of the screen using osascript is not perfect, as it lacks a subtitle and sound. On my computer, this happens with any application to which I do not give the permissions to send display notifications

It is strange that such a simple code as mine does not work for you. It seems to me that the point here is in the settings regarding notifications. I tried my code now as application too. It works as well, as with Script Debugger - full form notification is shown

NOTE: If you previously had No distribute notifications ON, and you changed this setting only recently, then you need to wait for this to take effect. Sometimes, half a day!

Yvan, were you see I use the sound in my script?

Ivan, I got the same error on your code. I just ran it “right of the screen” using gah e “Open this Scriplet in your Editor:” link and it threw an error -10828.

Tinkering with a tinier version of “app” versus “script” I had two strange things happen:

1st: the double-clicked app would ignore the notification code while the in-Editor run of the script worked fine.

2nd: After tinkering with it, the saved app would pop-up a notification but it was not in the same order as when run from within the Editor.

here’s the tiny version. The return was to lock out a loop in the experiment.It was saved as an app then tested as a double click and also run in Script Editor.

beep
set HEADER to "HEADER"
set SubHead to "SubHead"
set bottomLine to "bottomLine"
set soundName to "Sosumi"
(display notification bottomLine with title HEADER subtitle SubHead sound name soundName)
delay 1
say "Hello!"
beep

return "done"

KniazidisR, were you see that I wrote about what you got?

With this edited code I get the sound.

set theAppFile to path to application "Mail"

tell application "Finder" to open theAppFile
set soundName to "Sosumi"
display notification "EVENT:   Mail.app is opened using the Finder" sound name soundName

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 5 janvier 2020 11:12:36

This is a knotty one. :confused:

I no longer have Sierra or High Sierra, but Mr. Science’s “osacompile” script produces the same results on my Mojave system.

As KniazidisR has pointed out, the Notifications settings have to be set for the relevant application in System Preferences. However, the relevant application in this case appears to be Script Editor! When the “osacompile” script’s run in either Script Editor or Script Debugger, the style of the notification displayed (or not) is whatever’s currently set for Script Editor in the Notifications preferences. The same’s true if the “osascript” command is subsequently executed in Terminal. Change Script Editor’s Notifications settings, and the style of notification displayed by the applet immediately changes in the Script Debugger and Terminal contexts too.

But no notification’s displayed if the applet’s opened by the Finder (either through a script or by double-clicking) or activated in its own right by a script. I’ve no idea why this is, although I notice that neither the Finder nor the applet are shown in the Notifications preferences on my machine. This could be just coincidence, of course. :confused:

There’s a Notifications setting for OSAScript in my System Preferences, but this doesn’t seem to have any affect on the situation.

Edit: Further research (ie. actually looking at “osacompile”'s man file) shows that the default creator for files created with “osacompile” is “ToyS”, ie. Script Editor, so that could explain why Script Editor’s Notifications settings apply. I haven’t got round yet to fooling with “osacompile”'s -c option.
Further edit: The current “man osacompile” file on my Mojave system says that, by default, no creator is set if the -c option isn’t specified, which isn’t what is says in the older record to which I was referring just now. In fact, though, the applet created by Mr. Science’s “osacompile” script has the creator code “aplt”. Somebody at Apple seems determined to cause confusion! :confused:

Thanks, Nigel. Good to know it’s not just me getting goofy results! :smiley:
Apparently there are several flavors of “Vanilla” to sort through when trying to make a script which behaves with consistency across the plurality of Apple scripting environments.