Jump to content

Need help rewriting my script


Recommended Posts

So I have 0 coding experience so I pretty much just copied and pasted parts of this script together that I've made. It works, but it doesn't work the best.

This is script:

#include <file.au3>
Dim $aSite
If Not _FileReadToArray("websites.txt",$aSite) Then
   MsgBox(4096,"Error", " Error reading file to Array    error:" & @error)
   Exit
EndIf
While 1=1
$x = Random(1, $aSite[0], 1)
ShellExecute("C:\Program Files\Mozilla Firefox\firefox.exe", $aSite[$x])
; Msgbox(0,'Random Site', $aSite[$x])
Sleep(Random(45000, 180000, 1))
Send("!{F4}")
Sleep(5000)
Wend

Basicly what its suppose to do is open a random url from the url list I have in websites.txt. Then it waits for 45000-180000 and closes firefox and then the script just loops after that. Like I said, it works but every once in awhile it will hang when it starts over and I get this error message:

Firefox is already running, but is not responding. To open a new window, you must first close the existing Firefox process, or restart your system.

the script still runs so the window gets closed and the script starts again.

I'm pretty sure this is an issue with my poorly coded script because I've messed with firefox settings for days and I still get that same error.

If someone could help me pinpoint the problem and also rewrite my script I would greatly appreciate it.

Link to comment
Share on other sites

Welcome to AutoIt and the forum!

Seems that Send either does not reach the Firefox window or Firefox displays a popup (like: Do you really want to leave this site?") and your script does not handle this.
Then FF is still running when you open the next instance.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You might try using ProcessExists and ProcessClose to make sure FF is not running before your ShellExecute command

#include <file.au3>
Dim $aSite
If Not _FileReadToArray("websites.txt",$aSite) Then
   MsgBox(4096,"Error", " Error reading file to Array    error:" & @error)
   Exit
EndIf
While 1=1
$x = Random(1, $aSite[0], 1)
If ProcessExists("firefox.exe") Then ; Check if FireFox process is running.
   ProcessClose ( "firefox.exe" )
EndIf
ShellExecute("C:\Program Files\Mozilla Firefox\firefox.exe", $aSite[$x])
; Msgbox(0,'Random Site', $aSite[$x])
Sleep(Random(45000, 180000, 1))
Send("!{F4}")
Sleep(5000)
Wend

 

Link to comment
Share on other sites

Oh also 1 more thing, I run this script 24/7 but every 12hrs I need to switch to a different websites.txt file. So what I have done is made a copy of the script but used a different websites.txt file. So every 12hrs I stop the original and start the copy script. Is there anyway I can put this all in one script so I don't have to use 2 different scripts.

Link to comment
Share on other sites

kalelreturns,

I try this...

#include <file.au3>
#include <date.au3>

local $aSite, $sTime = _nowcalc(), $currsites = "websites1.txt"
If Not _FileReadToArray($currsites, $aSite) Then
    MsgBox(4096, "Error", " Error reading file to Array    error:" & @error)
    Exit
EndIf
While 1 = 1
    $x = Random(1, $aSite[0], 1)
    If ProcessExists("firefox.exe") Then ; Check if FireFox process is running.
        ProcessClose("firefox.exe")
        ; wait for process to close
        While ProcessExists("firefox.exe")
            Sleep(100)
        WEnd
    EndIf
    ShellExecute("C:\Program Files\Mozilla Firefox\firefox.exe", $aSite[$x])
    ; Msgbox(0,'Random Site', $aSite[$x])
    Sleep(Random(45000, 180000, 1))
    Send("!{F4}")
    Sleep(5000)

    ; switch site files every 12 hours
    if _DateDiff('h',$stime,_NowCalc()) > 11 then
        $currsites = ( $currsites = 'websites1.txt' ? 'websites2.txt' : 'websites1.txt' )
        $stime = _nowcalc()
        _FileReadToArray($currsites, $aSite)
        ConsoleWrite('Sites switched to ' $currsites & @CRLF)
    endif

WEnd

Note: I added code to make sure that FF process closes.   Not tested as I don't use FF

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Should be:

Consolewrite('sites switched to ' & $currsites & @CRLF)

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

That was an easy one :)

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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