-
Notifications
You must be signed in to change notification settings - Fork 3
/
Starter.ahk
894 lines (773 loc) · 31.5 KB
/
Starter.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
/**
* Description:
* Smart launcher for your scripts with ability to compile/combine them into a single portable Starter.exe
* executable by single click.
* Requirements:
* AutoHotkey v1.1.33+
* Installation:
* git clone --recursive https://github.com/temoridao/ahk
* or download latest snapshot here: https://github.com/temoridao/ahk/releases
*
* Launch Starter.ahk and it will create Starter.txt, prompting you for the list of scripts to control.
* After you have done, save Starter.txt file and launch Starter.ahk again. Now you have all your
* scripts running under control of Starter.ahk.
* Starter.ahk can be compiled into single portable .exe file, containing all your controlled
* scripts (either by right click in explorer > Compile Script or with tray menu > Compile Starter.exe).
* See README.md for the list of features and other details: https://github.com/temoridao/ahk#starterahk
* Links:
* GitHub : https://github.com/temoridao/ahk
* Forum Topic: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=77910
* License:
* Dedicated to Public Domain. See UNLICENSE.txt for details
*/
SetWorkingDir %A_ScriptDir%
ListLines Off
FileEncoding UTF-8-RAW
;{ Config Section
; WARNING: Commented lines below starting with @ are directives for Ahk2Exe compiler
; and DIRECTLY AFFECT THE BEHAVIOR of resulting executable.
; Do not change these lines unless you know what you do.
;-------------------------------------------------------------------------------------------------
;@Ahk2Exe-Bin Unicode 64*
;@Ahk2Exe-AddResource *RT_RCDATA %A_AhkPath%, RC_AHKRUNTIME
; @Ahk2Exe-SetMainIcon %A_ScriptName~\..+$~.exe%.ico
;@Ahk2Exe-Obey SelfCompilationCommandResult, RunWait %A_AhkPath% "%A_ScriptFullPath%" --compile-package`, "%A_ScriptFullPath%\.."
;-------------------------------------------------------------------------------------------------
global Config := { Version : "3.0.0"
;@Ahk2Exe-SetVersion %A_PriorLine~U)^(.+"){1}(.+)".*$~$2%
, Elevate : HasVal(A_Args, "--elevate") ;Request elevated rights on startup
, ShowTrayTip : HasVal(A_Args, "--enable-tray-tip") || !A_IsCompiled
, ChildScriptsMatchMode : GetCmdParameterValue("--child-scripts-match-mode", "name") ; Possible values: "name", "pid"
, ReloadAllShortcut : "#+Escape"
;========================Options for compilation process========================================
, CompileMe : !A_IsCompiled && HasVal(A_Args, "--compile-package") || (CommonUtils.isKeyboardKeyPressedDllCall("Ctrl") && scriptsFromCommandLine().Length())
, UseCompression : HasVal(A_Args, "--compress-package")
, ProductName : GetCmdParameterValue("--product-name", scriptBaseName()) ;Name of resulting executable
, CompilerPath : FileExist("Ahk2Exe.exe") ? "Ahk2Exe.exe" : A_AhkPath "\..\Compiler\Ahk2Exe.exe"
, EmbedAhkAds : true ;Embed AutoHotkey.exe interpreter into Alternate Data Stream (ADS) of Starter.exe on supported file systems. Set to false to extract AutoHotkey.exe into A_Temp during Starter.exe runtime
, AdsName : GetCmdParameterValue("--ads-name", "AutoHotkey.exe") ;Name of the embeded file if ADS supported
, SkipDirPattern : GetCmdParameterValue("--skip-dir-pattern") } ;Directory names in A_ScriptDir (non-recursive) to skip to. (regex)
;===============================================================================================
;}
global cScriptResourceAliasPrefix := "StarterExeResourcePrefix_"
global g_exemptFromSuspensionScriptTitleRegex := ""
#NoEnv
#Warn UseUnsetLocal
#Warn UseUnsetGlobal
#UseHook
;Allow to launch this script for self-compilation while uncompiled version is running already.
;Second instance is prohibited by manual check in ScriptInfoUtils.checkForExistingInstance() function.
#SingleInstance OFF
#include <CommonUtils>
#include <AhkScriptController>
#include <TrayIconUtils>
#include <ErrMsg>
#include <ScriptInfoUtils>
SetTitleMatchMode 2 ;Match anywhere
DetectHiddenWindows ON
SetBatchLines -1
;@Ahk2Exe-IgnoreBegin
if (Config.CompileMe) {
compilePackage()
ExitApp
}
data := getScriptsForBundle()
global g_scriptsPaths := data.scripts
, g_scriptsCmdLines := data.cmdLines
, g_cInitialScriptsPaths := g_scriptsPaths.Clone() ;Immutable collection of script paths
;@Ahk2Exe-IgnoreEnd
if (otherInstancePid := ScriptInfoUtils.checkForExistingInstance()) {
MsgBox % "Already running (PID: " otherInstancePid ")"
ExitApp
}
if (Config.Elevate) {
CommonUtils.elevateThisScript()
}
/*@Ahk2Exe-Keep
if (ConfigCompiled.Elevate) {
CommonUtils.elevateThisScript()
}
global g_ahkRuntimeFile := extractExecutable()
, g_scriptsPaths := fetchScriptsList()
*/
OnExit("exitFunc")
OnMessage(WM_COMMAND := 0x111, "OnWM_COMMAND")
global g_scriptsPids := runScripts()
, g_forceSuspend := false ;true if the scripts suspended by user manually. Must take precedence over automatic suspension methods if any
setupTray()
/*
* Win+Shift+Escape — reload this script (preserving command line) and all of its managed scripts
*/
Hotkey(Config.ReloadAllShortcut, CommonUtils.getFuncObj("reloadScript"))
/* Win+Shift+` - Smart Reload Script matching %winTitle% (active window "A" by default)
*
* This function can reload even those scripts not managed by Starter.
* If %winTitle% is not an instance of AutoHotkey.exe, the result of WinGetTitle(winTitle)
* matched against names of ALL AutoHotkey scripts running on the system. The matched
* script (if any) will be reloaded. This is very convenient to reload the script which you
* are currently editing (i.e. the name of that script can be found in the title of the text
* editor you are working in) for debugging purposes.
*
* If matching script is managed by Starter.exe - reloads whole package.
* See comments below for more details.
*/
Hotkey("#+SC029", "smartReloadScript")
/*
* Win+Shift+S — Toggle suspend state of all managed scripts
* Alt+Win+Shift+S — same as previous but for ALL running scripts found on the system
*/
Hotkey("#+s", "toggleSuspendScripts")
Hotkey("!#+s", "toggleSuspendScriptsAll")
/*
* Place your custom code here if needed.
* You can utilize g_scriptsPids global variable which contain all controlled scripts' PIDs
* for use with "ahk_pid", for example. Alternatively (and preferably), place your code to the
* injection file(s). See Starter_injection.ahk for details; it included at the end of auto-execute section below.
*/
#include *i %A_LineFile%\..\Starter_injection.ahk
;
;--------------------------------------------End of auto-execute section------------------------------------------------
;
smartReloadScript(winTitle:="A") {
;If this is one of the scripts managed by Starter.exe - just reload whole package, because
;scripts running through named pipe have title like ".\\pipe\..." instead of human-readable path
pid := WinGet("PID", winTitle)
if (A_IsCompiled && HasVal(g_scriptsPids, pid)) {
Reload
return 0
}
if (scriptPath := CommonUtils.getAhkScriptFilePath(WinGet("ID", winTitle))) {
if (pidIndex := HasVal(g_scriptsPids, pid)) { ;this script managed by Starter.ahk
return reloadScript(pidIndex, scriptPath)
}
return CommonUtils.reloadScript(winTitle) ;this script is external, NOT managed by Starter.ahk
}
;If title of currently active window contains a (possibly part) file name of one of the
;AHK scripts running on the system - reload matching script
activeWinTitle := WinGetTitle(winTitle)
for i, hWnd in WinGet("List", "ahk_class AutoHotkey") {
fullPath := CommonUtils.getAhkScriptFilePath(hWnd)
SplitPath(fullPath, scriptName)
if (scriptName && InStr(activeWinTitle, scriptName)) {
pid := WinGet("PID", "ahk_id" hWnd)
if (pidIndex := HasVal(g_scriptsPids, pid)) {
return reloadScript(pidIndex, fullPath)
} else {
return CommonUtils.reloadScript("ahk_pid" pid)
}
}
}
return 0
}
OnWM_COMMAND(wParam, lParam, msg, hWnd) {
if (wParam = AhkScriptController.ID_FILE_SUSPEND || wParam = AhkScriptController.ID_TRAY_SUSPEND) {
toggleSuspendScripts()
return 0
}
}
setSuspendScripts(willSuspend, childScriptsOnly := true, showOsdIndication := true) {
regex := g_exemptFromSuspensionScriptTitleRegex
if (childScriptsOnly) {
if (A_IsCompiled) {
for each, pid in g_scriptsPids {
if (regex && (WinGetTitle("ahk_pid" pid) ~= regex))
continue
AhkScriptController.setSuspend("ahk_pid" pid, willSuspend)
}
} else {
if (Config.ChildScriptsMatchMode = "name") {
for i, name in g_cInitialScriptsPaths {
if (regex && (name ~= regex))
continue
AhkScriptController.setSuspend(name " ahk_class AutoHotkey", willSuspend)
}
} else if (Config.ChildScriptsMatchMode = "pid") {
for each, pid in g_scriptsPids {
if (regex && (WinGetTitle("ahk_pid" pid) ~= regex))
continue
AhkScriptController.setSuspend("ahk_pid" pid, willSuspend)
}
}
}
} else {
;Ignore suspension for current script to avoid unneeded recursion in OnMessage() handler. Current script will be
;suspended explicitly with `Suspend` command further below
prevValue := AhkScriptController.IgnoreCurrentScript
AhkScriptController.IgnoreCurrentScript := true
AhkScriptController.setSuspend("ahk_class AutoHotkey", willSuspend)
AhkScriptController.IgnoreCurrentScript := prevValue
}
Suspend % willSuspend ? 1 : 0 ;Explicitly suspend itself
if (showOsdIndication) {
suspendEnabledText := childScriptsOnly ? " Ⓢ " : " Ⓢ ALL "
suspendDisabledText := childScriptsOnly ? " 🔆 " : " 🔆 ALL "
CommonUtils.ShowSplashPictureWithText(,,, willSuspend ? suspendEnabledText : suspendDisabledText,,,100)
}
}
toggleSuspendScripts(showOsdIndication := true) {
Suspend Permit
setSuspendScripts(g_forceSuspend := !g_forceSuspend, true, showOsdIndication)
}
toggleSuspendScriptsAll(showOsdIndication := true) {
Suspend Permit
setSuspendScripts(g_forceSuspend := !g_forceSuspend, false, showOsdIndication)
}
runPlanFileName() {
return scriptBaseName() ".txt"
}
currentlyManagedScripts() {
return g_scriptsPids
}
scriptsFromCommandLine() {
scripts := []
for i, param in A_Args {
attr := FileExist(param)
if (!attr)
continue
if (!InStr(attr, "D") && (param ~= "\.ahk$")) {
scripts.Push(param)
continue
}
Loop Files, %param%\*.ahk
if (FileExist(A_LoopFileLongPath))
scripts.Push(A_LoopFileLongPath)
}
return scripts
}
;Returns list of script paths passed via command line. If command line has no scripts, then all scripts and their
;command lines (if any) from Starter.txt file
getScriptsForBundle() {
scripts := scriptsFromCommandLine()
if (scripts.Length()) {
return {scripts: scripts, cmdLines: []}
}
cmdLines := []
if (FileExist(runPlanFile := runPlanFileName())) {
Loop Read, %runPlanFile%
{
if (A_LoopReadLine ~= "^\s*;") ;Skip comment lines starting with ";"
continue
fileWantCompile := A_LoopReadLine ~= "^~"
if (Config.CompileMe && !fileWantCompile)
continue
path := fileWantCompile ? SubStr(A_LoopReadLine, 2) : A_LoopReadLine
cmdLine := ""
if (delimPos := InStr(path, "|")) {
cmdLine := SubStr(path, delimPos + 1)
path := SubStr(path, 1, delimPos - 1)
}
if (!(attr := FileExist(path)))
continue
if (isDirectory := InStr(attr, "D")) {
Loop Files, %path%\*.ahk
{
cmdLines.Push("")
Gosub RememberScriptPath
}
} else {
cmdLines.Push(cmdLine)
Gosub RememberScriptPath
}
}
}
; MsgBox % "Scripts to " (Config.CompileMe ? "compile: " : "launch: ") . ObjToString(scripts)
if (scripts.Length() = 0) {
showHelpDialog()
}
return {scripts: scripts, cmdLines: cmdLines}
RememberScriptPath:
scriptPath := isDirectory ? A_LoopFileLongPath : path
if (HasVal(scripts, scriptPath))
return
if (Config.CompileMe && !fileWantCompile)
return
scripts.Push(scriptPath)
return
}
runScript(path, cmdLine := "") {
if (path) {
absolutePath := GetAbsolutePath(path)
scriptDir := ""
SplitPath(absolutePath,, scriptDir)
return Run(A_AhkPath " /CP65001 /restart " quote(absolutePath) . (cmdLine ? (" " cmdLine) : ""), scriptDir)
}
return 0
}
;Restart script \scriptPath and optionally update g_scriptsPids. Returns new PID of reloaded script
reloadScript(oldPidIndex, scriptPath) {
newPid := runScript(scriptPath, g_scriptsCmdLines[oldPidIndex])
if (oldPidIndex) {
g_scriptsPids[oldPidIndex] := newPid
TrayIconUtils_removeTrayIcons([newPid])
}
return newPid
}
showHelpDialog() {
baseName := scriptBaseName()
helpTxt := A_ScriptName " cannot find any scripts to " (Config.CompileMe ? "compile" : "launch")
. " in " runPlanFileName() " or passed via command line parameters or by drag & drop from explorer.`r`n`r`n"
exampleTxtFile =
(LTrim
;Lines started with semicolon are comments and ignored, as well as empty lines
;Each line in this file is a script path (or folder with scripts) to launch (can be absolute or relative to this file)
;Put tilde (~) at the beginning of path to mark the script or folder for inclusion into portable compiled %baseName%.exe
;(just compile %A_ScriptName% after filling %baseName%.txt, or see README.md for other details)
;Command line parameters for each script (but not a directory) can be specified after pipe character (|)
;--------------------------------------Some Examples--------------------------------------------
; -------------
;3rdparty\MyGoodScriptToLaunch.ahk
;~ThisScriptWillBeIncludedIntoPortableExe.ahk
;~C:\path\to\AnotherScriptWhichWillBeIncludedIntoPortableExe.ahk
;ExampleScriptWithCommandLineParameters.ahk|--first-cmd-parameter --second --config my_config.ini
;
;By specifying path to a directory all .ahk files in that directory will be launched
;or compiled (if preceded with ~) into %baseName%.exe
;
;D:\Path\To\DirectoryWithScritpsToLaunch
;~D:\Path\To\DirectoryWithScriptsToLaunchAndIncludedIntoPortableExe
;
;-----------------------------------------------------------------------------------------------
;Put path to your scripts below and run %A_ScriptName% when you are done.
)
txtFile := runPlanFileName()
MsgBox % helpTxt "(Press OK to start editing scripts list)"
if (!FileExist(txtFile)) {
FileAppend(exampleTxtFile, txtFile)
}
Run % quote(txtFile)
WinWait % txtFile,,3
WinActivate % txtFile
ExitApp
}
setupTrayTip() {
tip := ""
for i, name in g_scriptsPaths {
tip .= " * " RegExReplace(cleanupScriptResourceAlias(name), "i)(.*[/\\])?(.+)\.ahk", "$2") "`n" ; Extract file's base name without extension
}
Menu Tray, Tip, % tip
}
cleanupScriptResourceAlias(resourceAlias) {
return RegExReplace(resourceAlias, "i)" cScriptResourceAliasPrefix "\d+_") ; Case-insensitive, because Ahk2Exe makes resource aliases uppercase
}
exitFunc(exitReason, exitCode) {
stopChildScripts()
/*@Ahk2Exe-Keep
if (ConfigCompiled.AhkRuntimeInAds) {
return ; No need to delete ADS file - it will be deleted automatically together with Starter.exe
}
FileDelete % g_ahkRuntimeFile
if (ErrorLevel) {
MsgBox % "Error deleting " quote(g_ahkRuntimeFile) ": " ErrMsg()
}
*/
}
stopChildScripts() {
errors := AhkScriptController.exitExternalScripts(g_scriptsPids, true)
if (errors.Length()) {
prev := LoggerConfiguration.WriteToFile
LoggerConfiguration.WriteToFile := true
logWarn("Errors occurred while exiting managed scripts: " ObjToString(errors))
LoggerConfiguration.WriteToFile := prev
}
g_scriptsPids := []
}
;--------------------------Starter.ahk-only Functions--------------------------
;@Ahk2Exe-IgnoreBegin
runScripts() {
pids := []
for i, filePath in g_scriptsPaths {
pids.Push(runScript(filePath, g_scriptsCmdLines[i]))
}
return pids
}
setupTray() {
for i, name in g_scriptsPaths {
Menu SubMenu_%name%, Add, View &Lines, OnScriptTrayCommandClicked
Menu SubMenu_%name%, Add, View &Variables, OnScriptTrayCommandClicked
Menu SubMenu_%name%, Add, View &Hotkeys, OnScriptTrayCommandClicked
Menu SubMenu_%name%, Add, View &Key History, OnScriptTrayCommandClicked
Menu SubMenu_%name%, Add
Menu SubMenu_%name%, Add, &Open, OnScriptTrayCommandClicked
Menu SubMenu_%name%, Add, &Reload, OnScriptTrayCommandClicked
Menu SubMenu_%name%, Add, &Edit, OnScriptTrayCommandClicked
Menu SubMenu_%name%, Add, &Exit, OnScriptTrayCommandClicked
Menu Tray, Add, &%name%, :SubMenu_%name%
}
Menu Tray, NoStandard
Menu Tray, Add
menuText := "&Compile " scriptBaseName() ".exe"
Menu Tray, Add, %menuText%, OnScriptTrayCommandClicked
showSummaryText := "Show Scripts Summary"
Menu Tray, Add, %showSummaryText%, showScriptsSummary
;Without explicit binding of empty parameter, the function will be called with standard parameters
;for Menu's function (ItemName, ItemPos, MenuName) which is obviously a wrong winTitle for target script
funcObj := CommonUtils.getFuncObj("reloadScript", "", "", "2")
Menu Tray, Add, % "Reload (preserve command line) |" Config.ReloadAllShortcut "|", % funcObj
menuText := "Reload as admin (preserve command line)"
funcObj := CommonUtils.getFuncObj("elevateThisScript", false)
Menu Tray, Add, % menuText, % funcObj
if (A_IsAdmin) {
Menu Tray, Disable, % menuText
}
menuText := "Edit " runPlanFileName()
Menu Tray, Add, %menuText%, editRunPlanFile
Menu Tray, Add
Menu Tray, Standard
Menu Tray, Default, %showSummaryText%
if (Config.ShowTrayTip) {
setupTrayTip()
}
CommonUtils.SetupTrayIcon()
TrayIconUtils_ensureTrayIconsHidden(Func("currentlyManagedScripts"))
}
OnScriptTrayCommandClicked() {
static Cmd_Open := 65300
, Cmd_Reload := AhkScriptController.ID_TRAY_RELOADSCRIPT
, Cmd_Edit := 65401
, Cmd_Exit := 65405
, Cmd_ViewLines := 65406
, Cmd_ViewVariables := 65407
, Cmd_ViewHotkeys := 65408
, Cmd_ViewKeyHistory := 65409
if (InStr(A_ThisMenuItem, "Compile")) {
return Run(A_ScriptFullPath " --compile-package")
}
cmd := RegExReplace(A_ThisMenuItem, "[^\w#@$?\[\]]") ; strip invalid chars
cmd := Cmd_%cmd%
scriptNamePartial := RegExReplace(A_ThisMenu,"SubMenu_(.+$)","$1")
if (cmd = Cmd_Exit) {
fullPath := CommonUtils.getAhkScriptFilePath(WinGet("ID", scriptNamePartial))
fileBaseName := ""
SplitPath(fullPath,,,, fileBaseName)
; tip := RegExReplace(A_IconTip, "s)\R? \* \Q" fileBaseName "\E\R?")
tip := RegExReplace(A_IconTip, "\* " fileBaseName, "✘ " fileBaseName)
Menu Tray, Tip, % tip
for i, scriptPath in g_scriptsPaths {
if InStr(fullPath, scriptPath) {
g_scriptsPaths.Remove(i)
g_scriptsPids.Remove(i)
g_scriptsCmdLines.Remove(i)
Menu Tray, Delete, %i%&
break
}
}
} else if (cmd = Cmd_Reload) {
fullPath := CommonUtils.getAhkScriptFilePath(WinGet("ID", scriptNamePartial))
for i, scriptPath in g_scriptsPaths
if InStr(fullPath, scriptPath)
return reloadScript(i, fullPath)
return
}
AhkScriptController.sendCommand(scriptNamePartial, cmd)
if (g_scriptsPaths.Length() = 0) {
ExitApp
}
}
showScriptsSummary() {
rows := g_scriptsPaths.MaxIndex() + 5
Gui Add, ListView, Grid +Resize r%rows% w600 Sort, Script|PID|Path|Command Line
Loop, % g_scriptsPaths.MaxIndex() {
SplitPath(g_scriptsPaths[A_Index], scriptName)
pid := g_scriptsPids[A_Index]
path := CommonUtils.getAhkScriptFilePath(WinGet("ID", "ahk_pid" pid))
LV_Add(, scriptName, pid, path, g_scriptsCmdLines[A_Index])
}
LV_ModifyCol()
LV_ModifyCol(3, "AutoHdr")
title := "Controlled Scripts - " A_ScriptName
Gui Show, Center, %title%
return
GuiEscape:
GuiClose:
Gui Destroy
return
}
editRunPlanFile() {
Run % runPlanFileName()
}
cleanTemporaryScripts() {
for i, file in getScriptsForBundle().scripts {
FileDelete % file . ".preprocessed"
}
FileDelete % A_ScriptFullPath ".augmented_with_addresource_directives"
}
compilePackage() {
Menu Tray, Tip, % A_ScriptName " (Compiling...)"
startTime := A_TickCount
try {
renamedDirectories := {} ;key -> original name; value -> new name
if (Config.SkipDirPattern) {
cRenameSuffix := "_"
for i, v in StrSplit(Config.SkipDirPattern, "|")
if (!FileExist(dirPath := A_ScriptDir "\" v))
return MsgBox("Aborting due to impossibility to skip non-existent directory: " quote(dirPath))
Loop Files, %A_ScriptDir%\*.*, D
{
if (!(A_LoopFileName ~= "i)" Config.SkipDirPattern))
continue
newDirName := A_LoopFileLongPath . cRenameSuffix
FileMoveDir(A_LoopFileLongPath, newDirName, "R")
;FileMove/FileMoveDir/FileDelete throw exception instead of setting ErrorLevel if finally{} block present.
;But inside finally{} block itself they set ErrorLevel again instead of throwing…
; if (ErrorLevel)
; return MsgBox("Error renaming " quote(A_LoopFileLongPath) ": " ErrMsg()) ;executes finally{} block
renamedDirectories[A_LoopFileLongPath] := newDirName
}
}
compress := useCompression()
OnExit("cleanTemporaryScripts") ;Ensure that intermediate scripts will be deleted even in case of exception in the code below
inFile := preprocessScripts()
outFile := A_ScriptDir "\" Config.ProductName (compress ? "c" : "") ".exe"
;CMD cheat-sheet: Ahk2Exe.exe /in infile.ahk [/out outfile.exe] [/icon iconfile.ico] [/bin AutoHotkeySC.bin] [/mpress 1 (true) or 0 (false)] [/cp codepage]
RunWait % Config.CompilerPath " /in " quote(inFile) " /out " quote(outFile) . (compress ? " /compress 2" : ""),,UseErrorLevel
cleanTemporaryScripts()
; Terminates Ahk2Exe process and its child which waits on 'Ahk2Exe-Obey SelfCompilationCommandResult' directive
; And delete orphaned temporary file(s)
orphanedFile := ""
for i, hWnd in WinGet("List", "ahk_exe Ahk2Exe.exe") {
compilerPid := WinGet("PID", "ahk_id" hWnd)
if (InStr(CommonUtils.getProcessCommandLine(compilerPid), A_ScriptName)) { ; If this is compiler which compiles us
wmi := ComObjGet("winmgmts:")
queryEnum := wmi.ExecQuery("SELECT * FROM Win32_Process WHERE ParentProcessId=" compilerPid)._NewEnum()
queryEnum[procCompilerChild] ;AutoHotkey.exe which waits in RunWait from Obey
if (RegExMatch(procCompilerChild.CommandLine, "~Ahk2Exe.+\.tmp")) {
orphanedFile := GetAbsolutePath(RegExReplace(procCompilerChild.CommandLine, ".+?(\Q" A_Temp "\E\\~Ahk2Exe.+\.tmp)", "$1"))
Process Close, % compilerPid
Process Close, % procCompilerChild.ProcessId
}
}
}
includedScripts := ""
for i, scriptPath in getScriptsForBundle().scripts {
includedScripts .= " " scriptPath "`n"
}
MsgBox % Format("Compilation finished!`nProduct: {} v{}`n{} seconds elapsed`n`nIncluded scripts:`n{}`n`n{}`n{}"
, outFile
, Config.Version
, (A_TickCount - startTime) / 1000
, includedScripts
, "Press OK to finish"
, "(Hold Ctrl key to open product destination folder also)")
if (GetKeyState("Ctrl", "P")) {
WinExist(A_ScriptDir " ahk_exe explorer.exe") ? (WinRestore(), WinActivate(), CommonUtils.setExplorerSelection(WinExist(), [outFile]))
: Run("explorer.exe /select`, " outFile)
}
; logDebug("Deleting orphaned temporary: " quote(orphanedFile))
if (FileExist(orphanedFile)) {
FileDelete % orphanedFile
}
TrayIconUtils_removeOrphans()
return outFile
} catch e {
MsgBox % "Exception: " ObjToString(e) "`nTraceBack: " ObjToString(Traceback())
} finally {
failedRenames := {}
;Restore names for previously renamed directories
for originalName, newName in renamedDirectories {
FileMoveDir(newName, originalName, "R")
if (ErrorLevel)
failedRenames[originalName] := newName
}
if (failedRenames.Count()) {
MsgBox % "Failed to restore original names: " ObjToString(failedRenames)
ExitApp 1
}
}
}
preprocessScripts() {
;-------------------------------------------------------------------------------------------------
addResourceDirectives := ""
for i, file in getScriptsForBundle().scripts {
scriptIndex := A_Index
Loop Files, %file%
{
scriptCopy := A_LoopFileLongPath . ".preprocessed"
FileCopy(A_LoopFilePath, scriptCopy, 1)
;Use undocumented Ahk2Exe-OutputPreproc directive, which accepts single parameter - path where to place preprocessed script
FileAppend("`n;@Ahk2Exe-OutputPreproc " scriptCopy, scriptCopy)
;Replace copy of original script with its preprocessed variant after compilation
;/out to NUL because we interested only in preprocessed output for now
exitCode := RunWait(Format("{:s} /in ""{:s}"" /out NUL", Config.CompilerPath, scriptCopy))
if (exitCode != 0) {
MsgBox % "Cannot preprocess script " quote(A_LoopFileLongPath) ".`n"
. "Error code: " exitCode
ExitApp exitCode
}
;Add prefix to resource alias to allow further sorting and maintain correct launch order. See ResourceAliasSortFunctor()
addResourceDirectives .= "`n;@Ahk2Exe-AddResource *RT_RCDATA " scriptCopy ", " cScriptResourceAliasPrefix . scriptIndex "_" A_LoopFilePath
}
}
addResourceDirectives .= "`n"
;-------------------------------------------------------------------------------------------------
compiledConfigText := "/*@Ahk2Exe-Keep"
. "`nglobal ConfigCompiled :="
. "`n( LTrim Join"
. "`n{"
if (Config.EmbedAhkAds) {
compiledConfigText .= "`n AhkRuntimeInAds: true`n, AhkRuntimeAdsName: " quote(Config.AdsName) ","
}
compiledConfigText .= "`n Elevate: " (Config.Elevate ? "true" : "false") ","
;Remove last comma if any
if (SubStr(compiledConfigText, 0) = ",") {
compiledConfigText := SubStr(compiledConfigText, 1, StrLen(compiledConfigText) - 1)
}
compiledConfigText .= "`n}`n)`n*/`n"
;-------------------------------------------------------------------------------------------------
;1. Create copy of this script
;2. Add "global ConfigCompiled" object (which will be consulted in Starter.exe only) at the top
; of resulting script. The object may have no key/value pairs at all.
;3. Add necessary Ahk2Exe directives for packaging
outFileName := A_ScriptFullPath ".augmented_with_addresource_directives"
thisScriptText := FileRead(A_ScriptFullPath)
; Delete @Ahk2Exe-Obey directive from preprocessed script to prevent eternal spawning of Ahk2Exe processes
thisScriptText := RegExReplace(thisScriptText, "m)^\s*;@Ahk2Exe-Obey SelfCompilationCommandResult.+$")
FileAppend(compiledConfigText . addResourceDirectives . thisScriptText, outFileName, "UTF-8")
return outFileName
}
useCompression() {
compress := Config.UseCompression
SplitPath(Config.CompilerPath,,compilerDirectory)
if (Config.UseCompression && !FileExist(compilerDirectory "\upx.exe")) {
if (FileExist("upx.exe")) {
FileCopy("upx.exe", compilerDirectory "\upx.exe")
} else {
MsgBox % "upx.exe not found. Executable will not be compressed.`r`nPlace upx.exe near " A_ScriptName " to be able create compressed executable"
compress := false
}
}
return compress
}
;--------------------------End of Starter.ahk-only Functions--------------------------
;@Ahk2Exe-IgnoreEnd
;--------------------------Starter.exe-only Functions--------------------------
/*@Ahk2Exe-Keep
#WinActivateForce
DllRead( ByRef Var, Filename, Section, Key ) { ; By SKAN | goo.gl/DjDxzW
Local ResType, ResName, hMod, hRes, hData, pData, nBytes := 0
ResName := ( Key+0 ? Key : &Key ), ResType := ( Section+0 ? Section : &Section )
VarSetCapacity( Var,128 ), VarSetCapacity( Var,0 )
If hMod := DllCall( "LoadLibraryEx", "Str",Filename, "Ptr",0, "UInt",0x2, "Ptr" )
If hRes := DllCall( "FindResource", "Ptr",hMod, "Ptr",ResName, "Ptr",ResType, "Ptr" )
If hData := DllCall( "LoadResource", "Ptr",hMod, "Ptr",hRes, "Ptr" )
If pData := DllCall( "LockResource", "Ptr",hData, "Ptr" )
If nBytes := DllCall( "SizeofResource", "Ptr",hMod, "Ptr",hRes )
VarSetCapacity( Var,nBytes,1 )
, DllCall( "RtlMoveMemory", "Ptr",&Var, "Ptr",pData, "Ptr",nBytes )
DllCall( "FreeLibrary", "Ptr",hMod )
Return nBytes
}
extractExecutable() {
resultPath := A_Temp "\" UUIDCreate()
if (ConfigCompiled.AhkRuntimeInAds) {
;Only NTFS file system supports ADS. In other cases will fallback to temporary file
if (DriveGet("FileSystem", SubStr(A_ScriptFullPath, 1, 3)) = "NTFS") {
resultPath := A_ScriptFullPath ":" ConfigCompiled.AhkRuntimeAdsName
if (FileExist(resultPath)) {
return resultPath ;No need to write ADS again if it already there from previous launch
}
}
}
bytesCount := DllRead(var, A_ScriptFullPath, "RT_RCDATA", "RC_AHKRUNTIME")
file := FileOpen(resultPath, "w")
if (!IsObject(file)) {
MsgBox % "Can't open " resultPath " for writing"
ExitApp -3
}
file.RawWrite(var, bytesCount)
return resultPath
}
runScripts() {
pids := []
for each, script in g_scriptsPaths {
bytesCount := DllRead(data, A_ScriptFullPath, "RT_RCDATA", script)
scriptText := StrGet(&data, bytesCount, "utf-8") ; convert bytes from utf-8 to native script's encoding
pids.Push(ExecScript(g_ahkRuntimeFile, scriptText))
}
return pids
}
setupTray() {
Menu Tray, NoStandard
Menu Tray, Add, Exit, onExitTrayItemClicked
if (Config.ShowTrayTip) {
setupTrayTip()
}
;NOTE: do not call CommonUtils.SetupTrayIcon() here, because for the icon compiled script was set at compile time with @SetMainIcon directive
TrayIconUtils_ensureTrayIconsHidden(Func("currentlyManagedScripts"))
}
onExitTrayItemClicked() {
ExitApp
}
enumResourcesCallback(hModule, lpszType, lpszName, lParam) {
resourceAlias := StrGet(lpszName)
if (isAhkScriptResource := InStr(resourceAlias, ".ahk", false)) {
Object(lParam).Push(resourceAlias)
}
; OutputDebug % StrGet(lpszType) " - " StrGet(lpszName) " - " lParam
return true ; Return false to stop enumeration
}
;Extracts script names from the Starter.exe's resources
fetchScriptsList() {
scriptNames := []
if (!DllCall("EnumResourceNames", "Ptr",A_ScriptFullPath, "Str","RT_RCDATA", "Ptr",RegisterCallback("enumResourcesCallback", "F" ), "UInt",&scriptNames )) {
MsgBox % "Error calling EnumResourceNames(): " ErrMsg()
ExitApp -1
}
if (!scriptNames.Count()) {
MsgBox % "No suitable resources found!"
ExitApp -2
}
return sortArray(scriptNames, "F ResourceAliasSortFunctor")
}
ResourceAliasSortFunctor(a1, a2) {
; Sorts in ascending numeric order. This method works only if the difference is never so large as to overflow a signed 64-bit integer.
return (RegExReplace(a1, "i)^" cScriptResourceAliasPrefix "(\d+)_.+$", "$1"))
- (RegExReplace(a2, "i)^" cScriptResourceAliasPrefix "(\d+)_.+$", "$1"))
}
ExecScript(ahkPath, scriptText, scriptCommandLineParams := "", workingDir := "") {
if !FileExist(ahkPath) {
throw Exception("AutoHotkey runtime not found: " ahkPath)
}
pid := 0
try {
pipeName := "\\.\pipe\" A_TickCount
pipeHandles := []
Loop 3 {
pipeHandles[A_Index] := DllCall("CreateNamedPipe"
, "Str", pipeName
, "UInt", 2, "UInt", 0
, "UInt", 255, "UInt", 0
, "UInt", 0, "UPtr", 0
, "UPtr", 0, "UPtr")
}
if !FileExist(pipeName) {
return 0
}
pid := Run(ahkPath " /CP65001 " pipeName " " scriptCommandLineParams, workingDir)
DllCall("ConnectNamedPipe", "UPtr", pipeHandles[2], "UPtr", 0)
DllCall("ConnectNamedPipe", "UPtr", pipeHandles[3], "UPtr", 0)
FileOpen(pipeHandles[3], "h", "UTF-8").Write(scriptText)
} finally {
Loop 3 {
DllCall("CloseHandle", "UPtr", pipeHandles[A_Index])
}
}
return pid
}
UUIDCreate(mode:=1, format:="", ByRef UUID:="") {
UuidCreate := "Rpcrt4\UuidCreate"
if InStr("02", mode)
UuidCreate .= mode? "Sequential" : "Nil"
VarSetCapacity(UUID, 16, 0) ;// long(UInt) + 2*UShort + 8*UChar
pString := ""
if (DllCall(UuidCreate, "Ptr", &UUID) == 0)
&& (DllCall("Rpcrt4\UuidToString", "Ptr", &UUID, "UInt*", pString) == 0)
{
string := StrGet(pString)
DllCall("Rpcrt4\RpcStringFree", "UInt*", pString)
if InStr(format, "U")
DllCall("CharUpper", "Ptr", &string)
return InStr(format, "{") ? "{" . string . "}" : string
}
}
*/
;--------------------------End of Starter.exe-only Functions--------------------------