-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertDateAndInitials.ahk
More file actions
423 lines (352 loc) · 13.6 KB
/
InsertDateAndInitials.ahk
File metadata and controls
423 lines (352 loc) · 13.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#Requires AutoHotkey v2.0
#SingleInstance Force
#Warn
SetWorkingDir(A_ScriptDir)
; ===== Version Information =============================================================================
compiledGitTag := "v2.1.2"
; ===== INI & Settings ==================================================================================
IniFileName := "Settings_InsertDateAndInitials.ini"
if !FileExist(IniFileName) {
IniWrite("", IniFileName, "Settings", "Initials")
IniWrite("MM/dd/yy ddd", IniFileName, "Settings", "DateTimeFormat")
IniWrite("()", IniFileName, "Settings", "InitialsList") ; default list
}
; Active initials and format
vInitials := IniRead(IniFileName, "Settings", "Initials", "()")
vDateTimeFormat := IniRead(IniFileName, "Settings", "DateTimeFormat", "MM/dd/yy ddd")
; Load list (pipe-delimited), ensure active is present
InitialsList := LoadInitialsList()
if !InitialsList.Has(vInitials)
InitialsList.Push(vInitials), SaveInitialsList()
; ===== Tray Menu =======================================================================================
A_TrayMenu.Delete()
A_TrayMenu.Add("Show GUI", GoToShowGUI)
; Build dynamic submenu for initials
InitialsMenuLabel := "Initials"
RebuildInitialsTrayMenu()
A_TrayMenu.Add("Exit", TrayExit)
A_TrayMenu.Default := "Show GUI"
; ===== GUI Creation ====================================================================================
Main := Gui()
Main.MarginX := 10, Main.MarginY := 10
Main.SetFont("s10 cWhite", "Arial")
Main.BackColor := 0x1E1E1E
Main.Title := "Date and Initials Helper"
Main.AddText("w300 Center", "________________________________________")
Main.AddText("w300 Center", "Currently Using:")
PreviewCtrl := Main.AddText("w300 Center", "")
Main.AddText("w300 Center", "________________________________________")
Main.AddText("w300 Center", "--- General Setup / Hide to Tray ---")
btn := Main.AddButton("w300", "Change Initials"), btn.OnEvent("Click", ChangeInitials)
btn := Main.AddButton("w300", "Edit Date Format"), btn.OnEvent("Click", EditDateTimeFormat)
btn := Main.AddButton("w300", "Hide to Tray"), btn.OnEvent("Click", HideToTray)
Main.AddText("w300", "")
Main.AddText("w300 Center", "--- Settings / Help ---")
btn := Main.AddButton("w300", "Check for Updates"), btn.OnEvent("Click", CheckforUpdates)
btn := Main.AddButton("w300", "Reset to Default"), btn.OnEvent("Click", ConfirmResetToDefault)
btn := Main.AddButton("w300", "Help"), btn.OnEvent("Click", ShowHelp)
Main.AddText("w300 Right", compiledGitTag)
Main.OnEvent("Close", ConfirmExitApp)
UpdatePreview()
Main.Show()
; ===== Hotkey ==========================================================================================
Hotkey("^d", SendDateAndInitials)
SendDateAndInitials(*) {
global vDateTimeFormat, vInitials
dt := FormatTime(, vDateTimeFormat) ; (Time, Format) with blank time = now
SendText dt . " " . vInitials . ": "
}
; ===== Handlers / Functions ============================================================================
ShowHelp(*) {
MsgBox "Usage Instructions:`n`n1) Change Initials to manage or switch saved initials.`n2) Use the hotkey Ctrl+D to insert the current date/time and active initials."
}
CheckforUpdates(*) {
global compiledGitTag
latestGitTag := GetLatestGithubTag("RobertCSternberg", "InsertDateandInitials")
if (latestGitTag != "" && latestGitTag != compiledGitTag) {
MsgBox "New version (" latestGitTag ") available! Visit https://github.com/RobertCSternberg/InsertDateandInitials/ for the latest release."
} else if (latestGitTag = "") {
MsgBox "Unable to fetch the latest version from GitHub."
} else {
MsgBox "You are using the latest version: " compiledGitTag "."
}
}
HideToTray(*) {
global Main
Main.Hide()
}
ConfirmExitApp(*) {
txt := "
(
Are you sure you want to quit the application?
Instead consider hiding to the system tray. This can be done on the main menu with "Hide to Tray" and will allow you to continue using the hotkey Ctrl+D.
)"
if MsgBox(txt, "Quit?", "YesNo Icon?") = "Yes" {
ExitApp()
}
}
GoToShowGUI(*) {
global Main
Main.Show()
}
TrayExit(*) {
ExitApp()
}
; ===== Change Initials (new feature) ===================================================================
ChangeInitials(*) {
global vInitials, InitialsList
GuiCI := Gui("-SysMenu")
GuiCI.MarginX := 10, GuiCI.MarginY := 10
GuiCI.SetFont("s10 cWhite", "Arial")
GuiCI.BackColor := 0x1E1E1E
GuiCI.Title := "Change Initials"
GuiCI.AddText("w360", "Add a new initial, remove existing, or double-click to switch:")
NewEdit := GuiCI.AddEdit("w180 cBlack")
addBtn := GuiCI.AddButton("x+m w90", "Add")
remBtn := GuiCI.AddButton("x+m w120", "Remove Selected")
useBtn := GuiCI.AddButton("w120", "Use Selected")
closeBtn:= GuiCI.AddButton("x+m w90", "Close"), closeBtn.OnEvent("Click", (*) => GuiCI.Destroy())
LV := GuiCI.AddListView("w360 r10 Grid", ["Initials"])
LV.SetFont("cBlack")
LV.ModifyCol(1, 320)
FillInitialsLV(LV)
LV.OnEvent("DoubleClick", (*) => (
(init := GetLVSelected(LV)) ? (SetActiveInitials(init), GuiCI.Destroy()) : 0
))
addBtn.OnEvent("Click", (*) => (
val := Trim(NewEdit.Value)
, val := StrUpper(val)
, (val != "")
? (AddInitials(val), NewEdit.Value := "", FillInitialsLV(LV))
: 0
))
remBtn.OnEvent("Click", (*) => (
(init := GetLVSelected(LV))
? (RemoveInitials(init), FillInitialsLV(LV))
: MsgBox("Select an initial to remove.", "No selection", "Icon!")
))
useBtn.OnEvent("Click", (*) => (
(init := GetLVSelected(LV))
? (SetActiveInitials(init), GuiCI.Destroy())
: MsgBox("Select an initial to use.", "No selection", "Icon!")
))
GuiCI.Show()
}
GetLVSelected(lv) {
row := lv.GetNext()
return row ? lv.GetText(row, 1) : ""
}
FillInitialsLV(lv) {
lvCount := lv.GetCount()
while (lv.GetCount() > 0)
lv.Delete(1)
global InitialsList, vInitials
for _, init in InitialsList {
idx := lv.Add("", init)
if (init = vInitials)
lv.Modify(idx, "Select Focus")
}
}
AddInitials(init) {
global InitialsList
; avoid duplicates
for _, v in InitialsList
if (v = init)
return
InitialsList.Push(init)
SaveInitialsList()
RebuildInitialsTrayMenu()
}
RemoveInitials(init) {
global InitialsList, vInitials
idx := 0
for i, v in InitialsList
if (v = init) {
idx := i
break
}
if (idx) {
InitialsList.RemoveAt(idx)
; if we removed the active one, switch to first if any, else "()"
if (init = vInitials) {
newActive := InitialsList.Length ? InitialsList[1] : "()"
SetActiveInitials(newActive, false) ; false => don't rebuild list again (we will below)
} else {
SaveInitialsList()
}
RebuildInitialsTrayMenu()
}
}
SetActiveInitials(newInit, rebuild := true) {
global vInitials, IniFileName
vInitials := newInit
IniWrite(vInitials, IniFileName, "Settings", "Initials") ; keep legacy key for compatibility
UpdatePreview()
if rebuild
RebuildInitialsTrayMenu()
}
LoadInitialsList() {
global IniFileName
raw := IniRead(IniFileName, "Settings", "InitialsList", "")
list := []
if (raw != "") {
for _, seg in StrSplit(raw, "|") {
seg := Trim(seg)
if (seg != "")
list.Push(seg)
}
} else {
; fall back to single active initial if list missing
act := IniRead(IniFileName, "Settings", "Initials", "()")
list := [act]
IniWrite(act, IniFileName, "Settings", "InitialsList")
}
; provide handy .Has() for membership checks
list.DefineProp("Has", {Call:(this, x) => (IndexOf(this, x) != 0)})
return list
}
IndexOf(arr, val) {
for i, v in arr
if (v = val)
return i
return 0
}
SaveInitialsList() {
global IniFileName, InitialsList, vInitials
; ensure active is present
pres := false
for _, v in InitialsList {
if (v = vInitials) {
pres := true
break
}
}
if !pres
InitialsList.Push(vInitials)
IniWrite(StrJoin(InitialsList, "|"), IniFileName, "Settings", "InitialsList")
}
StrJoin(arr, delim := ",") {
out := ""
for i, v in arr
out .= (i>1 ? delim : "") . v
return out
}
RebuildInitialsTrayMenu() {
global InitialsMenuLabel, InitialsList, A_TrayMenu
; remove old submenu if it exists
try A_TrayMenu.Delete(InitialsMenuLabel)
catch as err {
; ignore if not present yet
}
sub := Menu()
; each initials entry points to one common handler
for _, init in InitialsList
sub.Add(init, SwitchInitialsFromTray)
sub.Add("Change Initials...", ChangeInitials)
A_TrayMenu.Add(InitialsMenuLabel, sub)
}
; Menu callback: first param is the clicked item's label (the initials)
SwitchInitialsFromTray(ItemName, ItemPos := "", MyMenu := "") {
if ItemName
SetActiveInitials(ItemName)
}
; ===== Date Format Picker (ListView tabs with examples) ===============================================
EditDateTimeFormat(*) {
global vDateTimeFormat, IniFileName
FormatPicker := Gui("-SysMenu")
FormatPicker.MarginX := 10, FormatPicker.MarginY := 10
FormatPicker.SetFont("s10 cWhite", "Consolas") ; white for labels; LV sets its own color
FormatPicker.BackColor := 0x1E1E1E
FormatPicker.Title := "Update Date Format"
FormatPicker.AddText("w360", "Example Date (for preview below): January 2, 2003 (Thu)")
useBtn := FormatPicker.AddButton("w120", "Use Selected")
cancelBtn := FormatPicker.AddButton("x+m w120", "Cancel")
cancelBtn.OnEvent("Click", (*) => FormatPicker.Destroy())
tab := FormatPicker.AddTab("vMyTab", ["No Weekday","Weekday Short","Weekday Long","Month Written","Full Dates"])
sampleDate := "20030102090000" ; Thu, Jan 2, 2003 @ 09:00
tabLVs := [] ; listviews per tab
tab.UseTab(1)
tabLVs.Push( MakeLV(FormatPicker, sampleDate, ["MM/yy","MM/dd","MM/yyyy","MM/dd/yy","MM/dd/yyyy","yyyy/MM/dd"]) )
tab.UseTab(2)
tabLVs.Push( MakeLV(FormatPicker, sampleDate, ["MM/dd ddd","MM/dd/yy ddd","MM/dd/yyyy ddd"]) )
tab.UseTab(3)
tabLVs.Push( MakeLV(FormatPicker, sampleDate, ["MM/dd dddd","MM/dd/yy dddd","MM/dd/yyyy dddd"]) )
tab.UseTab(4)
tabLVs.Push( MakeLV(FormatPicker, sampleDate, ["MMM dd.","MMM dd","MMMM dd","MMMM dd, yyyy"]) )
tab.UseTab(5)
tabLVs.Push( MakeLV(FormatPicker, sampleDate, ["dddd, MMMM dd, yyyy"]) )
tab.UseTab()
apply := (fmt) => (
vDateTimeFormat := fmt
, IniWrite(vDateTimeFormat, IniFileName, "Settings", "DateTimeFormat")
, UpdatePreview()
, FormatPicker.Destroy()
)
GetSelectedFmt := () => (
idx := tab.Value
, lv := tabLVs[idx]
, row := lv.GetNext()
, (row ? lv.GetText(row, 2) : "")
)
for _, lv in tabLVs
lv.OnEvent("DoubleClick", (*) => (
(fmt := GetSelectedFmt()) ? apply(fmt) : 0
))
useBtn.OnEvent("Click", (*) => (
(fmt := GetSelectedFmt()) ? apply(fmt) : MsgBox("Please select a format first.", "No selection", "Icon!")
))
FormatPicker.Show()
}
; helper to create & fill a LV with examples (black text on white)
MakeLV(containerGui, sampleDate, fmts) {
lv := containerGui.AddListView("w360 r10 Grid", ["Example","Format"])
lv.SetFont("cBlack")
lv.ModifyCol(1, 220), lv.ModifyCol(2, 120)
for _, fmt in fmts {
ex := FormatTime(sampleDate, fmt) ; (Time, Format)
lv.Add("", ex, fmt)
}
return lv
}
; ===== Reset & Preview =================================================================================
ConfirmResetToDefault(*) {
if MsgBox("Are you sure you want to reset the settings to default? This action cannot be undone.", "Reset to Default", "YesNo Icon!") = "Yes" {
ResettoDefault()
}
}
ResettoDefault() {
global vDateTimeFormat, vInitials, IniFileName, InitialsList
vDateTimeFormat := "MM/dd/yy ddd"
IniWrite(vDateTimeFormat, IniFileName, "Settings", "DateTimeFormat")
; reset initials to default "()"
vInitials := "()"
InitialsList := ["()"]
SaveInitialsList()
IniWrite(vInitials, IniFileName, "Settings", "Initials")
RebuildInitialsTrayMenu()
UpdatePreview()
}
UpdatePreview() {
global PreviewCtrl, vDateTimeFormat, vInitials
dt := FormatTime(, vDateTimeFormat) ; (Time, Format)
PreviewCtrl.Text := dt . " " . vInitials . ": "
}
; ===== GitHub Tag Helper ==============================================================================
GetLatestGithubTag(username, repo) {
try {
url := "https://api.github.com/repos/" username "/" repo "/releases/latest"
whr := ComObject("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", url, false)
whr.SetRequestHeader("User-Agent", "AHK Script")
whr.Send()
if whr.Status != 200
return ""
response := whr.ResponseText
tagPattern := '"tag_name":\s*"(v?\d+\.\d+\.\d+)"'
if RegExMatch(response, tagPattern, &m)
return m[1]
return ""
} catch {
return ""
}
}