-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotification_script.py
More file actions
97 lines (83 loc) · 3.12 KB
/
notification_script.py
File metadata and controls
97 lines (83 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
跨平台通知脚本
支持Windows、macOS和Linux系统的桌面通知
"""
import sys
import platform
import subprocess
import os
def show_notification(title="Trae通知", message="任务完成", duration=5000):
"""
显示桌面通知
Args:
title (str): 通知标题
message (str): 通知内容
duration (int): 显示时长(毫秒)
"""
system = platform.system().lower()
try:
if system == "windows":
# Windows 使用 PowerShell 的 BurntToast 或原生通知
try:
# 尝试使用 BurntToast
cmd = f'powershell.exe -Command "New-BurntToastNotification -Text \'{message}\' -AppLogo $null"'
subprocess.run(cmd, shell=True, check=True, capture_output=True)
print(f"✅ Windows通知已发送: {title} - {message}")
except subprocess.CalledProcessError:
# 备用方案:使用 Windows 原生通知
try:
import win10toast
toaster = win10toast.ToastNotifier()
toaster.show_toast(title, message, duration=duration//1000)
print(f"✅ Windows原生通知已发送: {title} - {message}")
except ImportError:
# 最后备用方案:使用 msg 命令
subprocess.run(["msg", "*", f"{title}: {message}"], check=True)
print(f"✅ Windows消息已发送: {title} - {message}")
elif system == "darwin": # macOS
# 使用 osascript 显示通知
script = f'display notification "{message}" with title "{title}"'
subprocess.run(["osascript", "-e", script], check=True)
print(f"✅ macOS通知已发送: {title} - {message}")
elif system == "linux":
# 使用 notify-send
subprocess.run(["notify-send", title, message], check=True)
print(f"✅ Linux通知已发送: {title} - {message}")
else:
print(f"❌ 不支持的操作系统: {system}")
return False
except subprocess.CalledProcessError as e:
print(f"❌ 通知发送失败: {e}")
return False
except Exception as e:
print(f"❌ 发生错误: {e}")
return False
return True
def main():
"""
主函数 - 支持命令行参数
"""
if len(sys.argv) >= 3:
title = sys.argv[1]
message = sys.argv[2]
elif len(sys.argv) == 2:
title = "Trae通知"
message = sys.argv[1]
else:
title = "Trae通知"
message = "任务完成"
print(f"🔔 准备发送通知...")
print(f"📱 系统: {platform.system()}")
print(f"📝 标题: {title}")
print(f"💬 内容: {message}")
print("-" * 40)
success = show_notification(title, message)
if success:
print("\n🎉 通知发送成功!")
else:
print("\n❌ 通知发送失败!")
sys.exit(1)
if __name__ == "__main__":
main()