-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathAlreadyRunning.ahk
120 lines (108 loc) · 2.95 KB
/
AlreadyRunning.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
;************************
;* *
;* AlreadyRunning *
;* *
;************************
;
;
; Description
; ===========
; This function checks to see if multiple occurrences of the script are
; running.
;
;
;
; Parameters
; ==========
;
; Name Description
; ---- -----------
; p_MsgBox If set to TRUE and there are multiple occurrences of the
; script running, an appropriate error message is
; displayed. [Optional] The default is TRUE.
;
;
; p_ExitApp If set to TRUE and there are multiple occurrences of the
; script running, the "ExitApp" command is performed.
; [Optional] The default is TRUE.
;
;
;
; Return Codes
; ============
; Returns TRUE if multiple occurrences of the script are running, otherwise
; returns FALSE.
;
;
;
; Credit
; ======
; The original idea/script from jonny
; http://www.autohotkey.com/forum/viewtopic.php?p=108296#108296
;
;
;
; Programming and Usage Notes
; ===========================
; - The function only works if the "#SingleInstance off" directive has been
; defined and is active.
;
; - Please note that setting p_Message to FALSE and p_ExitApp to TRUE is the
; functional equivalent of using the "#SingleInstance ignore" directive.
;
;
;-------------------------------------------------------------------------------
#SingleInstance off
AlreadyRunning(p_MsgBox=True,p_ExitApp=True)
{
;[==============]
;[ Initialize ]
;[==============]
l_DetectHiddenWindows:=A_DetectHiddenWindows
DetectHiddenWindows On
;[========================]
;[ Find other processes ]
;[ (if any) ]
;[========================]
Process Exist
WinGetTitle l_Title,ahk_pid %ErrorLevel%
WinGet l_Count,Count,%l_Title%
DetectHiddenWindows %l_DetectHiddenWindows%
;[======================]
;[ Only 1 occurrence? ]
;[======================]
if l_Count=1
return False
;[===================]
;[ Already running ]
;[===================]
;-- Initialize
l_Type=script
SplitPath l_Title,,,l_TitleExt
if l_TitleExt=exe
l_Type=program
;[==================]
;[ Error message? ]
;[==================]
if p_MsgBox
{
SplitPath A_ScriptName,,,,l_ScriptName
MsgBox
,262192
; 0 ("OK" button)
; + 48 (Exclamation Icon)
; + 262144 (Always On Top)
; ------
; 262192
;
,%l_ScriptName%
,The "%A_ScriptName%" %l_Type% is already running. %A_Space%
}
;[=====================]
;[ Terminate script? ]
;[=====================]
if p_ExitApp
ExitApp
;-- Return to sender
return True
}