This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstaller.ahk
1943 lines (1783 loc) · 62.5 KB
/
Installer.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#NoEnv
#NoTrayIcon
; #Warn
#SingleInstance Off
try Menu Tray, Icon, appwiz.cpl, -1500if !A_IsAdmin && !%False%
{
if A_OSVersion not in WIN_2003,WIN_XP,WIN_2000
{
Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%",, UseErrorLevel
if !ErrorLevel
ExitApp
}
MsgBox 0x31, AutoHotkey Setup,
(LTrim Join`s
Setup is running as a limited user. If you continue, some problems
are likely to occur. It is strongly recommended that you run Setup
as an administrator.`n
`n
To continue anyway, click OK. Otherwise click Cancel.
)
IfMsgBox Cancel
ExitApp
}
SourceDir := A_ScriptDirSilentMode := false
SilentErrors := 0
AutoRestart := false
ProductName := "AutoHotkey"
ProductVersion := A_AhkVersion
ProductPublisher := "Lexikos"
ProductWebsite := "https://autohotkey.com/"
EnvGet ProgramW6432, ProgramW6432
DefaultPath := (ProgramW6432 ? ProgramW6432 : A_ProgramFiles) "\AutoHotkey"
DefaultType := A_Is64bitOS ? "x64" : "Unicode"
DefaultStartMenu := "AutoHotkey"
DefaultCompiler := true
DefaultDragDrop := true
DefaultToUTF8 := false
DefaultIsHostApp := false
DefaultUIAccess := false
AutoHotkeyKey := "SOFTWARE\AutoHotkey"
UninstallKey := "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AutoHotkey"
FileTypeKey := "AutoHotkeyScript"
RegRead UACIsEnabled, HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System, EnableLUA
Menu Tray, MainWindow ; Enable debugging setup.exe.
if 1 = /exec ; For internal use
{
HandleExec(1)
ExitApp
}
DetermineVersion()
ConfigureMode := DefaultPath = A_ScriptDir
Loop %0%
if %A_Index% = /S
SilentMode := true
else if %A_Index% = /R
AutoRestart := true
else if %A_Index% = /U32
DefaultType = Unicode
else if %A_Index% in /U64,/x64
DefaultType = x64
else if %A_Index% in /A32,/ANSI
DefaultType = ANSI
else if %A_Index% in /uiAccess,/uiAccess=0,/uiAccess=1
DefaultUIAccess := SubStr(%A_Index%, 10) != "=0"
else if %A_Index% in /IsHostApp,/IsHostApp=0,/IsHostApp=1
DefaultIsHostApp := SubStr(%A_Index%, 11) != "=0"
else if InStr(%A_Index%, "/D=") = 1 {
if !RegExMatch(DllCall("GetCommandLine", "str"), "(?<!"")/D=\K[^""]*?(?=$|[ `t]+/)", DefaultPath)
DefaultPath := SubStr(%A_Index%, 4)
Loop %DefaultPath%, 2 ; Resolve relative path.
DefaultPath := A_LoopFileLongPath
SlashD := true
}
else if (%A_Index% = "/?") {
ViewHelp("/docs/Scripts.htm#install")
ExitApp
}
else if (%A_Index% = "/Uninstall") {
SilentMode := true
Uninstall()
ExitApp
}
else if (%A_Index% = "/E") {
Extract(SlashD ? DefaultPath : "")
ExitApp
}
else if (SubStr(%A_Index%,1,5) = "/Test")
TestMode := SubStr(%A_Index%,6)
if SilentMode {
QuickInstall()
ExitApp % SilentErrors
}
if WinExist("AutoHotkey Setup ahk_class AutoHotkeyGUI") {
MsgBox 0x30, AutoHotkey Setup, AutoHotkey Setup is already running!
WinActivate
ExitApp
}
OnExit GuiClose
Gui Margin, 0, 0
Gui +LastFound
try { ; Hide window title.
DllCall("UxTheme\SetWindowThemeAttribute", "ptr", WinExist()
, "int", 1, "int64*", (3<<32)|3, "int", 8)
}
OnMessage(0x100, "gui_KeyDown", 2)
try Gui Add, ActiveX, vwb w600 h400 hwndhwb, Shell.Explorer
try {
if !wb
throw Exception("Failed to create IE control")
if GetKeyState("Shift") || GetKeyState("Ctrl")
throw 1
SetWBClientSite()
InitUI()
}
catch excpt {
if ConfigureMode {
MsgBox 0x10, AutoHotkey Setup, Setup failed to initialize its user interface and will now exit.
ExitApp
}
message := IsObject(excpt)
? "Setup encountered an error.`n"
. " Specifically: " excpt.Message
: "Setup is in troubleshooting mode (you're holding Ctrl or Shift)."
type := DefaultType="ANSI" ? "ANSI 32-bit" : "Unicode " (DefaultType="x64"?"64":"32") "-bit"
MsgBox 0x33, AutoHotkey Setup,
(
%message%
Do you want to install with default options?
%ProductName% v%ProductVersion% (%type%)
%DefaultPath%
Click Yes to install.
Click No to copy setup files to a directory of your choosing.
Click Cancel to exit.
)
IfMsgBox Yes
{
QuickInstall()
MsgBox 0, AutoHotkey Setup, Installation complete.
}
else IfMsgBox No
Extract()
ExitApp
}
Gui Show,, AutoHotkey Setup
Gui +OwnDialogs
WinWaitClose ; Let +OwnDialogs apply to threadless callbacks.
return
GuiEscape:
Gui +OwnDialogs
MsgBox 0x2034, AutoHotkey Setup, Are you sure you want to exit setup?
IfMsgBox No
return
GuiClose:
Gui Destroy
OnExit
ExitApp
DetermineVersion() {
global
local url, v
; This first section has two purposes:
; 1) Determine the location of any current installation.
; 2) Determine which view of the registry it was installed into
; (only applicable if the OS is 64-bit).
CurrentRegView := ""
Loop % (A_Is64bitOS ? 2 : 1) {
SetRegView % 32*A_Index
RegRead CurrentPath, HKLM, %AutoHotkeyKey%, InstallDir
if !ErrorLevel {
CurrentRegView := A_RegView
break
}
}
if ErrorLevel {
CurrentName := ""
CurrentVersion := ""
CurrentType := ""
CurrentPath := ""
CurrentStartMenu := ""
return
}
RegRead CurrentVersion, HKLM, %AutoHotkeyKey%, Version
RegRead CurrentStartMenu, HKLM, %AutoHotkeyKey%, StartMenuFolder
RegRead url, HKLM, %UninstallKey%, URLInfoAbout
; Identify by URL since uninstaller display name is the same:
if (url = "http://www.autohotkey.net/~Lexikos/AutoHotkey_L/"
|| url = "http://l.autohotkey.net/")
CurrentName := "AutoHotkey_L"
else
CurrentName := "AutoHotkey"
; Identify which build is installed/set as default:
FileAppend ExitApp `% (A_IsUnicode=1) << 8 | (A_PtrSize=8) << 9, %A_Temp%\VersionTest.ahk
RunWait %CurrentPath%\AutoHotkey.exe "%A_Temp%\VersionTest.ahk",, UseErrorLevel
if ErrorLevel = 0x300
CurrentType := "x64"
else if ErrorLevel = 0x100
CurrentType := "Unicode"
else if ErrorLevel = 0
CurrentType := "ANSI"
else
CurrentType := ""
FileDelete %A_Temp%\VersionTest.ahk
; Set some default parameter based on current installation:
if CurrentType
DefaultType := CurrentType
DefaultPath := CurrentPath
DefaultStartMenu := CurrentStartMenu
DefaultCompiler := FileExist(CurrentPath "\Compiler\Ahk2Exe.exe") != ""
RegRead v, HKCR, %FileTypeKey%\ShellEx\DropHandler
DefaultDragDrop := ErrorLevel = 0
RegRead v, HKCR, Applications\AutoHotkey.exe, IsHostApp
DefaultIsHostApp := !ErrorLevel
RegRead v, HKCR, %FileTypeKey%\Shell\uiAccess\Command
DefaultUIAccess := !ErrorLevel && UACIsEnabled
RegRead v, HKCR, %FileTypeKey%\Shell\Open\Command
DefaultToUTF8 := InStr(v, " /CP65001 ") != 0
}
InitUI() {
local w gosub DefineUI
wb.Silent := true
wb.Navigate("about:<!DOCTYPE HTML><meta http-equiv='x-ua-compatible' content='IE=Edge'>")
while wb.ReadyState != 4 {
Sleep 10
if (A_TickCount-initTime > 2000)
throw 1
}
wb.Document.open()
wb.Document.write(html)
wb.Document.close() w := wb.Document.parentWindow
if !w || !w.initOptions
throw 1
w.AHK := Func("JS_AHK")
if (!CurrentType && A_ScriptDir != DefaultPath)
CurrentName := "" ; Avoid showing the Reinstall option since we don't know which version it was.
w.initOptions(CurrentName, CurrentVersion, CurrentType
, ProductVersion, DefaultPath, DefaultStartMenu
, DefaultType, A_Is64bitOS = 1)
w.configureMode := ConfigureMode
w.document.body.className := ConfigureMode ? "config-mode" : ""
if ConfigureMode {
w.installdir.disabled := true
w.installdir_browse.disabled := true
w.nav_install.innerText := "apply"
w.install_button.innerText := "Apply"
w.opt1.onclick := ""
w.opt1.removeAttribute("href")
w.opt1.firstChild.innerText := "Checking for updates..."
}
w.installcompiler.checked := DefaultCompiler
w.enabledragdrop.checked := DefaultDragDrop
w.separatebuttons.checked := DefaultIsHostApp
w.enableuiaccess.checked := DefaultUIAccess && IsTrustedLocation(DefaultPath)
; w.defaulttoutf8.checked := DefaultToUTF8
if !A_Is64bitOS
w.it_x64.style.display := "None"
if A_OSVersion in WIN_2000,WIN_2003,WIN_XP,WIN_VISTA ; i.e. not WIN_7, WIN_8 or a future OS.
w.separatebuttons.parentNode.style.display := "none"
if !UACIsEnabled
w.enableuiaccess.parentNode.style.display := "none"
else {
w.enableuiaccess.onchange := Func("enableuiaccess_onchange")
w.installdir.onchange := Func("installdir_onchange")
}
w.switchPage("start")
w.document.body.focus()
; Scale UI by screen DPI. My testing showed that Vista with IE7 or IE9
; did not scale by default, but Win8.1 with IE10 did. The scaling being
; done by the control itself = deviceDPI / logicalDPI.
logicalDPI := w.screen.logicalXDPI, deviceDPI := w.screen.deviceXDPI
if (A_ScreenDPI != 96)
w.document.body.style.zoom := A_ScreenDPI/96 * (logicalDPI/deviceDPI)
if ConfigureMode
CheckForUpdates()
}
CheckForUpdates() {
local w := getWindow(), latestVersion := ""
try {
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", "https://autohotkey.com/download/1.1/version.txt", true)
whr.Send()
whr.WaitForResponse()
latestVersion := whr.responseText
}
if RegExMatch(latestVersion, "^(\d+\.){3}\d+") {
if (latestVersion = ProductVersion)
w.opt1.firstChild.innerText := "Reinstall (download required)"
else
w.opt1.firstChild.innerText := "Download v" latestVersion
w.opt1.href := "#"
w.opt1.onclick := Func("DownloadAHK")
} else
w.opt1.innerText := "An error occurred while checking for updates."
}
/* Fix keyboard shortcuts in WebBrowser control.
*/
gui_KeyDown(wParam, lParam, nMsg, hWnd) {
global wb
if (Chr(wParam) ~= "[A-Z]" || wParam = 0x74) ; Disable Ctrl+O/L/F/N and F5.
return
Gui +OwnDialogs ; For threadless callbacks which interrupt this.
pipa := ComObjQuery(wb, "{00000117-0000-0000-C000-000000000046}")
VarSetCapacity(kMsg, 48), NumPut(A_GuiY, NumPut(A_GuiX
, NumPut(A_EventInfo, NumPut(lParam, NumPut(wParam
, NumPut(nMsg, NumPut(hWnd, kMsg)))), "uint"), "int"), "int")
Loop 2
r := DllCall(NumGet(NumGet(1*pipa)+5*A_PtrSize), "ptr", pipa, "ptr", &kMsg)
; Loop to work around an odd tabbing issue (it's as if there
; is a non-existent element at the end of the tab order).
until wParam != 9 || wb.Document.activeElement != ""
ObjRelease(pipa)
if r = 0 ; S_OK: the message was translated to an accelerator.
return 0
}
/* javascript:AHK('Func') --> Func()
*/
JS_AHK(func, prms*) {
global wb
; Stop navigation prior to calling the function, in case it uses Exit.
wb.Stop(), %func%(prms*)
}
/* Complex workaround to override "Active scripting" setting
* and ensure scripts can run within the WebBrowser control.
*/
global WBClientSite
SetWBClientSite()
{
interfaces := {
(Join,
IOleClientSite: [0,3,1,0,1,0]
IServiceProvider: [3]
IInternetSecurityManager: [1,1,3,4,8,7,3,3]
)}
unkQI := RegisterCallback("WBClientSite_QI", "Fast")
unkAddRef := RegisterCallback("WBClientSite_AddRef", "Fast")
unkRelease := RegisterCallback("WBClientSite_Release", "Fast")
WBClientSite := {_buffers: bufs := {}}, bufn := 0,
for name, prms in interfaces
{
bufn += 1
bufs.SetCapacity(bufn, (4 + prms.MaxIndex()) * A_PtrSize)
buf := bufs.GetAddress(bufn)
NumPut(unkQI, buf + 1*A_PtrSize)
NumPut(unkAddRef, buf + 2*A_PtrSize)
NumPut(unkRelease, buf + 3*A_PtrSize)
for i, prmc in prms
NumPut(RegisterCallback("WBClientSite_" name, "Fast", prmc+1, i), buf + (3+i)*A_PtrSize)
NumPut(buf + A_PtrSize, buf + 0)
WBClientSite[name] := buf
}
global wb
if pOleObject := ComObjQuery(wb, "{00000112-0000-0000-C000-000000000046}")
{ ; IOleObject::SetClientSite
DllCall(NumGet(NumGet(pOleObject+0)+3*A_PtrSize), "ptr"
, pOleObject, "ptr", WBClientSite.IOleClientSite, "uint")
ObjRelease(pOleObject)
}
}
WBClientSite_QI(p, piid, ppvObject)
{
static IID_IUnknown := "{00000000-0000-0000-C000-000000000046}"
static IID_IOleClientSite := "{00000118-0000-0000-C000-000000000046}"
static IID_IServiceProvider := "{6d5140c1-7436-11ce-8034-00aa006009fa}"
iid := _String4GUID(piid)
if (iid = IID_IOleClientSite || iid = IID_IUnknown)
{
NumPut(WBClientSite.IOleClientSite, ppvObject+0)
return 0 ; S_OK
}
if (iid = IID_IServiceProvider)
{
NumPut(WBClientSite.IServiceProvider, ppvObject+0)
return 0 ; S_OK
}
NumPut(0, ppvObject+0)
return 0x80004002 ; E_NOINTERFACE
}
WBClientSite_AddRef(p)
{
return 1
}
WBClientSite_Release(p)
{
return 1
}
WBClientSite_IOleClientSite(p, p1="", p2="", p3="")
{
if (A_EventInfo = 3) ; GetContainer
{
NumPut(0, p1+0) ; *ppContainer := NULL
return 0x80004002 ; E_NOINTERFACE
}
return 0x80004001 ; E_NOTIMPL
}
WBClientSite_IServiceProvider(p, pguidService, piid, ppvObject)
{
static IID_IUnknown := "{00000000-0000-0000-C000-000000000046}"
static IID_IInternetSecurityManager := "{79eac9ee-baf9-11ce-8c82-00aa004ba90b}"
if (_String4GUID(pguidService) = IID_IInternetSecurityManager)
{
iid := _String4GUID(piid)
if (iid = IID_IInternetSecurityManager || iid = IID_IUnknown)
{
NumPut(WBClientSite.IInternetSecurityManager, ppvObject+0)
return 0 ; S_OK
}
NumPut(0, ppvObject+0)
return 0x80004002 ; E_NOINTERFACE
}
NumPut(0, ppvObject+0)
return 0x80004001 ; E_NOTIMPL
}
WBClientSite_IInternetSecurityManager(p, p1="", p2="", p3="", p4="", p5="", p6="", p7="", p8="")
{
if (A_EventInfo = 5) ; ProcessUrlAction
{
if (p2 = 0x1400) ; dwAction = URLACTION_SCRIPT_RUN
{
NumPut(0, p3+0) ; *pPolicy := URLPOLICY_ALLOW
return 0 ; S_OK
}
}
return 0x800C0011 ; INET_E_DEFAULT_ACTION
}
_String4GUID(pGUID)
{
VarSetCapacity(String,38*2)
DllCall("ole32\StringFromGUID2", "ptr", pGUID, "str", String, "int", 39)
Return String
}
/* Utility Functions
*/
getWindow() {
global wb
return wb.document.parentWindow
}
ErrorExit(errMsg) {
global
if !SilentMode
MsgBox 0x2010, AutoHotkey Setup, %errMsg%
ExitApp 1
}
CloseScriptsEtc(installdir, actionToContinue) {
titles := ""
DetectHiddenWindows On
close := [], reopen := []
WinGet w, List, ahk_class AutoHotkey
Loop % w {
; Exclude the install script.
if (w%A_Index% = A_ScriptHwnd)
continue
; Determine if the script actually needs to be terminated.
WinGet exe_path, ProcessPath, % "ahk_id " w%A_Index%
if (exe_path != "") {
; Exclude external executables.
if InStr(exe_path, installdir "\") != 1
continue
; The main purpose of this next check is to avoid closing
; SciTE4AutoHotkey's toolbar, but also may be helpful for
; other situations.
exe := SubStr(exe_path, StrLen(installdir) + 2)
if !RegExMatch(exe, "i)^(AutoHotkey((A32|U32|U64)(_UIA)?)?\.exe|Compiler\\Ahk2Exe.exe)$")
continue
}
; Append script path to the list.
WinGetTitle title, % "ahk_id " w%A_Index%
title := RegExReplace(title, " - AutoHotkey v.*")
titles .= " - " title "`n"
close.Push(w%A_Index%)
if FileExist(title)
reopen.Push({path: title, exe: exe_path})
}
if (titles != "") {
global SilentMode, installInPlace
if !SilentMode {
static button_retry, button_mode
button_retry := 3
if (actionToContinue = "installation") {
help_text =
(LTrim
Click Reload to automatically reload the scripts later.
Click Close All to just close the scripts and continue.
)
button_mode := 3
} else {
help_text =
(LTrim
Click Close All to close all scripts and continue the %actionToContinue%.
)
button_mode := 1
}
SetTimer CloseScriptsEtc_Buttons, -5
MsgBox % 0x2030|button_mode, AutoHotkey Setup,
(LTrim
Setup needs to close the following script(s):
`n%titles%
%help_text%
)
IfMsgBox Cancel
Exit
IfMsgBox Yes
global AutoRestart := true
}
; Close script windows (typically causing them to exit).
Loop % close.MaxIndex()
{
WinClose % "ahk_id " close[A_Index]
WinWaitClose % "ahk_id " close[A_Index],, 1
}
}
; Close all help file windows automatically:
GroupAdd autoclosegroup, AutoHotkey_L Help ahk_class HH Parent
GroupAdd autoclosegroup, AutoHotkey Help ahk_class HH Parent
; Also close the old Ahk2Exe (but the new one is a script, so it
; was already handled by the section above):
GroupAdd autoclosegroup, Ahk2Exe v ahk_exe %installdir%\Compiler\Ahk2Exe.exe
WinClose ahk_group autoclosegroup
return reopen
CloseScriptsEtc_Buttons:
Critical
if !WinExist("ahk_class #32770 ahk_pid " DllCall("GetCurrentProcessId")) {
if (button_retry--)
SetTimer,, -5
return
}
if (button_mode = 1)
ControlSetText Button1, Close &All
else {
ControlSetText Button1, &Reload
ControlSetText Button2, Close &All
}
return
}
ReopenScripts(scripts) {
global AutoRestart
if !AutoRestart || !scripts || !scripts.MaxIndex()
return
failed := ""
for i, script in scripts {
workdir := script.path
SplitPath workdir,, workdir
try
script.exe ? Run_(script.exe, """" script.path """", workdir)
: Run_("""" script.path """",, workdir)
catch
failed .= "`n" script
}
if (failed != "" && !SilentMode)
MsgBox 0x2010, AutoHotkey Setup, Failed to restart the following scripts:`n%failed%
}
GetErrorMessage(error_code="") {
VarSetCapacity(buf, 1024) ; Probably won't exceed 1024 chars.
if DllCall("FormatMessage", "uint", 0x1200, "ptr", 0, "int", error_code!=""
? error_code : A_LastError, "uint", 1024, "str", buf, "uint", 1024, "ptr", 0)
return buf
}
switchPage(page) {
global
if !SilentMode
getWindow().switchPage(page)
}
UpdateStatus(status) {
; ToolTip % status
; if !SilentMode
; getWindow().install_status.innerText := status
}
ShellRun(prms*)
{
shellWindows := ComObjCreate("Shell.Application").Windows
VarSetCapacity(_hwnd, 4, 0)
desktop := shellWindows.FindWindowSW(0, "", 8, ComObj(0x4003, &_hwnd), 1)
if ptlb := ComObjQuery(desktop
, "{4C96BE40-915C-11CF-99D3-00AA004AE837}" ; SID_STopLevelBrowser
, "{000214E2-0000-0000-C000-000000000046}") ; IID_IShellBrowser
{
if DllCall(NumGet(NumGet(ptlb+0)+15*A_PtrSize), "ptr", ptlb, "ptr*", psv:=0) = 0
{
VarSetCapacity(IID_IDispatch, 16)
NumPut(0x46000000000000C0, NumPut(0x20400, IID_IDispatch, "int64"), "int64")
DllCall(NumGet(NumGet(psv+0)+15*A_PtrSize), "ptr", psv
, "uint", 0, "ptr", &IID_IDispatch, "ptr*", pdisp:=0)
shell := ComObj(9,pdisp,1).Application
shell.ShellExecute(prms*)
ObjRelease(psv)
}
ObjRelease(ptlb)
}
}
Run_(target, args:="", workdir:="") {
try
ShellRun(target, args, workdir)
catch e
Run % args="" ? target : target " " args, % workdir
}
/* Utility Functions invoked by the UI
*/
Customize() {
local w := getWindow()
if !ConfigureMode
w.document.body.className := "custom-mode"
w.switchPage("version")
}
SelectFolder(id, prompt="", root="::{20d04fe0-3aea-1069-a2d8-08002b30309d}") {
global wb
if !(field := wb.document.getElementById(id))
return
Gui +OwnDialogs
FileSelectFolder path
, % root " *" field.value
,, % prompt
if !ErrorLevel && (id != "installdir" || installdir_allowed(path))
field.value := path
}
ReadLicense() {
Run_(A_ScriptDir "\license.txt")
}
ViewHelp(topic) {
local path
if FileExist(A_ScriptDir "\AutoHotkey.chm")
path := A_ScriptDir "\AutoHotkey.chm"
else
path := CurrentPath "\AutoHotkey.chm"
if FileExist(path)
Run_("hh.exe", "mk:@MSITStore:" path "::" topic)
else
Run_("https://autohotkey.com" topic)
}
RunAutoHotkey() {
; Setup may be running as a user other than the one that's logged
; in (i.e. an admin user), so in addition to running AutoHotkey.exe
; in user mode, have it call the function below to ensure the script
; file is correctly located.
Run_("AutoHotkey.exe", """" A_WorkingDir "\Installer.ahk"" /exec runahk")
}
Exec_RunAHK() {
; This could detect %ExeDir%\AutoHotkey.ahk (which takes precedence
; over %A_MyDocuments%\AutoHotkey.ahk), but that file is unlikely to
; exist in this situation.
script_path := A_MyDocuments "\AutoHotkey.ahk"
; Start the script.
Run AutoHotkey.exe,,, pid
; Check for common failures.
SetTitleMatchMode 2
DetectHiddenWindows On
message := ""
message_flags := 0x2034
Loop {
Sleep 50
Process Exist, %pid%
if !ErrorLevel {
if !FileExist(script_path) {
WinWait AutoHotkey Help,, 1
if !ErrorLevel {
WinActivate ; Welcome screen (v1.1.20).
return
}
}
message =
(LTrim Join`s
AutoHotkey has exited. You may need to edit your startup
script. For instance, if it exited because it had nothing
to do, you can add a hotkey.
)
message_flags := 0x2044 ; Less severe, since it might be intentional.
break
}
if WinExist("ahk_class #32770 ahk_pid " pid) {
WinGetText message
if !InStr(message, "Error")
return
WinWaitClose
Process Exist, %pid%
message := "Your script encountered an error" (ErrorLevel ? "." : " and exited.")
. " You will need to edit it to resolve this error."
break
}
if WinExist("ahk_class AutoHotkey ahk_pid " pid) {
WinWaitClose,,, .2 ; Wait a moment in case the script is empty/about to exit.
if !ErrorLevel
continue ; Back to the top of the loop.
DetectHiddenWindows Off
if !WinExist("ahk_pid " pid)
MsgBox 0x2040, AutoHotkey Setup, Your script is running in the background.
return
}
}
MsgBox % message_flags, AutoHotkey Setup, %message%`n`nYour script is located here:`n %script_path%`n`nDo you want to edit this file?
IfMsgBox Yes
{
if !FileExist(script_path)
FileAppend,, %script_path%
Run edit "%script_path%"
}
}
Quit() {
ExitApp
}
ViewWebsite() {
global
Run_(ProductWebsite)
}
Extract(dstDir="") {
if (dstDir = "") {
FileSelectFolder dstDir,,, Select a folder to copy program files to.
if ErrorLevel
return
}
try {
global TestMode, SourceDir
if (TestMode = "FailExtract")
throw
shell := ComObjCreate("Shell.Application")
try FileCreateDir %dstDir%
dst := shell.NameSpace(dstDir)
src := shell.NameSpace(SourceDir)
if !(dst && src)
throw
try dst.CopyHere(src.Items, 256)
}
catch {
FileCopyDir %SourceDir%, %dstDir%, 1
if ErrorLevel {
MsgBox 0x2030, AutoHotkey Setup, An unspecified error occurred.
return
}
}
Run %dstDir%
}
DownloadAHK() {
global wb
wb.Stop()
file := A_Temp "\ahk-install.exe"
switchPage("downloading")
Sleep 10
if !Download("https://autohotkey.com/download/ahk-install.exe", file, "DownloadAHK_Progress") {
MsgBox 0x2010,, Download failed.
switchPage("start")
return
}
Run "%file%" /exec waitclose %A_ScriptHwnd% /exec downloaded "%file%"
ExitApp
}
Exec_WaitClose(hwnd) {
DetectHiddenWindows On
WinWaitClose ahk_id %hwnd%
}
Exec_Downloaded(file) {
; global SilentMode := true
DetermineVersion()
QuickInstall()
; NOTE: .\ is required here. Otherwise it launches the copy found
; in the directory containing the current module -- the temp dir.
Run .\AutoHotkeyU32.exe Installer.ahk /exec cleanup "%file%"
}
Exec_Cleanup(file) {
SplitPath file, name
Process WaitClose, %name%
MsgBox 64, AutoHotkey Setup, Installation complete.
FileDelete %file%
}
DownloadAHK_Progress(n, nMax) {
if !nMax
return
w := getWindow()
w.document.getElementById("dl_progress")
.style.width := (n*100/nMax) "%"
w.document.getElementById("dl_text")
.innerText := DownloadSize(n) " / " DownloadSize(nMax)
Sleep 10
}
DownloadSize(n) {
n /= 1024
if (n > 1024)
return Round(n/1024, 2) " MB"
return Round(n, 2) " KB"
}
; Based on code by Sean and SKAN @ http://www.autohotkey.com/forum/viewtopic.php?p=184468#184468
Download(url, file, callback) {
static vt
if !VarSetCapacity(vt) {
VarSetCapacity(vt, A_PtrSize*11), nPar := "31132253353"
Loop Parse, nPar
NumPut(RegisterCallback("DL_Progress", "F", A_LoopField, A_Index-1), vt, A_PtrSize*(A_Index-1))
}
if !(IsObject(callback) || (callback := Func(callback)))
return !(ErrorLevel := 1)
VarSetCapacity(bobj, A_PtrSize*2), NumPut(&callback, NumPut(&vt, bobj)), VarSetCapacity(tn, 520)
if (0 = DllCall("urlmon\URLDownloadToCacheFile", "ptr", 0, "str", url, "str", tn, "uint", 260, "uint", 0x10, "ptr", &bobj))
FileCopy %tn%, %file%, 1
else
ErrorLevel := 1
return !ErrorLevel
}
DL_Progress( pthis, nP=0, nPMax=0, nSC=0, pST=0 ) {
if A_EventInfo = 6
fn := Object(NumGet(pthis+A_PtrSize)), %fn%(np, npMax)
return 0
}
/* Setup Actions
*/
; Upgrade to newer version or from AutoHotkey to AutoHotkey_L.
; Type: "ANSI" or "Unicode"
Upgrade(Type="") {
global
_Install({
(Join C
type: Type,
path: DefaultPath,
menu: DefaultStartMenu,
ahk2exe: DefaultCompiler,
dragdrop: DefaultDragDrop,
uiAccess: DefaultUIAccess,
utf8: DefaultToUTF8,
isHostApp: DefaultIsHostApp
)})
}
; Quick install with default options.
QuickInstall() {
global
_Install({
(Join
type: DefaultType,
path: DefaultPath,
menu: DefaultStartMenu,
ahk2exe: DefaultCompiler,
dragdrop: DefaultDragDrop,
uiAccess: DefaultUIAccess,
utf8: DefaultToUTF8,
isHostApp: DefaultIsHostApp
)})
}
; Begin installation after reviewing options.
CustomInstall() {
local w := getWindow()
_Install({
(C Join
type: w.installtype.value,
path: w.installdir.value,
menu: w.startmenu.value,
ahk2exe: w.installcompiler.checked,
dragdrop: w.enabledragdrop.checked,
uiAccess: w.enableuiaccess.checked,
utf8: DefaultToUTF8, ;w.defaulttoutf8.checked
isHostApp: w.separatebuttons.checked
)})
}
; Uninstall.
Uninstall() {
global
try
SetWorkingDir % CurrentPath
catch
ErrorExit("Error uninstalling; installation directory '" CurrentPath "' may be invalid.")
CloseScriptsEtc(CurrentPath, "uninstallation")
switchPage("wait")
/* Registry
*/
SetRegView % CurrentRegView
RegDelete HKLM, %UninstallKey%
RegDelete HKLM, %AutoHotkeyKey%
RegDelete HKCU, %AutoHotkeyKey% ; Created by Ahk2Exe.
RegDelete HKCR, .ahk
RegDelete HKCR, %FileTypeKey%
RegDelete HKCR, Applications\AutoHotkey.exe
RegDelete HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AutoHotkey.exe
/* Files
*/
FileDelete AutoHotkeyU32.exe
FileDelete AutoHotkeyA32.exe
FileDelete AutoHotkeyU64.exe
FileDelete AutoHotkeyU32_UIA.exe
FileDelete AutoHotkeyA32_UIA.exe
FileDelete AutoHotkeyU64_UIA.exe
FileDelete WindowSpy.ahk
FileDelete AutoHotkey.chm
FileDelete license.txt
; These files would only exist if an older version of AutoHotkey(_L)
; installed it:
FileDelete Update.ahk
FileDelete AU3_Spy.exe
; Although the old installer was designed not to overwrite this in
; case the user made customizations, the old uninstaller deletes it:
FileDelete %A_WinDir%\ShellNew\Template.ahk
RemoveCompiler()
FileDelete %ProductName% Website.url
if (CurrentStartMenu != "") { ; Must not remove A_ProgramsCommon itself!
local i, lnk
for i, lnk in ["AutoHotkey", "AutoIt3 Window Spy", "Active Window Info (Window Spy)"
, "AutoHotkey Help File", "Website", "AutoHotkey Setup", "Convert .ahk to .exe"
, "Window Spy"]
FileDelete %A_ProgramsCommon%\%CurrentStartMenu%\%lnk%.lnk
FileRemoveDir %A_ProgramsCommon%\%CurrentStartMenu% ; Only if empty.
}
if !SilentMode
MsgBox 0x2040, AutoHotkey Setup
, Setup will now close to complete the uninstallation.
; Try deleting it normally first, in case this script is running
; on an external exe (such as via a downloaded installer).
FileDelete AutoHotkey.exe
if !ErrorLevel {
FileDelete Installer.ahk
SetWorkingDir %A_Temp% ; Otherwise FileRemoveDir will fail.
FileRemoveDir %CurrentPath% ; Only if empty.
ExitApp
}
Gui Cancel
; Use cmd.exe to work around the fact that AutoHotkey.exe is locked
; while it is still running. Having a second instance of the script
; terminate this instance should be more reliable than performing
; an arbitrary wait (e.g. by calling "ping").
Run %ComSpec% /c "
(Join`s&`s
AutoHotkey.exe "%A_ScriptFullPath%" /exec kill %A_ScriptHwnd%
del Installer.ahk
del AutoHotkey.exe
cd %A_Temp%
rmdir "%CurrentPath%"