学计算机的那个

不是我觉到、悟到,你给不了我,给了也拿不住;只有我觉到、悟到,才有可能做到,能做到的才是我的.

0%

Mac OS Notification with Python

一些在后台运行的任务,需要在特定条件下对用户发起通知,如果有跨端要求,可以用第三方库,不过需要付费,优势在于可同时兼容移动端和桌面端,这里只是个人使用,所以推荐使用Mac自带Apple Script来添加,本来考虑通过向Reminders添加Event的方式,实现Mack & iOS同时提醒,不过只找到OC和Swift的API,没找到Py的API

Notification

display notification

The AppleScript command:

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

With osascript:

1
osascript -e 'display notification "hello world!" with title "Greeting" subtitle "More text"'

make sound

The AppleScript command:

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

executed by osascript:

1
osascript -e 'display notification "hello world!" with title "Greeting" subtitle "More text" sound name "Submarine"'

If the name of the sound is incorrect Mac will make an alert sound.

The sound can be one of the files in /System/Library/Sounds or in ~/Library/Sounds.

Code in Py 1

1
2
3
4
5
6
7
8
import os

def notify(title, text):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))

notify("Title", "Heres an alert")

此示例没有转义引号、双引号或其他特殊字符,因此这些字符将无法在通知的文本或标题中正确工作

update:这应该适用于任何字符串,不需要转义任何内容。它的工作原理是将原始字符串作为参数传递给apple脚本,而不是试图将它们嵌入到apple脚本程序的文本中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import subprocess

CMD = '''
on run argv
display notification (item 2 of argv) with title (item 1 of argv)
end run
'''

def notify(title, text):
subprocess.call(['osascript', '-e', CMD, title, text])

# Example uses:
notify("Title", "Heres an alert")
notify(r'Weird\/|"!@#$%^&*()\ntitle', r'!@#$%^&*()"')

Code Demo 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os

def displayNotification(message,title=None,subtitle=None,soundname=None):
"""
Display an OSX notification with message title an subtitle
sounds are located in /System/Library/Sounds or ~/Library/Sounds
"""
titlePart = ''
if(not title is None):
titlePart = 'with title "{0}"'.format(title)
subtitlePart = ''
if(not subtitle is None):
subtitlePart = 'subtitle "{0}"'.format(subtitle)
soundnamePart = ''
if(not soundname is None):
soundnamePart = 'sound name "{0}"'.format(soundname)

appleScriptNotification = 'display notification "{0}" {1} {2} {3}'.format(message,titlePart,subtitlePart,soundnamePart)
os.system("osascript -e '{0}'".format(appleScriptNotification))



if __name__ == '__main__':
displayNotification("message","title","subtitle","Pop")
#displayNotification("message","title","subtitle")
# displayNotification("message","title")
#displayNotification("message")

display alert

display alert with confirmation request

the AppleScript command:

1
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."

The osascript wrapped command:

1
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."'

Saying

Saying Hello World

The AppleScript command:

1
say "Hello World!"

wrapped in osascript on the command line:

1
osascript -e 'say "Hello World!"'

参考

Displaying Notifications

Display notification from the Mac command line