1

I'd like to create a python script that sends me a windows toast notification and gives me two buttons, one to postpone, and one to start a backup (powershell script).

I tried multiple libraries, like winotify, win10toast, but settled on win11toast.

def on_postpone():
    print("Postponed")

print(f"starting Backup notification")

ps_script = os.path.join(user_folder, 'Pictures', 'Backup_Starter.ps1')
buttons = [
    {'activationType': 'protocol', 'arguments': f'{ps_script}', 'content': 'Start Backup'},
    {'activationType': 'protocol', 'arguments':'', 'content': 'Postpone Backup'}
]
    
# send toast
 toast(
    "Script Ready to Start",
    f"Click 'Start Script' to start, or 'Postpone' to postpone the Backup.",
    duration="long",  
    buttons=buttons,
    audio='ms-winsoundevent:Notification.Looping.Alarm'
)

# if Nothing clicked
print("Postponing the script...")
on_postpone()

While this kinda works, I cannot find a way to make the Start Backup button start the script, it does just open in notepad and also I cannot find a way to execute on_postpone always, except it there was a click on Start Backup.

Package used: https://pypi.org/project/win11toast/

2 Answers 2

0

I'm not sure if this Project is still active, but i've ran into the same Problem. with almost the same Intentions. Only Solution i could make work was to get rid of the Buttons and use the Notification itself:

from win11toast import *
import os, sys, win11toast


def on_postpone():
    print("Postponed")

print(f"starting Backup notification")

# send toast
toast(
    "Script Ready to Start",
    f"Click this Notification to start the Script, or ignore it to postpone the Backup.",
    duration="long",
    on_click=r'c:\path\to\backup_script',  # i've tried it with raw-path to script.py
    audio='ms-winsoundevent:Notification.Looping.Alarm'
    )

# if Nothing clicked
print("Postponing the script...")

In this way, you start your Backup-Script by clicking the Notification. To postpone it, close the Notification or simpy ignore it (stays in Notification Center until you clear it or restart your PC)

//Testet on Windows 10 and 11

Reference: some Books, older Scripts from me and a lot of Time google Stuff

Sign up to request clarification or add additional context in comments.

1 Comment

if your script still starts in notepad, check 'Open with' defaults or import and call a child.py-script which forces your backup-script to start in powershell.
0

It's possible to combine the callback used for clicking any area of the notification with button click handling. See code example below:

from win11toast import toast
import subprocess

def open_notepad():
    subprocess.Popen(["notepad.exe"])

def on_click(args):
    print(args['arguments'][5:])
    if args['arguments'][5:] == 'Yes':
        print('Yes clicked!')
        open_notepad()

toast('Ask user', 'Do you want to open Notepad ?', on_click, buttons=['Yes', 'No'])

Here is another examples: https://github.com/GitHub30/win11toast/discussions/3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.