如何写一个脚本任务提醒

wen 实用脚本 2

本文目录导读:

如何写一个脚本任务提醒

  1. Windows 系统:使用任务计划程序
  2. Linux/macOS:使用 Cron
  3. Python 跨平台脚本
  4. 使用 notify-send 命令 (Linux)
  5. macOS 使用 AppleScript
  6. 使用 Python with 桌面通知库
  7. 高级功能:带 GUI 界面的提醒
  8. 使用方法

Windows 系统:使用任务计划程序

批处理脚本 (.bat)

@echo off
echo 提醒:请检查每日报告!
msg * "提醒:请检查每日报告!"
timeout /t 10

PowerShell 脚本

# task-reminder.ps1
$duration = 10
$title = "任务提醒"
$message = "请检查每日报告完成情况!"
[System.Windows.Forms.MessageBox]::Show($message, $title)

Linux/macOS:使用 Cron

创建脚本

#!/bin/bash
# /usr/local/bin/task-reminder.sh
echo "提醒:完成数据备份任务"
notify-send "任务提醒" "请完成数据备份"  # Linux
# osascript -e 'display notification "请完成数据备份" with title "任务提醒"'  # macOS

设置 Cron 定时任务

# 编辑 crontab
crontab -e
# 每天上午9点执行
0 9 * * * /usr/local/bin/task-reminder.sh
# 每周一、三、五下午2点
0 14 * * 1,3,5 /usr/local/bin/task-reminder.sh

Python 跨平台脚本

# task_reminder.py
import time
import platform
import subprocess
def send_notification(title, message):
    system = platform.system()
    if system == "Windows":
        # Windows 通知
        import ctypes
        ctypes.windll.user32.MessageBoxW(0, message, title, 1)
    elif system == "Darwin":  # macOS
        subprocess.run(['osascript', '-e', 
            f'display notification "{message}" with title "{title}"'])
    else:  # Linux
        subprocess.run(['notify-send', title, message])
# 任务提醒逻辑
tasks = {
    "9:00": "开始工作",
    "12:00": "午餐时间",
    "15:00": "下午会议",
    "17:00": "整理文档"
}
while True:
    current_time = time.strftime("%H:%M")
    if current_time in tasks:
        send_notification("任务提醒", tasks[current_time])
        time.sleep(60)  # 避免重复通知
    time.sleep(30)  # 每30秒检查一次

使用 notify-send 命令 (Linux)

#!/bin/bash
# 任务提醒脚本"任务提醒"
MESSAGE="请完成以下任务:\n1. 更新周报\n2. 检查邮件"
# 使用 notify-send
notify-send -u critical "$TITLE" "$MESSAGE" -t 10000
# 或者使用 Zenity
zenity --warning --title="$TITLE" --text="$MESSAGE" --timeout=10

macOS 使用 AppleScript

-- task_reminder.scpt
display notification "请完成今日工作总结" with title "任务提醒" subtitle "每日任务"
delay 5
display alert "重要提醒" message "请检查项目进度!"

使用 Python with 桌面通知库

# 安装 plyer 库
# pip install plyer
from plyer import notification
import schedule
import time
def send_reminder(task_name):
    notification.notify(
        title="任务提醒",
        message=f"请完成:{task_name}",
        app_name="Task Reminder",
        timeout=10
    )
# 设置定时任务
schedule.every().day.at("09:00").do(send_reminder, "晨会准备")
schedule.every().day.at("12:30").do(send_reminder, "午休提醒")
schedule.every().monday.at("10:00").do(send_reminder, "周报提交")
while True:
    schedule.run_pending()
    time.sleep(1)

高级功能:带 GUI 界面的提醒

# tkinter 版本
import tkinter as tk
from tkinter import messagebox
import schedule
import time
import threading
class ReminderApp:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("任务提醒工具")
        self.root.geometry("300x200")
        # 添加任务按钮
        self.add_task_btn = tk.Button(self.root, text="添加任务", command=self.add_task)
        self.add_task_btn.pack(pady=10)
        # 任务列表
        self.task_listbox = tk.Listbox(self.root)
        self.task_listbox.pack(fill=tk.BOTH, expand=True)
    def add_task(self):
        # 弹出输入对话框
        import tkinter.simpledialog
        task_name = tkinter.simpledialog.askstring("添加任务", "任务名称:")
        task_time = tkinter.simpledialog.askstring("添加任务", "提醒时间 (HH:MM):")
        if task_name and task_time:
            self.task_listbox.insert(tk.END, f"{task_time} - {task_name}")
            schedule.every().day.at(task_time).do(self.show_reminder, task_name)
    def show_reminder(self, task_name):
        messagebox.showinfo("任务提醒", f"请完成:{task_name}")
    def run(self):
        # 启动定时线程
        def schedule_thread():
            while True:
                schedule.run_pending()
                time.sleep(1)
        threading.Thread(target=schedule_thread, daemon=True).start()
        self.root.mainloop()
if __name__ == "__main__":
    app = ReminderApp()
    app.run()

使用方法

  1. 选择适合你的方法

    • Windows 用户:推荐使用任务计划程序 + 批处理
    • Linux 用户:使用 Cron + notify-send
    • macOS 用户:使用 AppleScript 或 Cron
    • 跨平台:Python 脚本
  2. 设置自动启动

    • Windows:将脚本添加到启动文件夹
    • Linux:使用 systemd 或 autostart
    • macOS:使用 LaunchAgent
  3. 优化建议

    • 添加日志记录功能
    • 支持重复提醒
    • 集成日历同步
    • 添加声音提醒

选择最适合你需求的方法,根据实际情况调整脚本参数即可。

抱歉,评论功能暂时关闭!