在mac命令行执行显示通知

需要用的工具:
osascript在macos上可以执行AppleScript, JavaScript等.
这里介绍AppleScript两个常用命令: display, say.

display

这个命令可以在mac上发送系统通知,macos 会在侧边栏显示这个通知消息。
AppleStript这样写 display notification "hello world!"

为了执行这条命令需要用到osascript, 并且需要-e参数,后面跟的单引号字符引用的命令
执行发送这条通知:
osascript -e 'display notification "hello world!"'

Screen Shot 2019-07-17 at 12.31.58.png

这条通知显示在屏幕右上角,3秒后消失。

通知

通知太多直接右侧边栏有关闭按钮可以关闭所有通知

带标题的通知

display notification "hello world!" with title "This is the title"

可以直接执行的命令:
osascript -e 'display notification "hello world!" with title "This is the title"'

带副标题的通知

display notification "hello world!" with title "Greeting" subtitle "More text"

可以直接执行的命令:
osascript -e 'display notification "hello world!" with title "Greeting" subtitle "More text"'

Screen Shot 2019-07-17 at 12.54.40.png

带提醒声音的通知

音效文件在这 /System/Library/Sounds 或 ~/Library/Sounds.

display notification "hello world!" with title "Greeting" subtitle "More text" sound name "Submarine"

可以直接执行的命令:
osascript -e 'display notification "hello world!" with title "Greeting" subtitle "More text" sound name "Submarine"'

显示对话框 alert和确认

display alert "Hello World!" message "longer text can be added in the message field and it will be all shown on the pop-up alert."

可以直接执行的命令:
osascript -e 'display alert "Hello World!" message "longer text can be added in the message field and it will be all shown on the pop-up alert."'

当然这个alert不会显示在通知中心

播放语音

AppleScript 命令为: say "Hello World!"

可以执行的命令为:
osascript -e 'say "Hello World!"'

当然这是系统语音TTS,只能英文转文字。