Question

I'm new to AutoIt and have a program mostly working.

Objective: Open a website in a browser and then close it after a given period of time (1.5 hours).

Script:

;Open a command prompt to start chrome from.
Run("C:\WINDOWS\system32\cmd.exe")
WinWaitActive("C:\WINDOWS\system32\cmd.exe")
send('"c:\Program Files\Google\Chrome\Application\chrome.exe" http://website.com' & "{ENTER}")
;Close the dos box
WinWaitActive("C:\WINDOWS\system32\cmd.exe")
send('exit' & "{ENTER}")
; 5400000 = 1.5 hours
sleep( 5400000 );
WinWaitActive("Listen Live - Google Chrome","")
;Send("{F4}{ALTUP}")
Send("{CTRLDOWN}{SHIFTDOWN}q{SHIFTUP}{CTRLUP}")

The opening works just fine but the script never closes. It appears to work when the timer is very short but not when it is that long.

The script will be executed via Windows Scheduled tasks. I've tried separating the open/close into two separate scripts but that didn't seem to work either.

I'm open to alternative solutions that will work on Windows.

Was it helpful?

Solution

Changing the last WinWaitActive to WinActivate might resolve your issue, but getting the window handle and closing via that would probably be a better way.

Also, be certain that the window title you are looking for really exists. I noticed my Google Chrome window titles do not actually have " - Google Chrome" in them. Confirm this by opening up the web page and the use the Auto Window Info application. That application will show you the exact Window title and other information when you click/activate a window. And you don't have to have an exact match. For example, if the window title may differ after "Listen Live - xxxx", then just look for "Listen Live -".

Try the code below and see how it works:

; set variables
$browser_exe = "c:\Program Files\Google\Chrome\Application\chrome.exe"
$URL = "http://website.com"
$window_title = "Listen Live - Google Chrome"
$sleep_time = 5400000 ; 1.5 hours

; run the browser and pass the URL to it
Run($browser_exe & " " & $URL)

; get window handle
$window_handle = WinWait($window_title)

Sleep($sleep_time)

; close the window
WinClose($window_handle)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top