Skip to content

Commit 207c73c

Browse files
committed
feat: Allow PutWindowInFocus function to detect hidden windows, such as those running in the system tray
1 parent ac53ce0 commit 207c73c

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

src/AHKCommandPicker.ahk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ IDEAS:
1313
- Maybe just hide the GUI instead of actually closing it every time; this would be good for tooltip mode too, since the tooltip could show the currently selected command from the window.
1414
- Allow user to create new simple commands easily from the GUI and save them in their own file (open file/program, path, website).
1515
16-
- Use Ctrl+Space to copy the full command/parameter into the input textbox, and Ctrl+, to copy it with a trailing comma (easier for parameters). Or maybe use Shift instead of Ctrl.
16+
- Use Ctrl+Space to copy the full command/parameter into the input textbox, and Ctrl+, to copy it with a trailing comma (easier for parameters). Or maybe use Shift instead of Ctrl.
1717
1818
*/
1919

@@ -28,7 +28,7 @@ IDEAS:
2828
;==========================================================
2929
; Global Variables - prefix everything with "cp" for Command Picker, so that variable/function names are not likely to conflict with user variables/function names.
3030
;==========================================================
31-
_cpWindowName := "AHK Command Picker v2.0.1"
31+
_cpWindowName := "AHK Command Picker v2.1.0"
3232
_cpWindowGroup := "" ; The group that will hold our Command Picker window so we can reference it from # directive statements (e.g. #IfWinExists).
3333
_cpCommandList := "" ; Will hold the list of all available commands.
3434
_cpCommandSelected := "" ; Will hold the command selected by the user.

src/DefaultCommands/UtilityFunctions.ahk

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,21 @@ PutWindowInFocus(windowName, applicationPath = "", titleMatchMode = "")
1919
; Used to tell when we have succeeded and stop trying.
2020
windowActivated := false
2121

22+
; Turn on searching for hidden windows, like apps running in the system tray.
23+
DetectHiddenWindows, On
24+
2225
; If the user did not specify a specific match mode to use, try them all starting with the most specific ones.
2326
if (titleMatchMode = "")
2427
{
2528
SetTitleMatchMode, 3 ; Start by trying to match the window's title exactly.
2629
gosub, PWIFTryActivateWindow
27-
30+
2831
if (!windowActivated)
2932
{
3033
SetTitleMatchMode, 1 ; Next try to match the start of the window's title.
3134
gosub, PWIFTryActivateWindow
3235
}
33-
36+
3437
if (!windowActivated)
3538
{
3639
SetTitleMatchMode, 2 ; Next try to match any part of the window's title.
@@ -55,7 +58,7 @@ PutWindowInFocus(windowName, applicationPath = "", titleMatchMode = "")
5558

5659
; If the window is not already open.
5760
if (!windowActivated)
58-
{
61+
{
5962
; If we were given a program to launch as a backup in case the wanted window wasn't found, then try and launch it.
6063
if (applicationPath != "")
6164
{
@@ -79,34 +82,34 @@ PutWindowInFocus(windowName, applicationPath = "", titleMatchMode = "")
7982
}
8083
}
8184
}
82-
85+
8386
; Restore the previous global modes that we might have changed.
8487
SetTitleMatchMode, %previousTitleMatchMode%
8588
DetectHiddenWindows, %previousDetectHiddenWindowsMode%
86-
89+
8790
; Return the handle of the window that was activated.
8891
if (windowActivated)
8992
{
9093
return WinExist("A")
9194
}
92-
95+
9396
; Else the window was not activated, so return 0 (i.e. false).
9497
return 0
95-
98+
9699
; Tries to put the window in focus if it already exists.
97100
PWIFTryActivateWindow:
98101
; If the window is already open.
99102
IfWinExist, %windowName%
100-
{
103+
{
101104
; Put the window in focus.
102105
WinActivate
103106
WinShow
104-
107+
105108
; Not all apps Restore properly when using WinShow and the app is Minimized (e.g. apps minimized using TrayIt!), so explicitly restore windows that are still minimized.
106109
WinGet, isMinimized, MinMax
107110
if (isMinimized = -1)
108111
WinRestore
109-
112+
110113
; Record success.
111114
windowActivated := true
112115
}
@@ -125,14 +128,14 @@ PutWindowInFocus(windowName, applicationPath = "", titleMatchMode = "")
125128
StringListContains(ByRef itemList, targetItem, itemDelimiter = "", removeTargetItem = false)
126129
{
127130
global _cpParameterDelimiter
128-
131+
129132
; If no delimiter was supplied, use the default parameter delimiter.
130133
if (itemDelimiter = "")
131134
itemDelimiter := _cpParameterDelimiter
132-
135+
133136
; Trim whitespace off of the target item
134137
targetItem := Trim(targetItem)
135-
138+
136139
; Loop through each item in the list and return true if the target is found in it.
137140
Loop, Parse, itemList, %itemDelimiter%
138141
{
@@ -143,7 +146,7 @@ StringListContains(ByRef itemList, targetItem, itemDelimiter = "", removeTargetI
143146
{
144147
; We don't know if this item is first, last, or the only item in the list, so try and remove the itemDelimiter with the item.
145148
; ErrorLevel will be set to 1 if the string to replace is not found.
146-
StringReplace, itemList, itemList, %A_LoopField%%itemDelimiter%
149+
StringReplace, itemList, itemList, %A_LoopField%%itemDelimiter%
147150
if (ErrorLevel = 1)
148151
StringReplace, itemList, itemList, %itemDelimiter%%A_LoopField%
149152
if (ErrorLevel = 1)
@@ -171,14 +174,14 @@ StringListContainsAndRemove(ByRef itemList, targetItem, itemDelimiter = "")
171174
ArrayContains(itemList, targetItem)
172175
{
173176
global _cpParameterDelimiter
174-
177+
175178
; If no delimiter was supplied, use the default parameter delimiter.
176179
if (itemDelimiter = "")
177180
itemDelimiter := _cpParameterDelimiter
178-
181+
179182
; Trim whitespace off of the target item
180183
targetItem := Trim(targetItem)
181-
184+
182185
; Loop through each item in the list and return true if the target is found in it.
183186
For index, value in parameters
184187
{
@@ -207,7 +210,7 @@ PasteText(textToPaste = "", pasteKeys = "^v")
207210
pasteKeys = !{Space}ep
208211
else if (pasteKeys = "cmdEnter")
209212
pasteKeys = !{Space}ep{Enter}
210-
213+
211214
clipboardBackup := ClipboardAll ; Backup whatever is currently on the Clipboard, including pictures and anything else.
212215
while (Clipboard != textToPaste) ; Make sure the Clipboard text has been updated before pasting it, as sometimes it does not get updated instantly.
213216
Clipboard := textToPaste

0 commit comments

Comments
 (0)