Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: WinHide

Hello experts,
I am trying to performed a WinHide however I don't understand why it doesn't work:
:?*:hhx::
WinGetActiveTitle,Title
If (InStr(Title,"Title"))
{
	Sleep,500
	PostMessage,0x112,0xF030,,,%Title%
	WinHide,%Title%
	Msgbox,,, WinHide has been performed
	Return
}
Else
MsgBox,4144,,WinHide cannot be performed as the title of ActiveWindows doesn't match.
Return

Open in new window


When I performed the same opertation with:
WinClose, %Title%

Open in new window

Thank you for your help.
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Hi Luis,

Several things:

(1) You are looking for the word Title in the title of the active window. Is that really what you want to be looking for?

(2) When matching a window title in any AutoHotkey code, the setting of SetTitleMatchMode is crucial, as follows:

SetTitleMatchMode,1 ; starts with
SetTitleMatchMode,2 ; contains
SetTitleMatchMode,3 ; matches exactly

That is documented here:
https://www.autohotkey.com/docs/commands/SetTitleMatchMode.htm

(3) Keep in mind that window titles are case sensitive (except when using the i) modifier in a RegEx).

(4) The Sleep,500 is not needed there (no reason to sleep following either the WinGetActiveTitle or the InStr).

(5) The PostMessage maximizes the window. Just curious why you're doing that if the goal is to hide it.

Regards, Joe
Avatar of Luis Diaz

ASKER

Thank you Joe,
I am with my cell so I cannot provide you a deep comment.
1-I put this title as an example however I made the test with another title and it works as I get the message within the if condition.
2-Set Title match more can be putted within the instr or wingetactivetitle or at the beginning of my ahk? If so could you please provide an example?
4-Agree
5-I putted a winmaximize to make sure that I am acting in the targeted windows to make sure that winmaximize works and to show that winhide doesn't for this case.
Hi Luis,

> I made the test with another title and it works as I get the message within the if condition.

That's good.

> Set Title match more can be putted within the instr or wingetactivetitle or at the beginning of my ahk? If so could you please provide an example?

Not within the InStr. It is simply another command that should be executed prior to needing its value. For example, your AutoHotkey code might look like this:

SetTitleMatchMode,1 ; starts with
Title:="Welcome to Experts Exchange"
WinHide,%Title%

Open in new window

Or like this:

SetTitleMatchMode,2 ; contains
Title:="Experts Exchange"
WinHide,%Title%

Open in new window

Or this:

SetTitleMatchMode,3 ; matches exactly
Title:="Welcome to Experts Exchange - Google Chrome"
WinHide,%Title%

Open in new window


> I putted a winmaximize to make sure that I am acting in the targeted windows to make sure that winmaximize works and to show that winhide doesn't for this case.

Thanks for explaining. Of course, WinHide will work with the proper value in the title parameter and the proper setting in SetTitleMatchMode. Regards, Joe
Noted Joe. Thank you for your comment.
If I choose instr to put and if and else condition in case that the windows that I want to hide is not active.
How can I put an else in your previous proposals?
Thank you for your help.
> If I choose instr to put and if and else condition in case that the windows that I want to hide is not active.

I don't understand. There can be only one active window. Are you saying that you want to hide the active window, but only if it has a specific title? Or one of several possible titles?

> How can I put an else in your previous proposals?

Let's not worry about the AutoHotkey code now...simply explain to me what you are trying to achieve.

Regards, Joe
Hi Joe,
I am writing this message from my cell.
The idea is to check if there is a windows which contains a title string.
Example: Welcome to Experts Exchange. If so hide it. Else msgbox,,, Windows to hide hasn't been found.
> The idea is to check if there is a windows which contains a title string.

Any window that exists or must it be the active window?
Any windows that exists.
Only the first window found with a matching title or all windows with a matching title?
All windows with a matching title.
Exact match or Contains match?

Case sensitive or insensitive?
Contains and insensitive.
Here's an AutoHotkey code snippet that does what you want:

StringCaseSense,Off ; case insensitive
HideTitle:="partial title of windows to hide" ; title must contain this - case insensitive
WinGet,AllWindows,List ; get list of all windows that exist
NumWin:=AllWindows ; number of all windows
Loop,%NumWin% ; loop through all windows
{
  Handle:=AllWindows%A_Index% ; get the handle of each window
  WinGetTitle,Title,ahk_id %Handle% ; get the title of each window
  If (InStr(Title,HideTitle)) ; "contains" match and case insensitive
    WinHide,%Title% ; hide the window if it matches
}
MsgBox,4160,Finished,Done hiding windows that contain this in the title:`n`n%HideTitle%

Open in new window

Regards, Joe
Thank you. Unable to test it right now. I will keep you informed.
Joe, just some remarks.
If I want to add to my ahk file:
Return should be placed in between {} and at the end of line?
Stringcasesensitive should be also placed after the hotsring. No risk for other ahk located in my ahk file?
> Return should be placed in between {} and at the end of line?

No. Place it after the MsgBox.

>  No risk for other ahk located in my ahk file?

There's always a risk for other hotkeys/hotstrings when you set global values in any particular hotkey/hotstring. Thus, it's good programming practice to make sure that for any given hotkey/hotstring, values are set appropriately, such as CoordMode, SetTitleMatchMode, StringCaseSense, etc.

Using the hotstring from your initial post, here's the full AutoHotkey script for you:

:?*:hhx::
HideTitle:="partial title of windows to hide" ; title must contain this - case insensitive
WinGet,AllWindows,List ; get list of all windows that exist
NumWin:=AllWindows ; number of all windows
NumHidden:=0 ; number of windows hidden
Loop,%NumWin% ; loop through all windows
{
  Handle:=AllWindows%A_Index% ; get the handle of each window
  WinGetTitle,Title,ahk_id %Handle% ; get the title of each window
  If (InStr(Title,HideTitle)) ; "contains" match and case insensitive
  {
    WinHide,%Title% ; hide the window if it matches
    NumHidden:=NumHidden+1 ; increment number of windows hidden
  }
}
MsgBox,4160,Number Hidden: %NumHidden%,Done hiding windows that contain this in the title:`n`n%HideTitle%
Return

Open in new window

Note that I enhanced the closing MsgBox to show in the title bar the number of windows that were hidden. Regards, Joe
Thank you very much Joe. I will test it and keep you informed.
Sounds good!
Joe,

I tested your proposal and it works! Thank you again for your help.

I would like to go beyond and take the opportunity of this question to have the opposite approach.
Instead of using winhide I would like to apply winshow with the same parameters:
Loop all windows and check titles that matches with case insensitive.

Thank you in advance for your help.
> I tested your proposal and it works!

Great news...thanks for letting me know.

> Instead of using winhide I would like to apply winshow with the same parameters

It will work if you simply change the WinHide line to WinShow, as follows:

WinShow,%Title% ; show the window if it matches

However, for clarity/documentation purposes, you should change variable names and comments. Here's a revised AutoHotkey script that does it:

:?*:ssx::
ShowTitle:="partial title of windows to show" ; title must contain this - case insensitive
WinGet,AllWindows,List ; get list of all windows that exist
NumWin:=AllWindows ; number of all windows
NumShown:=0 ; number of windows shown
Loop,%NumWin% ; loop through all windows
{
  Handle:=AllWindows%A_Index% ; get the handle of each window
  WinGetTitle,Title,ahk_id %Handle% ; get the title of each window
  If (InStr(Title,ShowTitle)) ; "contains" match and case insensitive
  {
    WinShow,%Title% ; show the window if it matches
    NumShown:=NumShown+1 ; increment number of windows shown
  }
}
MsgBox,4160,Number Shown: %NumShown%,Done showing windows that contain this in the title:`n`n%ShowTitle%
Return

Open in new window

I made the hotstring ssx, but, of course, make it whatever you want. Regards, Joe
Thank you Joe.
I made some tests. Please find attached the video.
When I execute second AutoHotkey I don't get the windows in front I supposed that I need to add winactivate after winshow?
Thank you again for your help.
Hide-show-windows_20190926_053030.7z
ASKER CERTIFIED SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much for this update Joe.
Unable to test it right know.
I will keep you informed.
OK, I'll be around most of today if you have a problem, but I think it's solid now...tested here in W7 with two Notepad windows. The reason for the bug in my earlier AutoHotkey code is clearly stated at the WinGet,List doc:
https://www.autohotkey.com/docs/commands/WinGet.htm#List

Note this comment there (copied here under "Fair Use"):
Hidden windows are included only if DetectHiddenWindows has been turned on.
Regards, Joe
Thank you very much Joe.
I will test proposals and keep you informed.
Thank you for your help.
Tested and it works!
Thank you very much for your help.
Great news! You're very welcome, Luis, and thanks back to you for letting me know that it works. Regards, Joe
Hi Luis,
I'm cleaning up some open EE questions and found this one. Your last post said that you tested it and it works, but since you haven't marked it as solved yet, I'm wondering if there any issues with it. If so, let me know. Regards, Joe
Hi Joe,
Sorry. My mistake. I just marked as solved.
Thanks!