MacOS下命令行产生Notification通知实现方式(AppleScript或Shell等举例)

2022-06-06 2234点热度 0人点赞 0条评论

有时候写一些MacOS的小脚本,(例如AppleScript,或者Shell),需要在执行后,给自己一个发一个系统提醒通知,就想其他软件一样给处的Notification。

来看下现成的例子,直接复制过去可用!效果如下:

AppleScript的例子:

display notification "通知内容" with title "通知标题"

display notification "通知内容" with title "通知标题" subtitle "副标题"

display notification "通知内容" with title "通知标题" subtitle "副标题" sound name "default"
display notification "通知内容" with title "通知标题" subtitle "副标题" sound name "Frog"

以上看到可以给通知设置『通知内容』『标题』『副标题』,还可以设置通知的声音,如果你还要设置其他声音,可以找到文件夹:/System/Library/Sounds  和 ~/Library/Sounds 然后复制声音文件的名称(不需要带后缀名)

Shell脚本的例子

Shell脚本的调用其实最后也是调用的applescript来执行的(利用osascript -e命令)如下:

# 基础用法
osascript -e "display notification \"通知内容\" with title \"通知标题\""
osascript -e "display notification \"通知内容\" with title \"通知标题\" subtitle \"副标题\" sound name \"Frog\""

# 通知内容或者标题是参数的用法
osascript -e "display notification \"$text\" with title \"$title\""

其他实现

如果以上的实现方式没有满足你的需求,可以参考这个开源的实现:

https://github.com/julienXX/terminal-notifier

可以自定义图标、点击后打开链接或执行指定命令等

安装方法(homebrew):

brew install terminal-notifier

使用举例(更多例子请看https://github.com/julienXX/terminal-notifier):

$ terminal-notifier -group 'address-book-sync' -title 'Address Book Sync' -subtitle 'Finished' -message 'Imported 42 contacts.' -activate 'com.apple.AddressBook'

效果图:

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论