I'm trying to write a plugin for emacs that displays a notification using OS X's native notification display.
I've run into terminal-notifier
which works, but it's a dependency that doesn't work on every mac. Plus the user should be made aware that they need to install the package.
What I want to do is call a process osascript -e
and make it display the notification. The problem is, the only way to change its icon is from an external bundle. Is there any way to make osascript -e
display what I want.
starting sudo osascript
seems to do that, but it seems to be bad design and I also need to find a way to pass the root password every single time.
Unfortunately, the "display notification" documentation shows that you can't:
display notification
Posts a notification using the Notification Center, containing a title, subtitle, and explanation, and optionally playing a sound.
Syntax
display notification
– text, requiredwith title
– text, optionalsubtitle
– text, optionalsound name
– text, optional
(Even using the tell application "..."
trick from https://stackoverflow.com/a/49079025/3528 leaves you with the default notification icon.)
The reason why terminal-notifier
can is because it's using the Notification Center APIs directly which, as far as I can tell, osascript doesn't present an interface to.
You cannot. This is simply not a macOS feature exposed to AppleScript.
If you need a custom icon, consider using a pop-up "dialog" rather than a Notification Center pop-up. With timeouts and buttons you can recreate much of the functionality, though not the integration nor aesthetics.
In `display dialog', if you wish to use the standard icons: 0, 1, or 2 (stop, note, or caution), perhaps don't have osascript be the program that displays the icon. Finder, for example:
osascript -e 'tell application "Finder"' -e 'activate' -e 'display dialog \
"this is the note icon." with icon note' -e 'end tell'
or without the tell application…
you may use an icon of your choice by referencing it directly, e.g. the Terminal app's icon:
osascript -e 'display dialog "Terminal icon" with icon alias \
"Macintosh HD:Applications:Utilities:Terminal.app:Contents:Resources:Terminal.icns"'
I'm not sure what you mean by, "the only way to change its icon is from an external bundle. Is there any way to make osascript -e
display what I want." What, precisely, do you want? What have you tried?
Here's the display dialog section from Apple's documentation.
Actually, this is possible.
Just save your script as an application and then switch the applet.icns
file within the application's Contents/Resources
folder for the icon you want.
Any notifications sent from your script will use that icon.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With