Jump to content

How do you close an IE tab?


luger
 Share

Go to solution Solved by luger,

Recommended Posts

 

Fun times...

#include <IE.au3>

Const $ie_new_in_tab = 0x0800
$oIE = _IECreate("http://www.autoitscript.com")
_IELoadWait($oIE)
__IENavigate($oIE, "http://www.autoitscript.com/forum/", 1, 0x0800)

; [n] = the object to the tab
Global $ga_IETabs = 0
$ga_IETabs = __IE_TabLoadWait($oIE, "http://www.autoitscript.com/forum/", "url")
If @error Then Exit 101

Local $go_NewTab = $ga_IETabs[1] ; I know it's only returning 1
_IEQuit($go_NewTab)


; this will return the objects of the tabs matching type/str and wait if need be
Func __IE_TabLoadWait(ByRef $o_obj, $s_str = "", $s_type = "", $n_msecexpire = 0)

    If Not IsObj($o_obj) Then
        Return SetError(1, 0, 0)
    EndIf

    Local $a_objret = 0
    Local $n_timer = TimerInit()

    While 1
        $a_objret = __IE_GetTabObjs($o_obj, $s_str, $s_type)
        If IsArray($a_objret) Then ExitLoop

        ; return if we set the wait timer and it's reached
        If $n_msecexpire > 0 Then
            If $n_timer >= $n_msecexpire Then
                Return SetError(2, 0, 0)
            EndIf
        EndIf
        Sleep(250)
    WEnd

    If Not IsArray($a_objret) Then
        Return SetError(3, 0, 0)
    EndIf
    
    ; set normal _ieloadwait default
    If $n_msecexpire < 1 Then $n_msecexpire = -1

    For $i = 1 To UBound($a_objret) -  1
        _IELoadWait($a_objret[$i], 0, $n_msecexpire)
    Next
    
    ; this could return multiple if there is more than 1 tab
    ;   with the same url/title for $s_type passed
    Return $a_objret
EndFunc

; returns all the instances of tabs matching type/str criteria
Func __IE_GetTabObjs(ByRef $o_obj, $s_str = "", $s_type = "")

    If Not IsObj($o_obj) Then
        Return SetError(1, 0, 0)
    EndIf

    Local $a_otabs[101], $i_add = 0, $i_next = 1, $o_ieattach
    $s_type = StringLower($s_type)

    While 1
        ; we start at instance 1 and enum as we find more instances
        $o_ieattach = _IEAttach($o_obj, "instance", $i_next + 1)
        If @error = $_IEStatus_NoMatch Or Not IsObj($o_ieattach) Then ExitLoop
        
        ; control array size
        If Mod($i_add + 1, 100) = $i_add Then
            ReDim $a_otabs[$i_add + 101]
        EndIf

        Switch StringLower($s_type)
            Case "url"
                If String(Execute("$o_ieattach.locationurl")) = $s_str Then
                    $i_add += 1
                    $a_otabs[$i_add] = $o_ieattach
                EndIf
            Case "title"
                If WinGetText(String(Execute("$o_ieattach.hwnd"))) = $s_str Then
                    $i_add += 1
                    $a_otabs[$i_add] = $o_ieattach
                EndIf
            Case Else
                $i_add += 1
                $a_otabs[$i_add] = $o_ieattach
        EndSwitch
        $i_next += 1
    WEnd

    If $i_add = 0 Then Return SetError(2, 0, 0)

    ReDim $a_otabs[$i_add + 1]
    $a_otabs[0] = $i_add

    Return $a_otabs
EndFunc

Thank you.. :) It works .. :)  

There are just some warnings.. :)

 

PE98Qv.png

Edited by luger
Link to comment
Share on other sites

  • Moderators

Warnings can be turned off.  They are there for your debugging.

My version ensures that the tab/item you're attaching too is a tab from the object you are attempting to receive the object from.

_IEAttach() works for you, that's great, but if there is another object out there with that url, you're not ensured to get the object you think you are getting.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Warnings can be turned off.  They are there for your debugging.

My version ensures that the tab/item you're attaching too is a tab from the object you are attempting to receive the object from.

_IEAttach() works for you, that's great, but if there is another object out there with that url, you're not ensured to get the object you think you are getting.

Okey.. :) I read your article.. :) I realized as I can understand .. :)

__IENavigate($oIE, "http://www.autoitscript.com/forum/", 1, 0x0800) ;Not waiting for page to load.. :( This part does not work at all..

instead:

$oIE.Navigate("http://www.autoitscript.com/forum/", 0x0800) ;Does not give any warning.. :)
Edited by luger
Link to comment
Share on other sites

  • Moderators

The warnings are built into IE.au3, DaleHohm wrote them in there.  That's why accessing the properties/methods directly from the object doesn't show a warning/error.

Run this script and you'll see what I'm talking about.

#include <IE.au3>

_IEErrorNotify(0)

Const $ie_new_in_tab = 0x0800
$oIE = _IECreate("http://www.autoitscript.com")
_IELoadWait($oIE)
__IENavigate($oIE, "http://www.autoitscript.com/forum/", 1, 0x0800)

; [n] = the object to the tab
Global $ga_IETabs = 0
$ga_IETabs = __IE_TabLoadWait($oIE, "http://www.autoitscript.com/forum/", "url")
If @error Then Exit 101

Local $go_NewTab = $ga_IETabs[1] ; I know it's only returning 1
_IEQuit($go_NewTab)


; this will return the objects of the tabs matching type/str and wait if need be
Func __IE_TabLoadWait(ByRef $o_obj, $s_str = "", $s_type = "", $n_msecexpire = 0)

    If Not IsObj($o_obj) Then
        Return SetError(1, 0, 0)
    EndIf

    Local $a_objret = 0
    Local $n_timer = TimerInit()

    While 1
        $a_objret = __IE_GetTabObjs($o_obj, $s_str, $s_type)
        If IsArray($a_objret) Then ExitLoop

        ; return if we set the wait timer and it's reached
        If $n_msecexpire > 0 Then
            If $n_timer >= $n_msecexpire Then
                Return SetError(2, 0, 0)
            EndIf
        EndIf
        Sleep(250)
    WEnd

    If Not IsArray($a_objret) Then
        Return SetError(3, 0, 0)
    EndIf

    ; set normal _ieloadwait default
    If $n_msecexpire < 1 Then $n_msecexpire = -1

    For $i = 1 To UBound($a_objret) -  1
        _IELoadWait($a_objret[$i], 0, $n_msecexpire)
    Next

    ; this could return multiple if there is more than 1 tab
    ;   with the same url/title for $s_type passed
    Return $a_objret
EndFunc

; returns all the instances of tabs matching type/str criteria
Func __IE_GetTabObjs(ByRef $o_obj, $s_str = "", $s_type = "")

    If Not IsObj($o_obj) Then
        Return SetError(1, 0, 0)
    EndIf

    Local $a_otabs[101], $i_add = 0, $i_next = 1, $o_ieattach
    $s_type = StringLower($s_type)

    While 1
        ; we start at instance 1 and enum as we find more instances
        $o_ieattach = _IEAttach($o_obj, "instance", $i_next + 1)
        If @error = $_IEStatus_NoMatch Or Not IsObj($o_ieattach) Then ExitLoop

        ; control array size
        If Mod($i_add + 1, 100) = $i_add Then
            ReDim $a_otabs[$i_add + 101]
        EndIf

        Switch StringLower($s_type)
            Case "url"
                If String(Execute("$o_ieattach.locationurl")) = $s_str Then
                    $i_add += 1
                    $a_otabs[$i_add] = $o_ieattach
                EndIf
            Case "title"
                If WinGetText(String(Execute("$o_ieattach.hwnd"))) = $s_str Then
                    $i_add += 1
                    $a_otabs[$i_add] = $o_ieattach
                EndIf
            Case Else
                $i_add += 1
                $a_otabs[$i_add] = $o_ieattach
        EndSwitch
        $i_next += 1
    WEnd

    If $i_add = 0 Then Return SetError(2, 0, 0)

    ReDim $a_otabs[$i_add + 1]
    $a_otabs[0] = $i_add

    Return $a_otabs
EndFunc

See, no warnings/errors ;).

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

The warnings are built into IE.au3, DaleHohm wrote them in there.  That's why accessing the properties/methods directly from the object doesn't show a warning/error.

Run this script and you'll see what I'm talking about.

#include <IE.au3>

_IEErrorNotify(0)

Const $ie_new_in_tab = 0x0800
$oIE = _IECreate("http://www.autoitscript.com")
_IELoadWait($oIE)
__IENavigate($oIE, "http://www.autoitscript.com/forum/", 1, 0x0800)

; [n] = the object to the tab
Global $ga_IETabs = 0
$ga_IETabs = __IE_TabLoadWait($oIE, "http://www.autoitscript.com/forum/", "url")
If @error Then Exit 101

Local $go_NewTab = $ga_IETabs[1] ; I know it's only returning 1
_IEQuit($go_NewTab)


; this will return the objects of the tabs matching type/str and wait if need be
Func __IE_TabLoadWait(ByRef $o_obj, $s_str = "", $s_type = "", $n_msecexpire = 0)

    If Not IsObj($o_obj) Then
        Return SetError(1, 0, 0)
    EndIf

    Local $a_objret = 0
    Local $n_timer = TimerInit()

    While 1
        $a_objret = __IE_GetTabObjs($o_obj, $s_str, $s_type)
        If IsArray($a_objret) Then ExitLoop

        ; return if we set the wait timer and it's reached
        If $n_msecexpire > 0 Then
            If $n_timer >= $n_msecexpire Then
                Return SetError(2, 0, 0)
            EndIf
        EndIf
        Sleep(250)
    WEnd

    If Not IsArray($a_objret) Then
        Return SetError(3, 0, 0)
    EndIf

    ; set normal _ieloadwait default
    If $n_msecexpire < 1 Then $n_msecexpire = -1

    For $i = 1 To UBound($a_objret) -  1
        _IELoadWait($a_objret[$i], 0, $n_msecexpire)
    Next

    ; this could return multiple if there is more than 1 tab
    ;   with the same url/title for $s_type passed
    Return $a_objret
EndFunc

; returns all the instances of tabs matching type/str criteria
Func __IE_GetTabObjs(ByRef $o_obj, $s_str = "", $s_type = "")

    If Not IsObj($o_obj) Then
        Return SetError(1, 0, 0)
    EndIf

    Local $a_otabs[101], $i_add = 0, $i_next = 1, $o_ieattach
    $s_type = StringLower($s_type)

    While 1
        ; we start at instance 1 and enum as we find more instances
        $o_ieattach = _IEAttach($o_obj, "instance", $i_next + 1)
        If @error = $_IEStatus_NoMatch Or Not IsObj($o_ieattach) Then ExitLoop

        ; control array size
        If Mod($i_add + 1, 100) = $i_add Then
            ReDim $a_otabs[$i_add + 101]
        EndIf

        Switch StringLower($s_type)
            Case "url"
                If String(Execute("$o_ieattach.locationurl")) = $s_str Then
                    $i_add += 1
                    $a_otabs[$i_add] = $o_ieattach
                EndIf
            Case "title"
                If WinGetText(String(Execute("$o_ieattach.hwnd"))) = $s_str Then
                    $i_add += 1
                    $a_otabs[$i_add] = $o_ieattach
                EndIf
            Case Else
                $i_add += 1
                $a_otabs[$i_add] = $o_ieattach
        EndSwitch
        $i_next += 1
    WEnd

    If $i_add = 0 Then Return SetError(2, 0, 0)

    ReDim $a_otabs[$i_add + 1]
    $a_otabs[0] = $i_add

    Return $a_otabs
EndFunc

See, no warnings/errors ;).

Yes you are right.. :) Thank you very much.. :) I understand you.. :)

I have difficulty understanding English.. :D

Best answer was very nice.. Thanks again.. :D

Edited by luger
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...