[Function][v1/v2] CloseOnOpen - Close specified windows.

Post your working scripts, libraries and tools
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

[Function][v1/v2] CloseOnOpen - Close specified windows.

08 Mar 2017, 09:14

Introduction.
Closing pop-up windows seems to be a recurring issue in the askfor help section. Often the solution is SetTimer and/or WinWait. This is an attempt to make an easy-to-use function, without the need for a timer.

Download.

AHK v1:
CloseOnOpen.ahk
(1.34 KiB) Downloaded 157 times
AHK v2:
CloseOnOpen_v2.ahk
(1.48 KiB) Downloaded 57 times
Documentation.
Closes specified windows when they are created.

Code: Select all

CloseOnOpen([wParam:="",lParam:="",msg:="",hwnd:=""])
Parameters
wParam
Can be an array of WinTitles to be closed on their creation, or the word add in which case the lParam should be a string representing a WinTitle to add to an earlier specified array of WinTitles, and, to remove an already added WinTitle, specifiy the string remove and let lParam be the WinTitle to be removed. WinTitle can also be an array with WinExist parameters, i.e., ["WinTitle", "WinText", "ExcludeTitle", "ExcludeText"]. Example, wParam:=["ahk_exe calc.exe",["ahk_exe notepad.exe","","a.txt"]]. Added 2017-03-13: You can now pass funcobjects or boundfuncs. An example is available a few posts down.
lParam
If wParam is either add or remove, let this parameter be a WinTitle. Example, wParam:="remove", lParam:="ahk_exe notepad.exe"
msg
Not used by user
hwnd
Not used by user

Example.

Code: Select all

#Include CloseOnOpen.ahk
list:=["ahk_exe notepad.exe", "ahk_exe calc.exe"]
CloseOnOpen(list)				; Specify a list
CloseOnOpen("add", "HelloGuiA")	; Add to the list
1::Run, notepad					; Press 1,2 and 3, the  windows should close quickly
2::Run, calc
3::	
	Gui A: new
	Gui A: show, w200 h100, HelloGuiA
return

guiclose:
	Gui,destroy
return
Esc::Exitapp					; Press escape to exit the script.
Example for v2,

Code: Select all

#Include CloseOnOpen.ahk
list:=["ahk_exe notepad.exe", "ahk_exe calc.exe"]
CloseOnOpen(list)				; Specify a list
CloseOnOpen("add", "HelloGuiA")	; Add to the list
1::Run("notepad")				; Press 1,2 and 3, the  windows should close quickly
2::Run("calc")
3::	
	gui:=guiCreate(,"HelloGuiA")
	gui.onEvent("Close", objBindMethod(gui,"destroy"))
	gui.show("w200 h100")
return
esc::exitapp()					; Press escape to exit the script.
Limitations and known issues.
  • A windows title seems to be updated a short while after the function is called, a work around is available below.
  • Doesn't seem to detect hidden windows.
  • Works when, A top-level, unowned window has been created. (source)
  • Briefly tested. The AHK v2 version is only tested on the example
Refrences.
Cheers.
Last edited by Helgef on 03 Nov 2017, 13:46, edited 3 times in total.
User avatar
jNizM
Posts: 2661
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: [Function] CloseOnOpen - Close specified windows.

08 Mar 2017, 09:49

Thanks for sharing =)
How about hosting your stuff on GitHub?
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

Re: [Function] CloseOnOpen - Close specified windows.

08 Mar 2017, 10:15

Here's a rather primitive alternative , I tend to write a lot of those,Too LAZY :), for those who don't want to so much as touch 'code'.

https://autohotkey.com/boards/viewtopic.php?f=6&t=26150
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Function] CloseOnOpen - Close specified windows.

08 Mar 2017, 10:25

Consider every other quote to be my answer to the one above :D
jNizM wrote:How about hosting your stuff on GitHub?
brutus_skywalker wrote:Too LAZY :)
brutus_skywalker wrote:Here's a rather primitive alternative
jNizM wrote:Thanks for sharing =)
Cheers! :wave:
aaffe
Posts: 158
Joined: 16 Jan 2014, 04:23

Re: [Function] CloseOnOpen - Close specified windows.

09 Mar 2017, 03:10

doesnt work with the new calculator on windows 10.
Even not with
ahk_exe ApplicationFrameHost.exe
and not with
Rechner
as Title.
isnt the function able to see this window?
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Function] CloseOnOpen - Close specified windows.

09 Mar 2017, 03:13

That's curious, I'll investigate when I get time to fetch my win10 laptop. Thanks for the report.
aaffe
Posts: 158
Joined: 16 Jan 2014, 04:23

Re: [Function] CloseOnOpen - Close specified windows.

09 Mar 2017, 03:22

ok thank you. I think with win10 you cant automate some windows......
But perhaps you get it to go.
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Function] CloseOnOpen - Close specified windows.

09 Mar 2017, 03:31

I have very limited experience with win10, but I'm sure we'll manage to close the calculator. ;)
guest3456
Posts: 3134
Joined: 09 Oct 2013, 10:31

Re: [Function] CloseOnOpen - Close specified windows.

09 Mar 2017, 09:03

ApplicationFrameHost is used for all of the Metro apps (as they were called in Win8). Maybe they don't respond to the shellhook?

User avatar
Nextron
Posts: 1376
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: [Function] CloseOnOpen - Close specified windows.

09 Mar 2017, 09:55

So they could all just be child windows of a main process, not triggering the hook? Last year there was a thread about properly identifying those windows: https://autohotkey.com/boards/viewtopic.php?f=5&t=13818
Helgef wrote:Consider every other quote to be my answer to the one above :D
:lol: That's real world code re-usability.
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Function] CloseOnOpen - Close specified windows.

13 Mar 2017, 12:02

The hook is called when opening the calculator on win10, but apparently, it is now called calculator.exe. So adding ahk_exe calculator.exe to the list closes it. However, when starting the calculator, a fancy blue window shows up a second or so before the actual calculator comes up, this seems to be called ApplicationFrameHost.exe. So you have to look at that first. :roll:
The blue window seems to have the title Calculator, or Rechner (german I guess), but the title seems to be added a short while after the hook has been called, so you have to make a sleep to catch it. I don't think you should make a sleep in the OpenOnClose function though. I have added the option to pass a func object or boundfunc to list. Here is an example that closes the calculator on win10, quickly.

Code: Select all

#Include CloseOnOpen.ahk
;f:=func("CloseByTitle").Bind("Rechner")
f:=func("CloseByTitle").Bind("Calculator")
list:=[f]
CloseOnOpen(list)				; Specify a list
1::Run, calc.exe
Esc::Exitapp
CloseByTitle(title,calledByHook,hWnd:=0) {
    if (calledByHook) {
		timerFn:=func("CloseByTitle").Bind(title,0,calledByHook)
		SetTimer, % timerFn, -25	; This number might need to be changed.
		return 0
	}
	WinGetTitle, t, % "ahk_id " hwnd
	if (t && t=title)
		WinClose, % "ahk_id " hwnd
    return
}
I'll update the first post momentarily.
aaffe
Posts: 158
Joined: 16 Jan 2014, 04:23

Re: [Function] CloseOnOpen - Close specified windows.

14 Mar 2017, 02:41

This works, thank you!
Itai402

Re: [Function] CloseOnOpen - Close specified windows.

03 Nov 2017, 07:00

how do i use the WinExist parameters with a title like "Sidewise" (wich is a chrome extension that opens in a new window)? i thing i dont understand how to use the example.
thanks
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Function] CloseOnOpen - Close specified windows.

03 Nov 2017, 07:49

Hello, please try this CloseOnOpen(["Sidewise ahk_exe chrome.exe"]) :wave:

Edit: I don't have chrome, but I believe it is quite similar to opera, which I have. And on opera, when a window opens, there is quite a long delay before the window title is set. So matching window tiltes doesn't work well on opera. :(
Also, on opera, titles are in general title - Opera, so maybe you need Sidewise - Chrome. If you have issues, consider trying this, it is a modified version of the example on how to close the calculator which presented similar issues,

Code: Select all

f:=func("CloseByTitle").Bind({title:"Sidewise - Chrome",exe:"chrome.exe"})	; Note, remove " - Chrome" if not appropriate
list:=[f]
CloseOnOpen(list)				; Specify a list
Esc::Exitapp
CloseByTitle(title,calledByHook,hWnd:=0) {
    if (calledByHook) {
		timerFn:=func("CloseByTitle").Bind(title,0,calledByHook)
		SetTimer, % timerFn, -25	; This number might need to be changed.
		return 0
	}
	WinGetTitle, t, % "ahk_id " hwnd (title.exe?" ahk_exe " title.exe:"")
	if (t && t=title.title)
		WinClose, % "ahk_id " hwnd
    return
}

egocarib
Posts: 100
Joined: 21 May 2015, 18:21

Re: [Function] CloseOnOpen - Close specified windows.

03 Nov 2017, 09:47

Thanks, Helgef! I have done this for several windows using WinWaitActive and similar, but this is a much slicker (and faster-closing) alternative.

I made a minor tweak to allow using "add" without first creating a winlist (it previously threw a "no object to invoke" error):

Code: Select all

else if (wParam="add")
    return IsObject(winList) ? winList.Push(lParam) : !!(winList := [lParam])
itai402

Re: [Function] CloseOnOpen - Close specified windows.

03 Nov 2017, 10:40

my problem is not closing but making an axception for sidewise. when i use just the parameter "ahk_exe chrome.exe" it works perfectly, but i want to make an axception to sidewise because i simetimes like to close and open agin later the sidewise window and i dont want it to close back up.
thw sidewise window has the exect same titles as all the other chrome windows except the uniqeid that changes every time i close and reopen any chrome window.
the two solutions you sugested didn't do anything
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Function][v1/v2] CloseOnOpen - Close specified windows.

03 Nov 2017, 11:21

Hello egocarib :wave:.
Thank you very much for your suggestion, I have updated the main post with a version for AHK v2, it includes your check :thumbup:

Cheers.
Helgef
Posts: 4459
Joined: 17 Jul 2016, 01:02
Contact:

Re: [Function] CloseOnOpen - Close specified windows.

03 Nov 2017, 13:41

itai402 wrote:my problem is not closing but making an axception for sidewise. when i use just the parameter "ahk_exe chrome.exe" it works perfectly, but i want to make an axception to sidewise because i simetimes like to close and open agin later the sidewise window and i dont want it to close back up.
thw sidewise window has the exect same titles as all the other chrome windows except the uniqeid that changes every time i close and reopen any chrome window.
the two solutions you sugested didn't do anything
Hello, I missed your post, probably because you are not registred, it took a while before a moderator aproved it.
I'm not sure I follow, is the title of the window Sidewise - Chrome, and you do not want to close it? If so, please try this example, it should close new chrome windows, except if the title is Sidewise - Chrome

Code: Select all

#Include CloseOnOpen.ahk
f:=func("CloseByTitle").Bind({title:"Sidewise - Chrome",exe:"chrome.exe"})	; Note, remove " - Chrome" if not appropriate
list:=[f]
CloseOnOpen(list)				; Specify a list
Esc::Exitapp
CloseByTitle(title,calledByHook,hWnd:=0) {
    if (calledByHook) {
		timerFn:=func("CloseByTitle").Bind(title,0,calledByHook)
		SetTimer, % timerFn, -50	; This number might need to be changed.
		return 0
	}
	WinGetTitle, t, % "ahk_id " hwnd (title.exe?" ahk_exe " title.exe:"")
	if (t && t!=title.title)
		WinClose, % "ahk_id " hwnd
    return
}
Cheers.
itai402

Re: [Function][v1/v2] CloseOnOpen - Close specified windows.

03 Nov 2017, 18:38

hi, thanks for the response. acording to the window spy the title is "‪Sidewise‬". the script doesn't exclude the the sidewise window with or wothout the " - Chrome". any ideas?
and version 2 doesn't launch

Return to “Scripts and Functions”

Who is online

Users browsing this forum: gwarble, pepoluan and 20 guests