Jump to content

Question on AutoIt Firefox script


Recommended Posts

Hey guys, I am a newbie to AutoIt, but I confess I got carried away by some of your examples.

Lately I tried to change the Notepad Autoscript to make a Firefox test scrip that opens firefox and then closes it.

My firefox is in PT language, but I don't think this is a problem because the shortcuts are correct.

Can someone tell me what is wrong in my script please?

; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
Local $answer = MsgBox(4, "AutoIt Example (English Only)", "This script will run Firefox and then quit.  Run?")

; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If $answer = 7 Then
MsgBox(0, "AutoIt", "OK.  Bye!")
Exit
EndIf

; Run Firefox
ShellExecute("C:Program FilesMozilla Firefoxfirefox.exe")
;MsgBox(0, "Debug Msg", "Running FF")

; Wait for the Firefox become active
WinWaitActive("Firefox")
MsgBox(0, "Debug Msg", "WinWait")
; Now quit by pressing Alt-f and then x (File menu -> Exit)
Send("!f")
Send("s")
MsgBox(0, "Debug Msg", "Quitting FF")

; Now wait for Notepad to close before continuing
WinWaitClose("Firefox")

; Finished!

I also checked the API for WinWaitActive, but I don't get how I can use each one of the separate modes. Can someone help me understand what I have wrong?

Thanks in advance!

Edited by Fl4m3Ph03n1x
Link to comment
Share on other sites

The default mode for WinTitleMatchMode will match a partial title string from the start. Check your Firefox title. I'll bet it starts with "Mozilla Firefox". So if you change your code to

; Wait for the Firefox become active
WinWaitActive("Mozilla Firefox")

it will work.

Now notice that your next statement is MsgBox.

MsgBox(0, "Debug Msg", "WinWait")
; Now quit by pressing Alt-f and then x (File menu -> Exit)
Send("!f")
Send("s")
MsgBox(0, "Debug Msg", "Quitting FF")

This will steal focus and your Send commands won't go to Firefox. Before you issue Send commands, use WinActivate() to be sure your intended target gets the keystroke.

MsgBox(0, "Debug Msg", "WinWait")
; Now quit by pressing Alt-f and then x (File menu -> Exit)
WinActivate("Mozilla Firefox")
"Send("!f")
Send("s")  ; BTW, shouldn't this be an "x"?
MsgBox(0, "Debug Msg", "Quitting FF")

Also, you can use Alt-F4 (Send("!{F4}") to exit most any Windows app.

Edited by bstjohn
Link to comment
Share on other sites

Put this at the top of your script:

Opt("WinTitleMatchMode", 2)

that'll probably resolve your issue. (look it up in the Help file for description)

And, Welcome to the Forum!

Thanks for the welcome!

Btw, I tried that and it didn't work =(

@bstjohn:

I tried your solution but it didn't work :S

It is ALT+S because S is for "Sair" which means "Exit" in PT/BR and PT/PT. Looks like you just learned a new word today ;)

I replaced it by ALT+F4 but I am still doing something wrong :S

@Bellicus:

Thanks for the link, but this is merely an academic exercise that I am failing to complete :S

I am willing to learn, but I can't see what's wrong. I am also using MsgBox as a debug mechanism, is there a better way?

New version:

;
; AutoIt Version: 3.0
; Language:    English
; Platform:    Windows 7
; Author:        Fl4m3Ph03n1x
;
; Script Function:
;   Opens Firefix, and then quits.
;

Opt("WinTitleMatchMode", 2)

; Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
Local $answer = MsgBox(4, "AutoIt Example (English Only)", "This script will run Firefox and then quit.  Run?")


; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If $answer = 7 Then
    MsgBox(0, "AutoIt", "OK.  Bye!")
    Exit
EndIf


; Run Firefox
ShellExecute("C:Program FilesMozilla Firefoxfirefox.exe")

; Wait for the Firefox become active
WinWaitActive("Mozzila Firefox")
;MsgBox(0, "Debug Msg", "WinWait")

; Now quit by pressing Alt-f and then x (File menu -> Exit)
WinActivate("Mozilla Firefox")
Send("!{F4}")
;MsgBox(0, "Debug Msg", "Quitting FF")

WinActivate("Mozilla Firefox")
; Now wait for Notepad to close before continuing
WinWaitClose("Mozilla Firefox")


; Finished!
Edited by Fl4m3Ph03n1x
Link to comment
Share on other sites

Will I be damned ... it actually worked. I am confused though, according to the tutorial it should have worked only with the word "Firefox" because "Mozilla Firefox" contains "Firefox".

Oh well, I guess that's somehow related to Opt("WinTitleMatchMode", 2) which I don't fully understand.

Thanks for the help guys!

PS: Can I give you karma or something for the help?

Link to comment
Share on other sites

AutoItSetOption

WinTitleMatchMode

Alters the method that is used to match window titles during search operations.

1 = Match the title from the start (default)

2 = Match any substring in the title

3 = Exact title match

4 = Advanced mode, see <a href="../../../autoit3/docs/intro/windowsadvanced.htm">Window Titles & Text (Advanced)

-1 to -4 = force lower case match according to other type of match.

Edited by somdcomputerguy

- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

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...