Skip to content

Commit 2723aa5

Browse files
authored
Merge pull request #150 from kujirahand/codex/update-send-notification-win
[WIP] Update send_notification_win for safer execution
2 parents b76e172 + ee22450 commit 2723aa5

1 file changed

Lines changed: 41 additions & 28 deletions

File tree

TkEasyGUI/dialogs.py

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
import subprocess
77
import sys
8-
import tempfile
98
import tkinter
109
from datetime import datetime, timedelta
1110
from re import Pattern
@@ -1346,10 +1345,13 @@ def send_notification_mac(message: str, title: str = "") -> bool:
13461345

13471346

13481347
def send_notification_win(message: str, title: str = "") -> bool:
1349-
"""Send Notification on Windows using PowerShell"""
1348+
"""Send Notification on Windows using PowerShell without temp files"""
13501349
# get powershell path
1350+
system_root = os.environ.get("SystemRoot")
1351+
if not system_root:
1352+
return False
13511353
powershell_path = os.path.join(
1352-
os.environ["SystemRoot"],
1354+
system_root,
13531355
"System32",
13541356
"WindowsPowerShell",
13551357
"v1.0",
@@ -1366,44 +1368,55 @@ def to_base64(s: str) -> str:
13661368

13671369
encoded_title = to_base64(title)
13681370
encoded_message = to_base64(message)
1369-
1370-
# PowerShell Script using Base64
13711371
script_content = r"""
1372-
param($encodedTitle, $encodedMessage, $appPath)
1372+
$ErrorActionPreference = "Stop"
1373+
1374+
$encodedTitle = [System.Environment]::GetEnvironmentVariable("TKEASYGUI_NOTIFY_TITLE_B64")
1375+
$encodedMessage = [System.Environment]::GetEnvironmentVariable("TKEASYGUI_NOTIFY_MESSAGE_B64")
1376+
$appId = [System.Environment]::GetEnvironmentVariable("TKEASYGUI_NOTIFY_APP_ID")
1377+
1378+
if ([string]::IsNullOrEmpty($encodedTitle) -or [string]::IsNullOrEmpty($encodedMessage) -or [string]::IsNullOrEmpty($appId)) {
1379+
exit 1
1380+
}
1381+
13731382
$decodedTitle = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodedTitle))
13741383
$decodedMessage = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodedMessage))
1375-
$bodyText = "$decodedTitle`n$decodedMessage"
1376-
1377-
$ToastText01 = [Windows.UI.Notifications.ToastTemplateType, Windows.UI.Notifications, ContentType = WindowsRuntime]::ToastText01
1378-
$TemplateContent = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::GetTemplateContent($ToastText01)
1379-
$TemplateContent.SelectSingleNode('//text[@id="1"]').InnerText = $bodyText
1380-
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($appPath).Show($TemplateContent)
1381-
"""
1382-
1383-
# generate temp script file
1384-
with tempfile.NamedTemporaryFile(
1385-
"w", suffix=".ps1", delete=False, encoding="utf-8"
1386-
) as script_file:
1387-
script_file.write(script_content)
1388-
script_path = script_file.name
1384+
1385+
$toastType = [Windows.UI.Notifications.ToastTemplateType, Windows.UI.Notifications, ContentType = WindowsRuntime]::ToastText02
1386+
$templateContent = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::GetTemplateContent($toastType)
1387+
$textNodes = $templateContent.GetElementsByTagName("text")
1388+
$textNodes.Item(0).AppendChild($templateContent.CreateTextNode($decodedTitle)) | Out-Null
1389+
$textNodes.Item(1).AppendChild($templateContent.CreateTextNode($decodedMessage)) | Out-Null
1390+
$toast = [Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime]::new($templateContent)
1391+
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($appId).Show($toast)
1392+
""".strip()
1393+
1394+
ps_env = os.environ.copy()
1395+
ps_env["TKEASYGUI_NOTIFY_TITLE_B64"] = encoded_title
1396+
ps_env["TKEASYGUI_NOTIFY_MESSAGE_B64"] = encoded_message
1397+
ps_env["TKEASYGUI_NOTIFY_APP_ID"] = sys.executable
1398+
13891399
try:
13901400
subprocess.run(
13911401
[
13921402
powershell_path,
1403+
"-NoLogo",
1404+
"-NoProfile",
1405+
"-NonInteractive",
13931406
"-ExecutionPolicy",
13941407
"Bypass",
1395-
"-File",
1396-
script_path,
1397-
encoded_title,
1398-
encoded_message,
1399-
sys.executable,
1408+
"-Command",
1409+
"-",
14001410
],
1411+
input=script_content,
1412+
text=True,
1413+
encoding="utf-8",
1414+
env=ps_env,
14011415
check=True,
14021416
)
14031417
return True
1404-
finally:
1405-
# 実行後に一時ファイルを削除
1406-
os.remove(script_path)
1418+
except (subprocess.SubprocessError, OSError):
1419+
return False
14071420

14081421

14091422
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)