11

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.

4 Answers4

11

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.

MJ Walsh
  • 593
  • 6
  • 11
  • Furthermore, if the text of the notification is dynamic, that is, it changes based on other factors rather than being hardcoded into the script, you can always copy the text you want to display to the clipboard and then write a script that displays the clipboard in a notification. – Peter Schorn Feb 05 '20 at 23:05
  • Not quite what I was looking for, as I'm looking to run this from within emacs... But thanks! – Petrosyan Alexander Apr 25 '20 at 19:53
  • For anyone looking for slightly more detail on this. You create a new script using the Script Editor application, put the stuff after the `osascript -e` into the script, then File-> Export ... and pick Application for the "File Format" leaving all the other options as default (unchecked). Then you can launch the exported application on the command line via `open ` – user1411349 Nov 17 '20 at 02:35
10

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.

Feline
  • 773
  • 3
  • 14
Joel Reid
  • 955
  • 1
  • 8
  • 14
  • 3
    A note for readers confused by this as I was: the original question asks about *notification*, but both the examples here display a *dialog* (with an ok+cancel button that must be hit). – Rob Howard Apr 02 '19 at 00:33
  • 1
    Then why is it that other apps can put notification with their custom icon then? Like this: https://imgur.com/I7EFlfZ – Frak Jun 29 '19 at 14:17
  • 1
    @frakman1 other apps are not AppleScripts, the narrow subject of the question. – Joel Reid Jul 01 '19 at 14:02
  • Using the first command doesn't work on Mac 12.3.1 (Terminal 2.12.5), it has to be single line. [https://i.ibb.co/KhCdk98/image.png](https://i.ibb.co/KhCdk98/image.png) – Coder Tavi Apr 23 '22 at 08:22
5

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.

Rob Howard
  • 1,789
  • 2
  • 20
  • 38
2

Best solution for was to:

  1. Create an AppleScript app and replace the applet.icns file, inside the /Contents/Resources folder, with your icon file

  2. create a handler:

    on notify(vTxt, vTtl)
     display notification vTxt with title vTtl
    end notify
    

Don't forget to create an 'on idle' handler as well:

on idle
 return 10
end idle
  1. Save the app as "Stay open"
  2. Run the app

now you can call the handler from another app or process and the notification will display with the icon of your app. Call it from the command line :

osascript -e 'tell application "MyApp" to notify("Message", "TITLE")'