Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change icon of notification when using osascript -e "display notification"

Tags:

applescript

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.

like image 592
Petrosyan Alexander Avatar asked Feb 18 '18 20:02

Petrosyan Alexander


3 Answers

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, required
with title – text, optional
subtitle – text, optional
sound 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.

like image 111
Rob Howard Avatar answered Sep 22 '22 05:09

Rob Howard


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.

like image 10
Joel Reid Avatar answered Nov 09 '22 13:11

Joel Reid


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.

like image 8
MJ Walsh Avatar answered Nov 09 '22 11:11

MJ Walsh