Change icon of notification when using osascript -e “display notification”

风流意气都作罢 提交于 2020-07-06 11:58:26

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/48856158/change-icon-of-notification-when-using-osascript-e-display-notification

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!