Skip to content

Commit 4309d85

Browse files
Codexkujirahand
andauthored
Improve Windows notification invocation
Co-authored-by: kujirahand <[email protected]>
1 parent a535602 commit 4309d85

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

TkEasyGUI/dialogs.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,10 +1339,13 @@ def send_notification_mac(message: str, title: str = "") -> bool:
13391339

13401340

13411341
def send_notification_win(message: str, title: str = "") -> bool:
1342-
"""Send Notification on Windows using PowerShell"""
1342+
"""Send Notification on Windows using PowerShell without temp files"""
13431343
# get powershell path
1344+
system_root = os.environ.get("SystemRoot")
1345+
if not system_root:
1346+
return False
13441347
powershell_path = os.path.join(
1345-
os.environ["SystemRoot"],
1348+
system_root,
13461349
"System32",
13471350
"WindowsPowerShell",
13481351
"v1.0",
@@ -1359,10 +1362,14 @@ def to_base64(s: str) -> str:
13591362

13601363
encoded_title = to_base64(title)
13611364
encoded_message = to_base64(message)
1365+
app_id = sys.executable.replace("\\", "\\\\")
1366+
1367+
# PowerShell Script using Base64 (embedded and executed via -EncodedCommand)
1368+
script_content = rf"""
1369+
$encodedTitle = "{encoded_title}"
1370+
$encodedMessage = "{encoded_message}"
1371+
$appPath = "{app_id}"
13621372
1363-
# PowerShell Script using Base64
1364-
script_content = r"""
1365-
param($encodedTitle, $encodedMessage, $appPath)
13661373
$decodedTitle = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodedTitle))
13671374
$decodedMessage = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($encodedMessage))
13681375
$bodyText = "$decodedTitle`n$decodedMessage"
@@ -1371,32 +1378,27 @@ def to_base64(s: str) -> str:
13711378
$TemplateContent = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]::GetTemplateContent($ToastText01)
13721379
$TemplateContent.SelectSingleNode('//text[@id="1"]').InnerText = $bodyText
13731380
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($appPath).Show($TemplateContent)
1374-
"""
1375-
1376-
# generate temp script file
1377-
with tempfile.NamedTemporaryFile(
1378-
"w", suffix=".ps1", delete=False, encoding="utf-8"
1379-
) as script_file:
1380-
script_file.write(script_content)
1381-
script_path = script_file.name
1381+
""".strip()
1382+
1383+
encoded_script = base64.b64encode(script_content.encode("utf-16le")).decode("ascii")
1384+
13821385
try:
13831386
subprocess.run(
13841387
[
13851388
powershell_path,
1389+
"-NoLogo",
1390+
"-NoProfile",
1391+
"-NonInteractive",
13861392
"-ExecutionPolicy",
13871393
"Bypass",
1388-
"-File",
1389-
script_path,
1390-
encoded_title,
1391-
encoded_message,
1392-
sys.executable,
1394+
"-EncodedCommand",
1395+
encoded_script,
13931396
],
13941397
check=True,
13951398
)
13961399
return True
1397-
finally:
1398-
# 実行後に一時ファイルを削除
1399-
os.remove(script_path)
1400+
except (subprocess.SubprocessError, OSError):
1401+
return False
14001402

14011403

14021404
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)