在Python中阅读osx通知 [英] Read osx notification in Python

查看:83
本文介绍了在Python中阅读osx通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在阅读osx上的Notification Center通知时启动一个应用程序.是否有任何Python库可用于读取通知中心通知?

I need to launch an application on reading a Notification center notification on osx. Is there any python library for reading the notification center notifications?

推荐答案

注意:由于我目前没有运行OSX的计算机,因此我没有尝试过任何方法,但是我从谷歌搜索了以下内容:

Note: I did not try any of this as I do not have a machine running OSX at the moment, but from what I googled:

您可以尝试在使用系统事件和通知中心的python代码中添加AppleScript代码段,以分析所有可用的通知,并将它们返回到stdout,然后直接在代码中解析输出.

You could try adding an AppleScript snippet in your python code that uses System Events and Notification Center to parse all available notifications and return them to your stdout and parse the output directly in your code.

获取所有可用通知的来源: https://macosxautomation.com/mavericks/notifications/01A.html .您应该感兴趣的部分是getNotificationTitles函数的代码.糟糕的是,在该实现中,您只能获取其标题,而不能获取完整的通知,因此您可能必须解析不同的区域才能获取完整的通知正文.

Source for getting all available notifications: https://macosxautomation.com/mavericks/notifications/01A.html. The part that should interest you is the code of the getNotificationTitles function. The bad part is that in that implementation you only get their titles, not the full notification, so you might have to parse different areas to get the full notification body.

运行它并获得结果的来源:

Source for running it and getting the result: How do I embed an AppleScript in in a Python script?.

另一种方法是使用 terminal-notifier cli工具的 -list参数(作为脚本内的shell命令),然后解析结果.

Another approach would be to use the terminal-notifier cli tool's -list argument (as a shell command inside your script) and then parse the results.

该工具还有一个 python包装器.该实现包含一个list()函数,该函数承诺仅可以使用cli工具,只是您不需要从自己的代码中生成任何shell并解析其stdout.

There's also a python wrapper for this tool available. The implementation contains a list() function which promises to work just the cli tool just that you wont need to spawn any shells from your own code and parse their stdouts.

这篇关于在Python中阅读osx通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆