Sign in to follow this  
Followers 0

Bring Firefox to front

32 posts in this topic

Posted (edited)

I would like to bring the Firefox window to the front and make it active. What I am trying to make work is:

$title = "MozillaWindowClass"
$exe = "Firefox.exe"
$toggle = 0

If WinActive($title) And $toggle Then
  
Else
    If WinExists($title) Then
        WinActivate($title)
    Else
        Run($exe)
    EndIf
    WinActivate($title)
EndIf

What shows in the Info is:

>>>> Window <<<<
Title: Posting New Topic - AutoIt Forums - Mozilla Firefox
Class: MozillaWindowClass
Position: 3, 8
Size: 1166, 735
Style: 0x16CF0000
ExStyle: 0x00000100
Handle: 0x00000000002103BC

>>>> Control <<<<
Class:
Instance:
ClassnameNN:
Name:
Advanced (Class):
ID:
Text:
Position:
Size:
ControlClick Coords:
Style:
ExStyle:
Handle:

>>>> Mouse <<<<
Position: 215, 23
Cursor ID: 0
Color: 0x219FD8

>>>> StatusBar <<<<

>>>> ToolsBar <<<<

>>>> Visible Text <<<<


>>>> Hidden Text <<<<

The information is pointing to the tab in firefox. I would like to bring to focus Firefox regargles of what tab is open.

Thanks,

Docfxit

Edited by docfxit

Share this post


Link to post
Share on other sites



Posted

WinActivate('[CLASS:MozillaWindowClass]')

Share this post


Link to post
Share on other sites

Posted (edited)

Thank you for the quick response...

I changed it to:

$title = "MozillaWindowClass"
$exe = "Firefox.exe"
$toggle = 0

If WinActive('[CLASS:MozillaWindowClass]') And $toggle Then

    If WinExists($title) Then
        WinActivate('[CLASS:MozillaWindowClass]')
    Else
        Run($exe)
    EndIf
    WinActivate('[CLASS:MozillaWindowClass]')
EndIf

And it doesn't work.

What am I doing wrong?

Thanks,

Docfxit

Edited by docfxit

Share this post


Link to post
Share on other sites

Posted (edited)

I have been able to get this to work:

$exe = "C:\Programs\FireFox\firefox.exe"
$appName = "[CLASS:MozillaWindowClass]"
$appHandle = WinGetHandle($appName, "")

;If WinActive("Firefox")  Then

    If WinExists($appName) Then
        WinActivate($appHandle)
    Else
        Run($exe)
    EndIf
    WinActivate($appHandle)
;EndIf


;EndIf

That does bring FireFox to the front. I haven't been able to get the If statement to work.

Docfxit

Edited by docfxit

Share this post


Link to post
Share on other sites

Posted (edited)

Well it's the structure of your code and the title string. What does this return?

MsgBox(4096, '', WinExists('[CLASS:MozillaWindowClass]'))

Edit: Seems you changed the layout of your code. Why not use ProcessExists instead?

Edited by guinness

Share this post


Link to post
Share on other sites

Posted

I have been able to get this to work:

 

That does bring FireFox to the front. I haven't been able to get the If statement to work.

Docfxit

You can't use the If statement the way you have it. That will only run the code inside of it if Firefox is already the active window. If it's not active, the code gets skipped. In your first code example, it would never run ever because you set $toggle to 0 and then the If statement is only run if both $toggle is True (1), AND Firefox is the currently active window. Since $toggle will never be True, it will never run.

Share this post


Link to post
Share on other sites

Posted

Thanks for the reply...

I tried Processexist It doen's seem to work

I just relized I don't need the if statement at all.

This is what I ended up with:

$exe = "C:\Programs\FireFox\firefox.exe"
$appName = "[CLASS:MozillaWindowClass]"
$appHandle = WinGetHandle($appName, "")

    If WinExists($appName) Then
        WinActivate($appHandle)
    Else
        Run($exe)
    EndIf
    WinActivate($appHandle)

Thanks a bunch,

Docfxit

Share this post


Link to post
Share on other sites

Posted

Depends how you used it, but glad you got it working. As I said it was your logic with the code.

Share this post


Link to post
Share on other sites

Posted

Thanks to you I got the $appName

so I could figure out the rest.

Thanks to BrewManNH for pointing out my logic was wrong. I did find that out just before you posted. Thanks for the post anyway.

It's working great now.

Docfxit

Share this post


Link to post
Share on other sites

Posted

When I use the code to bring Firefox to the front in a script it didn't work.

This is the entire script:

#include "File.au3"

; Autoit code to instll extensions and themes into firefox
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number
If _OSVersion() = "Win7" Or "Win7X64" Then
    $ProgramFiles = "C:\Programs"
Else
    $ProgramFiles = "C:\Program Files"
EndIf
;Declare Variables
$FireFoxDir = $ProgramFiles & '\FireFox\'
$FireFoxExe = $FireFoxDir & 'FireFox.exe'
$ScriptDir = "C:\Dnload\9xAddons"
$AddonsDir = @ScriptDir & '\FireFox Extensions\'
$AddonsFiles = _FileListToArray($AddonsDir)
For $i = 1 To $AddonsFiles[0]
    $AddonsFiles[$i] = "'" & $AddonsDir & $AddonsFiles[$i] & "'"
Next
$appName = "[CLASS:MozillaWindowClass]"
$appHandle = WinGetHandle($appName, "")
If WinExists($appName) Then
    WinActivate($appHandle)
    MsgBox(48, "Firefox", "Is Active")
    WinSetOnTop($appName, "", 1)

Else
    MsgBox(48, "Firefox", "Is Not Active")
    Run($FireFoxExe)
EndIf
WinActivate($appHandle)
WinSetOnTop($appName, "", 1)
MsgBox(48, "Firefox", "Should be Active")

For $i = 1 To $AddonsFiles[0]
    Switch StringRight($AddonsFiles[$i], 3)
        Case 'xpi'
            ;MsgBox(48, "File Number", $i)
            Send("{CTRLDOWN}o{CTRLUP}")
            WinWait("Open File", "Namespace Tree Contr")
            If Not WinActive("Open File", "Namespace Tree Contr") Then WinActivate("Open File", "Namespace Tree Contr")
            WinWaitActive("Open File", "Namespace Tree Contr")
            MsgBox(48, "File Nname", $AddonsFiles[$i])
            Send($AddonsFiles[$i], 1)
            Send("{ALTDOWN}o{ALTUP}")
            WinWait("Software Installation", "")
            If Not WinActive("Software Installation", "") Then WinActivate("Software Installation", "")
            WinWaitActive("Software Installation", "")
            ;            Sleep(2500)
        Case 'jar'
            ; This is not working
            ;RunWait($FireFoxExe & ' -install-global-theme "' & $AddonsFiles[$i] & '"', $FireFoxDir)
        Case Else
            Sleep(0)
    EndSwitch
    ;close firefox doesn't work
    ProcessClose('FireFox.exe *32')
Next
Exit
Func _OSVersion()
    Local $OS_Version, $servicepack_version
    $OS_Version = StringStripWS(StringRegExpReplace(@OSVersion, "(WIN_)|(Microsoft )|(Windows )|(\(TM\))|( Ultimate)", ""), 8)
    $OS_Version = StringRegExpReplace(StringRegExpReplace($OS_Version, '2008', 'Win7'), 'VISTA', 'Vista')
    If RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", 'CurrentBuildNumber') > 7000 Then $OS_Version = 'Win7'
    $servicepack_version = StringReplace(@OSServicePack, "Service Pack ", "SP")
    If @error = -1 Then $servicepack_version = ""
    ConsoleWrite($OS_Version & $servicepack_version & StringRegExpReplace(@OSArch, '(X86)', '') & @CRLF)
    Return $OS_Version & $servicepack_version & StringRegExpReplace(@OSArch, '(X86)', '')
EndFunc   ;==>_OSVersion

Please help me find some code that works to bring Firefox to the front.

Thanks,

Docfxit

Share this post


Link to post
Share on other sites

Posted

There's a macro, @OSVersion that would a lot simpler than your _OSVersion function.

BTW, other than not ,making FF the active window, what error checking have you done and where is it failing?

Share this post


Link to post
Share on other sites

Posted

There's a macro, @OSVersion that would a lot simpler than your _OSVersion function.

BTW, other than not ,making FF the active window, what error checking have you done and where is it failing?

They're using the macro already, but the logic is a little overcomplicated and @OSVersion doesn't return what they think it should.

MsgBox(4096, '', _OSVersion())

Func _OSVersion()
    Local $sOSVersion = StringReplace(@OSVersion, 'WIN_', '')
	$sOSVersion = StringReplace($sOSVersion, '2008', 'Win7')
	$sOSVersion = StringReplace($sOSVersion, 'VISTA', 'Vista')
	Return $sOSVersion & StringReplace(@OSServicePack, 'Service Pack ', 'SP') & StringReplace(@OSArch, '(X86)', '')
EndFunc   ;==>_OSVersion

Share this post


Link to post
Share on other sites

Posted

Right now I have a need to do two things.

1. Make FireFox the active window

2. Close FireFox

I have done way to much testing to post it here.

It's really very simple.

1. I have FireFox open

2. I have SciTE open

3. I run the script in SciTE with the F5 key.

4. I expect FireFox to become the active window so the included code will execute properly.

Thanks,

Docfxit

Share this post


Link to post
Share on other sites

Posted

Just quickly looking at your code, the assumption with operator usage is incorrect, for example, checking if the PC is Windows 7 is wrong, I have SP1 installed so this will always return the second option.

Local $sVersion = _OSVersion()
If $sVersion = 'Win7' Or $sVersion = 'Win7X64' Then
	MsgBox(4096, '', 'Windows 7')
EndIf

Also ProcessClose should be >>

ProcessClose('Firefox.exe')

Go back to basics, does the code in post #5 return 1? If yes then AutoIt is detecting Firefox.

Share this post


Link to post
Share on other sites

Posted

Thank you for the reply...

Just quickly looking at your code, the assumption with operator usage is incorrect, for example, checking if the PC is Windows 7 is wrong, I have SP1 installed so this will always return the second option.

Local $sVersion = _OSVersion()
If $sVersion = 'Win7' Or $sVersion = 'Win7X64' Then
	MsgBox(4096, '', 'Windows 7')
EndIf

Thank you for catching that. I haven't been using a 64bit win7 for a while. I tested it and fixed it in my code. I will fix it on this thread on my next post.

Also ProcessClose should be >>

ProcessClose('Firefox.exe')

I had it that way and it wasn't working. Probably because of something else. I switched it back and it works great now. Thanks.

Go back to basics, does the code in post #5 return 1? If yes then AutoIt is detecting Firefox.

Yes it does return a 1. Autoit is detecting Firefox. It just isn't bringing it to front for me. Why is the question.

Now if you could only figure out why it isn't making it the active window I will be all set.

Thank you very much,

Docfxit

Share this post


Link to post
Share on other sites

Posted

Well WinActivate should do that, but something is obviously taking focus from Firefox. I don't use Firefox so this all the help I can provide.

Share this post


Link to post
Share on other sites

Posted

Thanks for the help.

It would be nice if AutoIt had a way to show what program is currently taking focus.

Thanks,

Docfxit

Share this post


Link to post
Share on other sites

Posted (edited)

Help file >> WinGetTitle('[ACTIVE]')

Sleep(5000) ; Time to change focus of window.
MsgBox(4096, '', WinGetTitle('[ACTIVE]'))
Edited by guinness

Share this post


Link to post
Share on other sites

Posted

Help file >> WinGetTitle('[ACTIVE]')

Sleep(5000) ; Time to change focus of window.
MsgBox(4096, '', WinGetTitle('[ACTIVE]'))

This will get the title of SciTE (that's the program I use to run this script) You are correct this answers the question as I worded it.

You are correct. It will show me the current active program. It won't show me why FireFox can't take over the current active program.

Thanks,

Docfxit

Now If I could just figure out why Firefox won't change to the current active program I'd be all set.

Share this post


Link to post
Share on other sites

Posted

#include <array.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

$aHwnds_Firefox = WinList("[CLASS:MozillaWindowClass]")
_ArrayDisplay($aHwnds_Firefox)

$hWnd = WinGetHandle('[CLASS:MozillaWindowClass]')
$hWnd_RootOwner = _WinAPI_GetAncestor($hWnd, $GA_ROOTOWNER)
WinActivate($hWnd_RootOwner)

Share this post


Link to post
Share on other sites

Posted

Didn't think of that KaFu, thanks.

Share this post


Link to post
Share on other sites

Posted

#include <array.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

$aHwnds_Firefox = WinList("[CLASS:MozillaWindowClass]")
_ArrayDisplay($aHwnds_Firefox)

$hWnd = WinGetHandle('[CLASS:MozillaWindowClass]')
$hWnd_RootOwner = _WinAPI_GetAncestor($hWnd, $GA_ROOTOWNER)
WinActivate($hWnd_RootOwner)

That totally solved the problem. This is great.

Thank you very very much. I've been working on this for a long time. This fixed it.

Thank you,

Docfxit

Share this post


Link to post
Share on other sites

Posted

Great. Next time I will remember to ask the OP if they've checked the root owner. Firefox is strange.

Share this post


Link to post
Share on other sites

Posted

You're welcome, always a pleasure :).

Share this post


Link to post
Share on other sites

Posted

&nbsp;

That totally solved the problem. This is great.

Thank you very very much. I've been working on this for a long time. This fixed it.

Thank you,

Docfxit

&nbsp;

I've just tried the same thing, using KaFu's code. But it doesn't bring Firefox to the front. I just get an array that appears to be a list of Firefox tabs, i think.

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.