14

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.

5 Answers 5

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Not quite what I was looking for, as I'm looking to run this from within emacs... But thanks!
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 <path to application>
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.

5 Comments

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).
Then why is it that other apps can put notification with their custom icon then? Like this: imgur.com/I7EFlfZ
@frakman1 other apps are not AppleScripts, the narrow subject of the question.
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
The latter script does not work on newer macOS versions, since the Applications folder is now under "System" and not directly under disk: Simply adding "System:" to the path of the icon will fix it: bash osascript -e 'display dialog "Terminal icon" with icon alias "Macintosh HD:System:Applications:Utilities:Terminal.app:Contents:Resources:Terminal.icns"'
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.

Comments

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")'

Comments

0

This fun little example worked for me in macOS Catalina: It randomly changes the icon of an app and it's notifications. Save it in Script Editor by using "File"> OptionKey> "Save As..."> then "File Format"> Application. It makes an app which gives itself a new icon whenever you run it, then it uses that or another icon in a pop-up notification! -------------#GIVE MYSELF A RANDOM ICON#----------------- set target to POSIX path of ((path to me) as text) set icon to some item of (paragraphs of (do shell script "mdfind '.icns' ")) setIcon to icon for target say "Changing my icon, please wait!" delay 2 run script "display notification "" & (icon) & "" with title "" & (name of me) & " " " say "process complete" ---------------------DECLARATIONS--------------------- use framework "AppKit" property this : a reference to current application property NSWorkspace : a reference to NSWorkspace of this property NSImage : a reference to NSImage of this ---------------------IMPLEMENT--------------------- on duit(icon, target) setIcon to icon for target end duit ---------------------HANDLERS--------------------- to setIcon to iconPath for filePath set sharedWorkspace to NSWorkspace's sharedWorkspace() set newImage to NSImage's alloc() set icon to newImage's initWithContentsOfFile:iconPath set success to sharedWorkspace's setIcon:icon forFile:filePath options:0 end setIcon ---------------------#######---------------------

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.