0

From python I'd been triggering notifications like this:

def show_alert(message, title="Flashlight"):
    """Display a macOS notification."""
    message = json.dumps(message)
    title = json.dumps(title)
    script = 'display notification {0} with title {1}'.format(message, title)
    os.system("osascript -e {0}".format(pipes.quote(script)))

but now I want to be able to trigger these alerts some time in the future.

I had a method using time, time.sleep(60) would trigger an alert a minute in the future.

The problem with that is that if the script is ended, or the computer sleeps, I'm not sure how realiable it would be.

Is there a way I can use python (maybe with applescripts, or other macOS tools) to schedule a notification for some arbitrary time in the future?

theonlygusti
  • 11,032
  • 11
  • 64
  • 119

1 Answers1

0

Assuming you are on macOS, you can use crontab. It executes processes at a given time. For example, every 5 hours, or every monday at 10pm.

Also, you should take a look at terminal-notifier. Here you have an example.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Carles Mitjans
  • 4,786
  • 3
  • 19
  • 38
  • How would I integrate these into a python script? It looks like crontab is very much command-line based. – theonlygusti Dec 26 '16 at 09:42
  • Indeed, crontab is only in command-line. You can make a python script that notifies you when something happens, and using crontab, you can make your mac to execute that script every day. This way, your script will be run every day, and if *something* happens, it notifies you. – Carles Mitjans Dec 26 '16 at 09:44
  • I need a way for python to interface with crontab if this answer is going to be useful. – theonlygusti Dec 26 '16 at 09:58
  • Feel like helping out? – theonlygusti Dec 29 '16 at 05:36
  • @theonlygusti "but now I want to be able to trigger these alerts some time in the future." Well, `crontab` does that. How ahead in the future? – Carles Mitjans Dec 29 '16 at 09:32
  • It would be a user-defined delay. I don't know how to use crontab, I tried using `crontab -e` etc. but it didn't even seem to work properly. So using it through a python script will probably be even more difficult for me. – theonlygusti Dec 29 '16 at 09:38