View Mac desktop notifications from Python

There was a situation where I wanted to get Mac notifications from a Python script, so

--Execute AppleScript with a command --Execute the above command from Python

I created it in the flow.

Show desktop notifications in AppleScript

In AppleScript

notification.scpt


display notification "Hello World"

You can view desktop notifications with. In the figure below, it is executed from ScriptEditor.app.

notification_applescript.png

Execute AppleScript from a command

Use the ʻosascript` command.

At terminal.app

$ osascript -e 'display notification "Hello World"'

So you get the same result as before.

Call a command in a Python script

It can be executed by passing the above command as a character string to the argument of the system method of the ʻos` module.

notification.py


import os

os.system("osascript -e 'display notification \"Hello World\"'")

I want to execute the above process only when the execution environment is Mac

If the Python script may be executed in an environment other than Mac, it may be necessary to prevent the desktop notification from being displayed in some environments.

There is a module called platform in the Python standard library, so I used this.

Running platform.system () will return the system name as a string like 'Linux', 'Darwin','Java', 'Windows'. If you are not sure, an empty string will be returned.

Summary

The above is summarized as follows.

notification_matome.py


import os
from platform import system

pf = system()
if pf == "Darwin":
    os.system("osascript -e 'display notification \"Hello World\"'")

Future outlook

The notification comes from the "script editor", so I'd like to change it if possible.

environment

Reference

[How to operate the notification center of macOS from python-I suffer from output](http://atc.hateblo.jp/entry/2018/06/25/python%E3%81%8B%E3%82%89macOS % E3% 81% AE% E9% 80% 9A% E7% 9F% A5% E3% 82% BB% E3% 83% B3% E3% 82% BF% E3% 83% BC% E3% 82% 92% E6 % 93% 8D% E3% 82% 8B% E6% 96% B9% E6% B3% 95)

platform --- Refer to the specific information of the running platform — Python 3.8.5 documentation

Recommended Posts

View Mac desktop notifications from Python
Implementation of desktop notifications using Python
Call C / C ++ from Python on Mac
Update Python on Mac from 2 to 3
sql from python
python on mac
MeCab from Python
Use Tor to connect from urllib2 [Python] [Mac]
Use thingsspeak from python
Touch MySQL from Python 3
Operate Filemaker from Python
Use fluentd from python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Python from or import
Push notifications from Python to Android using Google's API
Use MySQL from Python
Preferences to generate animated GIFs from Python on Mac
Install Python on Mac
Install Python 3 on Mac
Run python from excel
Install python from source
Execute command from Python
Operate neutron from Python!
Read QR code from image file with Python (Mac)
Use MySQL from Python
Operate LXC from Python
Manipulate riak from python
Force Python from Fortran
Use BigQuery from python.
Install Python 3.4 on Mac
Execute command from python
Mac environment construction Python
[Python] Read From Stdin
Run iphone safari from mac with python + selenium + safari-webdriver
Use mecab-ipadic-neologd from python
Flatten using Python yield from
Call CPLEX from Python (DO cplex)
Deep Python learned from DEAP
Post from Python to Slack
Grammar features added from Python3.6
Make MeCab available from Python3
Install pygame on python3.4 on mac
Information obtained from tweet_id (Python)
Python3 + Django ~ Mac ~ with Apache
Set Up for Mac (Python)
OCR from PDF in Python
Run illustrator script from python
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Study from Python Hour4: Object-oriented ②
Query Athena from Lambda Python
Access Oracle DB from Python
[Python] Insert ":" in MAC address
Study from Python Hour3: Functions
Python installation (Mac edition) (old)
Python environment construction For Mac
Start / stop GCE from python
Handling of python on mac
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]