Code: Select all
; CHANGES:
; July 22, 2005 (changes provided by egilmour):
; - Added new hotkey to unhide the last hidden window (Win+U)
;
; November 3, 2004 (changes provided by trogdor):
; - Program manager is prevented from being hidden.
; - If there is no active window, the minimize-to-tray hotkey will have
; no effect rather than waiting indefinitely.
;
; October 23, 2004:
; - The taskbar is prevented from being hidden.
; - Some possible problems with long window titles have been fixed.
; - Windows without a title can be hidden without causing problems.
; - If the script is running under AHK v1.0.22 or greater, the
; maximum length of each menu item is increased from 100 to 260.
; CONFIGURATION SECTION: Change the below values as desired.
; This is the maximum number of windows to allow to be hidden (having a
; limit helps performance):
mwt_MaxWindows = 50
; This is the hotkey used to hide the active window:
mwt_Hotkey = #h ; Win+H
; This is the hotkey used to unhide the last hidden window:
mwt_UnHotkey = #u ; Win+U
; If you prefer to have the tray menu empty of all the standard items,
; such as Help and Pause, use N. Otherwise, use Y:
mwt_StandardMenu = Y
; These next few performance settings help to keep the action within the
; #HotkeyModifierTimeout period, and thus avoid the need to release and
; press down the hotkey's modifier if you want to hide more than one
; window in a row. These settings are not needed you choose to have the
; script use the keyboard hook via #InstallKeybdHook or other means:
#HotkeyModifierTimeout 100
SetWinDelay 10
SetKeyDelay 0
#SingleInstance ; Allow only one instance of this script to be running.
; END OF CONFIGURATION SECTION (do not make changes below this point
; unless you want to change the basic functionality of the script).
Hotkey, %mwt_Hotkey%, mwt_Minimize
Hotkey, %mwt_UnHotkey%, mwt_UnMinimize
; If the user terminates the script by any means, unhide all the
; windows first:
OnExit, mwt_RestoreAllThenExit
if mwt_StandardMenu = Y
Menu, Tray, Add
else
{
Menu, Tray, NoStandard
Menu, Tray, Add, E&xit and Unhide All, mwt_RestoreAllThenExit
}
Menu, Tray, Add, &Unhide All Hidden Windows, mwt_RestoreAll
Menu, Tray, Add ; Another separator line to make the above more special.
if A_AhkVersion = ; Since it's blank, version is older than 1.0.22.
mwt_MaxLength = 100
else
mwt_MaxLength = 260 ; Reduce this to restrict the width of the menu.
return ; End of auto-execute section.
mwt_Minimize:
if mwt_WindowCount >= %mwt_MaxWindows%
{
MsgBox No more than %mwt_MaxWindows% may be hidden simultaneously.
return
}
; Set the "last found window" to simplify and help performance.
; Since in certain cases it is possible for there to be no active window,
; a timeout has been added:
WinWait, A,, 2
if ErrorLevel <> 0 ; It timed out, so do nothing.
return
; Otherwise, the "last found window" has been set and can now be used:
WinGet, mwt_ActiveID, ID
WinGetTitle, mwt_ActiveTitle
WinGetClass, mwt_ActiveClass
if mwt_ActiveClass in Shell_TrayWnd,Progman
{
MsgBox The desktop and taskbar cannot be hidden.
return
}
; Because hiding the window won't deactivate it, activate the window
; beneath this one (if any). I tried other ways, but they wound up
; activating the task bar. This way sends the active window (which is
; about to be hidden) to the back of the stack, which seems best:
Send, !{esc}
; Hide it only now that WinGetTitle/WinGetClass above have been run (since
; by default, those commands cannot detect hidden windows):
WinHide
; If the title is blank, use the class instead. This serves two purposes:
; 1) A more meaningful name is used as the menu name.
; 2) Allows the menu item to be created (otherwise, blank items wouldn't
; be handled correctly by the various routines below).
if mwt_ActiveTitle =
mwt_ActiveTitle = ahk_class %mwt_ActiveClass%
; Ensure the title is short enough to fit. mwt_ActiveTitle also serves to
; uniquely identify this particular menu item.
StringLeft, mwt_ActiveTitle, mwt_ActiveTitle, %mwt_MaxLength%
; In addition to the tray menu requiring that each menu item name be
; unique, it must also be unique so that we can reliably look it up in
; the array when the window is later unhidden. So make it unique if it
; isn't already:
Loop, %mwt_MaxWindows%
{
if mwt_WindowTitle%A_Index% = %mwt_ActiveTitle%
{
; Match found, so it's not unique.
; First remove the 0x from the hex number to conserve menu space:
StringTrimLeft, mwt_ActiveIDShort, mwt_ActiveID, 2
StringLen, mwt_ActiveIDShortLength, mwt_ActiveIDShort
StringLen, mwt_ActiveTitleLength, mwt_ActiveTitle
mwt_ActiveTitleLength += %mwt_ActiveIDShortLength%
mwt_ActiveTitleLength += 1 ; +1 the 1 space between title & ID.
if mwt_ActiveTitleLength > %mwt_MaxLength%
{
; Since menu item names are limted in length, trim the title
; down to allow just enough room for the Window's Short ID at
; the end of its name:
TrimCount = %mwt_ActiveTitleLength%
TrimCount -= %mwt_MaxLength%
StringTrimRight, mwt_ActiveTitle, mwt_ActiveTitle, %TrimCount%
}
; Build unique title:
mwt_ActiveTitle = %mwt_ActiveTitle% %mwt_ActiveIDShort%
break
}
}
; First, ensure that this ID doesn't already exist in the list, which can
; happen if a particular window was externally unhidden (or its app unhid
; it) and now it's about to be re-hidden:
mwt_AlreadyExists = n
Loop, %mwt_MaxWindows%
{
if mwt_WindowID%A_Index% = %mwt_ActiveID%
{
mwt_AlreadyExists = y
break
}
}
; Add the item to the array and to the menu:
if mwt_AlreadyExists = n
{
Menu, Tray, add, %mwt_ActiveTitle%, RestoreFromTrayMenu
mwt_WindowCount += 1
Loop, %mwt_MaxWindows% ; Search for a free slot.
{
; It should always find a free slot if things are designed right.
if mwt_WindowID%A_Index% = ; An empty slot was found.
{
mwt_WindowID%A_Index% = %mwt_ActiveID%
mwt_WindowTitle%A_Index% = %mwt_ActiveTitle%
break
}
}
}
return
RestoreFromTrayMenu:
Menu, Tray, delete, %A_ThisMenuItem%
; Find window based on its unique title stored as the menu item name:
Loop, %mwt_MaxWindows%
{
if mwt_WindowTitle%A_Index% = %A_ThisMenuItem% ; Match found.
{
StringTrimRight, IDToRestore, mwt_WindowID%A_Index%, 0
WinShow, ahk_id %IDToRestore%
WinActivate ahk_id %IDToRestore% ; Sometimes needed.
mwt_WindowID%A_Index% = ; Make it blank to free up a slot.
mwt_WindowTitle%A_Index% =
mwt_WindowCount -= 1
break
}
}
return
;; This will pop the last minimized window off the stack and unhide it.
mwt_UnMinimize:
;; Make sure there's something to unhide.
if mwt_WindowCount > 0
{
;; Get the id of the last window minimized and unhide it
StringTrimRight, IDToRestore, mwt_WindowID%mwt_WindowCount%, 0
WinShow, ahk_id %IDToRestore%
WinActivate ahk_id %IDToRestore%
;; Get the menu name of the last window minimized and remove it
StringTrimRight, MenuToRemove, mwt_WindowTitle%mwt_WindowCount%, 0
Menu, Tray, delete, %MenuToRemove%
;; clean up our 'arrays' and decrement the window count
mwt_WindowID%mwt_WindowCount% =
mwt_WindowTitle%mwt_WindowCount% =
mwt_WindowCount -= 1
}
return
mwt_RestoreAllThenExit:
Gosub, mwt_RestoreAll
ExitApp ; Do a true exit.
mwt_RestoreAll:
Loop, %mwt_MaxWindows%
{
if mwt_WindowID%A_Index% <>
{
StringTrimRight, IDToRestore, mwt_WindowID%A_Index%, 0
WinShow, ahk_id %IDToRestore%
WinActivate ahk_id %IDToRestore% ; Sometimes needed.
; Do it this way vs. DeleteAll so that the sep. line and first
; item are retained:
StringTrimRight, MenuToRemove, mwt_WindowTitle%A_Index%, 0
Menu, Tray, delete, %MenuToRemove%
mwt_WindowID%A_Index% = ; Make it blank to free up a slot.
mwt_WindowTitle%A_Index% =
mwt_WindowCount -= 1
}
if mwt_WindowCount = 0
break
}
return
Code: Select all
ini_read(ByRef ini_file, ByRef ini_section, ByRef ini_key, ByRef ini_default := "error")
{
IniRead, output_var, %ini_file%, %ini_section%, %ini_key%, %ini_default%
return %output_var%
}
ini_read_array(ByRef ini_file, ByRef ini_section, ByRef ini_key, ByRef ini_default := "error") {
IniRead, output_str, %ini_file%, %ini_section%, %ini_key%, %ini_default%
arr := StrSplit(output_str, ", ")
return %arr%
}
random_sleep_k(ByRef min, ByRef max) {
sleep(random_k(min, max))
}
random_sleep(ByRef min, ByRef max) {
sleep(random(min, max))
}
random_k(ByRef min, ByRef max) {
random, n, % min*1000, % max*1000
return %n%
}
random(ByRef min, ByRef max) {
random, r, %min%, %max%
return %r%
}
send_sleep(ByRef key, ByRef t) {
send(key)
sleep(t)
}
send(ByRef key) {
Send, %key%
}
send_active_window(ByRef key, ByRef window) {
if !window_active(window)
return
send(key)
}
sleep(ByRef t) {
Sleep, %t%
}
send_random_key(byRef key_array) {
send(random_array_element(key_array))
}
control_send_random_key_window(byRef key_array, ByRef window) {
control_send(random_array_element(key_array), window)
}
random_array_element(ByRef array) {
return array[random(1, array.MaxIndex())]
}
control_send_random_key_active_window(byRef key_array, ByRef window) {
if window_active(window)
return control_send_random_key_window(key_array, window)
}
control_send_random_key_inactive_window(byRef key_array, ByRef window) {
if !window_active(window)
return control_send_random_key_window(key_array, window)
}
control_send(ByRef key, ByRef window) {
ControlSend, ahk_parent, %key%, %window%
}
connected_to_internet(flag = 0x40) {
return DllCall("Wininet.dll\InternetGetConnectedState", "Str", flag, "Int", 0)
}
window_active(ByRef window) {
return WinActive(window)
}
window_activate(ByRef window) {
window_show(window)
WinActivate, %window%
}
window_exist(ByRef window) {
return WinExist(window)
}
window_show(byRef window) {
WinShow, %window%
}
window_minimize(byRef window) {
WinMinimize, %window%
}
is_green(ByRef bgr) {
r := bgr & 0xff
g := ((bgr >> 8) & 0xff) - 32 ;variation allowed
b := (bgr >> 16) & 0xff
return g > r && g > b
}
is_yellow(ByRef bgr) {
r := bgr & 0xff
if (r < 120)
return false
g := (bgr >> 8) & 0xff
if (g < 120)
return false
d := abs(g - r)
if (d > 32 + 32)
return false
d += ((bgr >> 16) & 0xff) + 16
return r > d && g > d
}
is_red(ByRef bgr) {
r := (bgr & 0xff) - 32
g := (bgr >> 8) & 0xff
b := (bgr >> 16) & 0xff
return r > b && r > g
}
is_blue(ByRef bgr) {
r := bgr & 0xff
g := (bgr >> 8) & 0xff
b := ((bgr >> 16) & 0xff) - 32 ;variation allowed
return b > r && b > g
}
find_image(ByRef image_file_name, ByRef found_x := 0, ByRef found_y := 0) {
return image_search(image_file_name, 0, 0, A_ScreenWidth, A_ScreenHeight, found_x, found_y)
}
find_image_in(ByRef image_file_name, ByRef x_beg, ByRef y_beg, ByRef x_end, ByRef y_end, ByRef found_x := 0, ByRef found_y := 0) {
return image_search(image_file_name, x_beg, y_beg, x_end, y_end, found_x, found_y)
}
image_search(ByRef image_file_name, ByRef x_beg, ByRef y_beg, ByRef x_end, ByRef y_end, ByRef found_x := 0, ByRef found_y := 0) {
ImageSearch, found_x, found_y, x_beg, y_beg, x_end, y_end, *0 %image_file_name%
if (ErrorLevel)
return false ;ErrorLevel: 1 - image not found 2 - file not found
return true
}
drag_random(ByRef x1_min, ByRef x1_max, ByRef y1_min, ByRef y1_max, ByRef x2_min, ByRef x2_max, ByRef y2_min, ByRef y2_max) {
x1 := random(x1_min, x1_max)
y1 := random(y1_min, y1_max)
x2 := random(x2_min, x2_max)
y2 := random(y2_min, y2_max)
SendEvent {Click %x1%, %y1%, down}{Click %x2%, %y2%, up}
}
click_random(ByRef x_min, ByRef x_max, ByRef y_min, ByRef y_max, ByRef mouse_button) {
x := random(x_min, x_max)
y := random(y_min, y_max)
SendEvent {Click %x%, %y%, %mouse_button%}
}
click(byRef x, byRef y, byref mouse_button) {
SendEvent {Click %x%, %y%, %mouse_button%}
}
send_array(ByRef array) {
for index, element in array
send_sleep(element, random(40, 90))
}
send_string(ByRef str) {
array := StrSplit(str)
array.push("{Enter}")
send_array(array)
}
is_suspended() {
return A_IsSuspended
}
is_paused() {
return A_IsPaused
}
on_off_tooltip(ByRef script_name, byRef window, ByRef tool_tip_id := 1) {
win_get_pos(x, y, width, height, window)
tool_tip_x := width - 165
tool_tip_y := height - (1 + tool_tip_id) * 40
state := (is_suspended()) ? "Off" : "On"
tool_tip(script_name . " " . state, tool_tip_x, tool_tip_y, tool_tip_id)
}
tray_tip(ByRef title := " ", ByRef message := " ", ByRef duration := 2000, ByRef tray_icon := 1) {
TrayTip, %title%, %message%, %duration%, %tray_icon%
}
tool_tip(ByRef message, ByRef x, ByRef y, ByRef tool_tip_id := 1) {
Tooltip, %message%, %x%, %y%, %tool_tip_id%
}
pixel_get_color(ByRef x, ByRef y) {
PixelGetColor, bgr, % x, % y
return %bgr%
}
win_get_pos(byRef x, byref y, byref width, byref height, byref window) {
WinGetPos, x, y, width, height, %window%
}
;~^!s::
;Suspend
;return
;~^!e::
;ExitApp
;return