Display Notification specifically as Banner or Alert?

I’d like to be able to display a Notification and specify whether it should be a Banner or Alert. I assume there’s no easy way to do this - if there is, just let me know and ignore the rest of this post.

Assuming no easy way exists, I was thinking someone could write 2 really small applications (or perhaps just one that we copy and rename?). These applications would show up in Notification preferences, and could be assigned the desired Notification type. Then we would launch them from KM with some sort of parameters telling them what to display.

Does this sound doable? Any ideas on actual implementation? Thanks.

Hey Dan,

As far as I know the Notifications API doesn’t have any provisions for this.

Easy enough to do with AppleScript.

Use the Script Editor.app to save as an application.

on displayNotification(notificationText, theTitle, theSubtitle, theSoundName)
  display notification notificationText ¬
    with title theTitle ¬
    subtitle theSubtitle ¬
    sound name theSoundName
end displayNotification

Call it like this:

set notificationText to "My Notification Body Text"
set theTitle to "My Title"
set theSubtitle to "My Subtitle"
set theSoundName to "My Sound Name"

tell application "testApp"
  displayNotification(notificationText, theTitle, theSubtitle, theSoundName)
end tell

Change the app-name to suit.

The AppleScript applications will show up in the Notification prefs, where you can adjust their specifics.

-Chris

2 Likes

Awesome! I was wondering if something like this was possible, but I found it impossible to Google what constitutes an “Application”, as far as Notifications are concerned. Thanks!!

Where should I save the the AppleScript application? Is there a standard place to put these things?

Hey Dan,

The canonical locations for user-scripts are:

# Global
~/Library/Scripts/

# App-Specific
~/Library/Scripts/Applications/
~/Library/Scripts/Applications/Keyboard Maestro/

But you’re not strictly limited to that.

-Chris

Are you familiar with Growl?

It is very flexible.

But if I’m going to tell application "testApp", the system needs to know where to find it, right?

Is Growl still viable, with El Capitan? I don’t see many positive things being said about it lately…

Maybe not. That’s really too bad, since, IMO, it provides better notifications than the Apple notification system.

It will likely find it wherever, but another place would be ~/Applications or even /Applications, since you have now created an application.

1 Like

Chris -

Is there a better way to do this:

on displayNotification(theTitle, theSubtitle, theMessage, theSound)
  if (theSound = "None") then
    display notification theMessage ¬
      with title theTitle ¬
      subtitle theSubtitle
  else
    display notification theMessage ¬
      with title theTitle ¬
      subtitle theSubtitle ¬
      sound name theSound
  end if
end displayNotification

Thanks.

When you created the AppleScript applet the system registered it – you do not need to refer to its location.

Test it for yourself.

-Chris

Hey Dan,

No. The silly function insists on playing the default sound if it doesn’t understand the input.

I’ve tried “null” and “missing value” and “” (empty string).

So you’re pretty much stuck with an IF-structure.

-Chris

Thanks. I had a feeling that was the case, but I thought I’d check.

I did test it for myself, but just because something works when I test it doesn’t mean I know enough to test all scenarios! :stuck_out_tongue: For instance:

What about name conflicts? What if I name the applet the same name as one in another folder?

Along the same lines, are there any sort of naming conventions for things like this, that I should be following?

Thanks.

@ccstone Chris -

I’m experimenting with this applet, trying to do something else with a “countdown timer” function I’ve got. I added some code before the “onDisplayNotification”, and it gets executed when the applet is run by itself (which was what I wanted):

-- snooze button code
tell application "Keyboard Maestro Engine"
	do script "BitBarTimer__SetTimer" with parameter "10"
end tell

on showTimerExpired()
	set _message to "Click 'Show' to sleep 10 minutes"
	set _title to "Time is up!"
	set _sound to "Ping"
	display notification _message ¬
		with title _title ¬
		sound name _sound
end showTimerExpired

The idea being that when the Notification Alert pops up, I could click the “Show” button and it would act as a “snooze button”.

It works, except the “snooze button” code also runs when the applet is invoked with the subroutine specified, like this:

tell application "ShowTimerExpired"
	showTimerExpired()
end tell

Is there any way around this? I hope this is clear. If not, just let me know.

Thanks!

I am having problems with Chris's code. If I run the macro by selecting "Try" from the Gear menu it runs fine and shows the notification. However if I run the macro with the hotkey (CTRL =) I get different results. First the icon for the app "Notification" bounces in the dock. Clicking on the icon brings up a dialog box "Press Run to run this script, or Quit to quit." Pressing the Run button dismisses the dialog but no notification appears. Does anyone no what's going on here.

Carl

Not sure. Did you try changing “ignore results” to show results in a window? It might give you an error message.

Also, just to be sure, you saved “Notification” as an application, right? My guess is you did, or it wouldn’t work using “try”, but you never know, so I thought I’d ask.

No error message displayed when the macro is run and the Engine log just has
2016-05-25 10:02:27 Execute macro ‘Test Notifications’ from trigger The Hot Key ⌃= is pressed

Carl

Not sure.

I just uploaded my plugin - you might want to download it and see what’s different. Even without installing it, you can open my “notifier” applets in Script Editor and see if anything’s different (besides the icons).

I get exactly the same results with your plugin: works if I select “Try” but doesn’t work if I use the hotkey (bouncing icon in the dock etc).

Carl