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