-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript_Exe_BurntToast.ps1
More file actions
193 lines (157 loc) · 5.87 KB
/
Copy pathScript_Exe_BurntToast.ps1
File metadata and controls
193 lines (157 loc) · 5.87 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# ===================================================================
# HydrationReminder.ps1
# Hydration reminder using BurntToast portable, self-contained build
#
# Images are embedded as Base64 by Pack-Assets.ps1 (run that once first).
# After packing, this single file and the .exe you compile from it
# needs nothing else sitting next to it, and works under any Windows
# username since paths are resolved from %USERPROFILE%/Desktop/HydrationReminder.
# ===================================================================
# ----- EMBEDDED ASSETS -----
# Do not hand-edit these two lines Pack-Assets.ps1 fills them in.
$LogoBase64 = "" # <-- PACK:LOGO -->
$HeroBase64 = "" # <-- PACK:HERO -->
# ----- CUSTOMIZATION -----
$DataDir = Join-Path $env:USERPROFILE "Desktop\HydrationReminder"
$Config = [ordered]@{
Title = "Hydration Break"
Subtitle = "Time to drink water"
Sound = "Reminder" # Options: Default, Reminder, SMS, Alarm, Alarm2, etc.
UseSnoozeDismiss = $true
UseUrgent = $true # Set true only if you want it to break through Focus Assist / Do Not Disturb
ExpireAfterHours = 4 # Notification disappears from Action Center after this many hours
# Quiet hours. Example: do not notify from 10 PM to 7 AM.
RespectQuietHours = $true
QuietStartHour = 22
QuietEndHour = 7
DataDir = $DataDir
# Log file
LogPath = Join-Path $DataDir "drink-water-log.txt"
}
$Messages = @(
"Take a few sips now. Your body will thank you.",
"Small break: water first, then back to work.",
"Hydrate. Stretch your shoulders. Relax your eyes.",
"You have been focused for a while. Drink some water.",
"Quick reset: water, posture, deep breath.",
"Your future self wants you hydrated.",
"Drink water before your brain starts negotiating with coffee.",
"Two minutes: water + stretch + blink slowly."
)
# -------- HELPER FUNCTIONS --------
function Write-ReminderLog {
param([string]$Message)
$folder = Split-Path $Config.LogPath -Parent
if (-not (Test-Path $folder)) {
New-Item -ItemType Directory -Path $folder -Force | Out-Null
}
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $Config.LogPath -Value "[$timestamp] $Message"
}
function Test-IsQuietHour {
param(
[int]$StartHour,
[int]$EndHour
)
$currentHour = (Get-Date).Hour
if ($StartHour -eq $EndHour) {
return $false
}
if ($StartHour -lt $EndHour) {
return ($currentHour -ge $StartHour -and $currentHour -lt $EndHour)
}
return ($currentHour -ge $StartHour -or $currentHour -lt $EndHour)
}
function Initialize-EmbeddedAsset {
# Decodes embedded Base64 image to disk once, then reuses the
# cached copy on every later run. Returns $null if nothing was baked in,
# so the notification just skips that image same as leaving it blank.
param(
[string]$Base64,
[string]$FileName
)
if ([string]::IsNullOrWhiteSpace($Base64)) {
return $null
}
$buildDir = Join-Path -Path $Config.DataDir -ChildPath "build"
$outPath = Join-Path -Path $buildDir -ChildPath $FileName
if (-not (Test-Path -Path $outPath)) {
if (-not (Test-Path -Path $buildDir)) {
New-Item -ItemType Directory -Path $buildDir -Force | Out-Null
}
$bytes = [Convert]::FromBase64String($Base64)
[IO.File]::WriteAllBytes($outPath, $bytes)
}
return $outPath
}
# -------- QUIET HOURS CHECK --------
if ($Config.RespectQuietHours) {
if (Test-IsQuietHour -StartHour $Config.QuietStartHour -EndHour $Config.QuietEndHour) {
Write-ReminderLog "Skipped because quiet hours are active."
exit 0
}
}
# -------- LOAD BURNTTOAST --------
try {
Import-Module BurntToast -ErrorAction Stop
}
catch {
Write-ReminderLog "Failed: BurntToast module is not installed or could not be loaded. Error: $($_.Exception.Message)"
exit 1
}
# -------- EXTRACT EMBEDDED IMAGES --------
$Config.LogoPath = Initialize-EmbeddedAsset -Base64 $LogoBase64 -FileName "water-logo.png"
$Config.HeroImagePath = Initialize-EmbeddedAsset -Base64 $HeroBase64 -FileName "water-banner.png"
# -------- BUILD NOTIFICATION --------
$randomMessage = Get-Random -InputObject $Messages
$timeNow = Get-Date -Format "h:mm tt"
# Text shown in notification
$text1 = New-BTText -Text $Config.Title
$text2 = New-BTText -Text $randomMessage
$text3 = New-BTText -Text "Reminder time: $timeNow"
# Optional logo image
$logo = $null
if ($Config.LogoPath -and (Test-Path $Config.LogoPath)) {
$logo = New-BTImage -Source $Config.LogoPath -AppLogoOverride
}
# Optional banner image
$hero = $null
if ($Config.HeroImagePath -and (Test-Path $Config.HeroImagePath)) {
$hero = New-BTImage -Source $Config.HeroImagePath -HeroImage
}
# Build visual layout
$bindingParams = @{
Children = @($text1, $text2, $text3)
}
if ($logo) {
$bindingParams.AppLogoOverride = $logo
}
if ($hero) {
$bindingParams.HeroImage = $hero
}
$binding = New-BTBinding @bindingParams
$visual = New-BTVisual -BindingGeneric $binding
# Add Snooze and Dismiss buttons
# These buttons are important for reminder-style behavior
$actions = New-BTAction -SnoozeAndDismiss
# Reminder sound
$audio = New-BTAudio -Source "ms-winsoundevent:Notification.Reminder"
# This is the important part:
# Scenario Reminder tells Windows to treat this like a reminder, not a normal toast.
$content = New-BTContent `
-Visual $visual `
-Actions $actions `
-Audio $audio `
-Scenario Reminder
# -------- SHOW NOTIFICATION --------
try {
Submit-BTNotification `
-Content $content `
-UniqueIdentifier "DrinkWaterReminder" `
-ExpirationTime (Get-Date).AddHours($Config.ExpireAfterHours)
Write-ReminderLog "Notification shown as reminder: $randomMessage"
}
catch {
Write-ReminderLog "Failed to show notification. Error: $($_.Exception.Message)"
exit 1
}