Search code examples
applescript

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


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.


Solution

  • 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.