feltron_cq
feltron_cq

Reputation: 11

Closing specific Browser tabs in Internet Explorer programmatically from AHK script

I have an AHK script that creates multiple IE tabs, just for the purpose of visiting a number of URLs. At the end of the program, I would like to close all the tabs that have been opened, while leaving any tabs open that are not related to my program.

I've been struggling for a while to work out how to code this. WinClose seems to want to close IE down completely.

Would really value any assistance on this. I've been using AHK for a few years and it's my programming language of choice! I love what I can do with it! Especially how easy it is to compile code into a portable Executable!

Upvotes: 1

Views: 2740

Answers (2)

bgmCoder
bgmCoder

Reputation: 6370

It might be more than you are interested in doing, but you can use COM with Internet Explorer. AutoHotkey has a ComObjCreate() function. Using COM functions is like scripting Internet Explorer directly instead of hacking at it's GUI with window commands.

Check out this SE question.

Also, here is an Autohotkey thread for using COM.

Here is a COM example from the docs:

ie := ComObjCreate("InternetExplorer.Application")
ie.Visible := true  ; This is known to work incorrectly on IE7.
ie.Navigate("http://l.autohotkey.net/")

Here is Internet Explorer COM API.

Upvotes: 0

Grey
Grey

Reputation: 339

; Example is taken from here:
; http://www.autohotkey.com/board/topic/67438-navigate-to-javascript/#entry426699

VT_DISPATCH:=9, IID:="{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IHTMLWINDOW2
`(oIE:=ComObjCreate("InternetExplorer.Application")).visible:=True
oIE.navigate("http://stackoverflow.com/questions/16327523/"
           . "closing-specific-browser-tabs-in-internet-explorer-programmatically-from-ahk-scr")

While, oIE.ReadyState<>4 Or oIE.Busy
   Sleep, 500

oWindow:=ComObj(VT_DISPATCH, ComObjQuery(oIE, IID, IID), 1)

; http://msdn.microsoft.com/en-us/library/aa741489(v=vs.85).aspx
newWin:=oWindow.open("http://www.autohotkey.com", "_blank")
While, % newWin.document.readyState!="complete"
   Sleep, 500

MsgBox, 262144, % A_LineNumber, % newWin.document.title

; http://msdn.microsoft.com/en-us/library/aa741361(v=vs.85).aspx
newWin.close
MsgBox, 262144, % A_LineNumber, % oIE.document.title
oIE.quit
ExitApp

Upvotes: 0

Related Questions