-
Notifications
You must be signed in to change notification settings - Fork 28
/
Example_JsRT_Toast.ahk
83 lines (78 loc) · 3.17 KB
/
Example_JsRT_Toast.ahk
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
#Include JsRT.ahk
; Unlike TrayTip, this API does not require a tray icon:
#NoTrayIcon
; Toast notifications from desktop apps can only use local image files.
if !FileExist(A_ScriptDir "\sample.png")
Download "https://autohotkey.com/boards/styles/simplicity/theme/images/announce_unread.png"
, A_ScriptDir "\sample.png"
; The templates are described here:
; http://msdn.com/library/windows/apps/windows.ui.notifications.toasttemplatetype.aspx
toast_template := "toastImageAndText02"
; Image path/URL must be absolute, not relative.
toast_image := A_ScriptDir "\sample.png"
; Text is an array because some templates have multiple text elements.
toast_text := ["Hello, world!", "This is the sub-text."]
; For Windows 10.0.16299 (and possibly earlier or later versions), the AppID
; must identify an app which has a shortcut on the Start screen, otherwise
; the notification won't display. AppIDs for desktop apps seem to be the
; path of the executable, with system/known folders replaced with GUIDs.
; If this doesn't work, the Get-StartApps powershell command can be used to
; get a list of AppIDs on the system.
; This assumes AutoHotkey is installed in the default location:
toast_appid := (A_Is64bitOS ? "{6D809377-6AF0-444b-8957-A3773F02200E}"
: "{905e63b6-c1bf-494e-b29c-65b732d3d21a}")
. "\AutoHotkey\AutoHotkey.exe"
; Only the Edge version of JsRT supports WinRT.
js := (JsRT.Edge)()
js.AddObject "yesno", (s) => MsgBox(s,, "y/n") = "yes"
; Enable use of WinRT. "Windows.UI" or "Windows" would also work.
js.ProjectWinRTNamespace("Windows.UI.Notifications")
code := '
(
function toast(template, image, text, app) {
// Alias for convenience.
var N = Windows.UI.Notifications;
// Get the template XML as an XmlDocument.
var toastXml = N.ToastNotificationManager
.getTemplateContent(N.ToastTemplateType[template]);
// Insert our content.
var i = 0;
for (let el of toastXml.getElementsByTagName("text")) {
if (typeof text == 'string') {
el.innerText = text;
break;
}
el.innerText = text(++i);
}
toastXml.getElementsByTagName("image")[0]
.setAttribute("src", image);
// Show the notification.
var toastNotifier = N.ToastNotificationManager
.createToastNotifier(app || "AutoHotkey");
var notification = new N.ToastNotification(toastXml);
toastNotifier.show(notification);
// Unlike TrayTip, this API lets us hide the notification:
if (yesno("Hide the notification?")) {
toastNotifier.hide(notification);
}
}
function clearAllToasts(app) {
Windows.UI.Notifications.ToastNotificationManager.history.clear(app);
}
)'
try {
; Define the toast function.
js.Exec(code)
; Clear all toasts.
js.clearAllToasts(toast_appid)
; Show a toast notification.
js.toast(toast_template, toast_image, toast_text, toast_appid)
}
catch as ex {
try errmsg := ex.stack
if !errmsg
errmsg := "Error: " ex.message
MsgBox errmsg
}
; Note: If the notification wasn't hidden, it will remain after we exit.
ExitApp