Sign in to follow this  
Followers 0
Caravelle

Send Won't Send ?

9 posts in this topic

It's been a long time since I last wrote an AutoIt, script, and I have even less hair and brain cells now. I simply need to automatically shut down and restart a data collection program once a day while I am away. This looks like it should be a no-brainer, there is even a simple example using Notepad to copy, and the NotePad example works on my Vista laptop. However, my test script fails miserably...

Run("C:\BaseStation\BaseStation.exe") 
 WinWaitActive("BaseStation SBS1")
 WinClose("BaseStation SBS1")
 WinWaitActive("Confirm", "Do you really want to shut BaseStation down?")
 Send("!Y")

In the final script the first line will be transferred to the end, followed by a Sleep() for 24 hours, and the whole lot will be put in a loop to run as many times as the number of days I'm away. I start the script at midnight the day before I leave, and when I come back I should have a neat set of day-long data sets, ready to be analysed.

So what happens ? BaseStation.exe opens Just fine (line 1). It starts the closedown procedure just fine (line 3). It opens a TMessageForm window with header "Confirm" (as, er, confirmed by Window Info, containing the exact text "Do you really want to shut BaseStation down?" and two buttons, Yes and No. However, whatever I try to send to the MessageBox, nothing happens, and the script doesn't terminate I've tried "Y", "y", "!Y", "!y" and "ENTER, all of which do the job from the keyboard.

All I can see in the way of error reports is a line in the bottom pane of the Editor saying "C:\AutoIt3\SciTE\..\autoit3.exe" /ErrorStdOut "C:\Users\Public\AutoIt Scripts\SBSrestart.au3". I don't have a clue what that means apart from the fact that there is an error somewhere. The message appears before BaseStation.exe even opens.

So, sorry to be a pain, but there has to be something obvious I'm missing. Many thanks to anyone who can tell me what it is.

Caravelle

Share this post


Link to post
Share on other sites



See the ControlSend() function in the Helpfile. This may help.


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

Share this post


Link to post
Share on other sites

This is info can be found in the AutoIt help file, next time try to check there first.

To hold a key down

Send("{a down}") ;Holds the A key down

Send("{a up}") ;Releases the A key

So try this:

Send("{! down}")

Send("y")

Send("{! up}")

Also I think this might work:

Send("!{y}")

Hope that one of these works for you!

TJ.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks to all.

John wrote:

What happens if the script is not in a loop?Does it exit, or hang waiting for final message window?

The script is as you saw it, the looping bit will be added later. It does not exit, it's waiting for something.

Somdcomputerguy wrote:

See the ControlSend() function in the Helpfile. This may help.

It may... but nothing I've tried so far has worked.

I tried the following without any luck:

ControlSend ( "Confirm", "", 2393982, "y")
ControlClick("Confirm", "", "[ID:2393982]")
ControlClick("Confirm", "", 2393982)
ControlClick("Confirm", "", "[CLASS:TButton; TEXT:&Yes; INSTANCE:2]")
Finally, in desperation, I tried
MouseClick("left", 600, 436)
and it worked. This would be a practical solution as if no-one touches the laptop while I'm away the confirm dialog will always appear in the same place, but I'd still like to know why the theoretically more reliable approaches don't work. I believe the BaseStation program was written using Delphi, could it be that the dialogs are not to the Windows standard ?

Now we come to the solution without using MouseClick...

TJ123 wrote:

This is info can be found in the AutoIt help file, next time try to check there first.To hold a key downSend("{a down}") ;Holds the A key downSend("{a up}") ;Releases the A key

All too often one is told to search the Help but that's not much use if one is unfamiliar with the terms and doesn't know what to look for. Help files often require considerable familiarity with the subject. I have been writing programs in Delphi 7 for 10 years and I still have trouble finding what I need in the help. I can see from the help on "Send" that Send("{a down}") ;Holds the A key down etc. I don't have to hold the key down when using the keyboard, nor do I need to use ALT: - "y" or Enter does just fine as the Yes button has focus by default. So no matter how many times I read the Help on Send I would never have realised that this might be relevant - however, the suggested:

Send("{! down}")Send("y")Send("{! up}")

works like magic ! :guitar: Though I have no idea why this downing and upping business should be needed when it wasn't needed for the NotePad example.

Here is my final script. My plan changed slightly, I'll start it after shutting down the BaseStation program manually. Of course, it will take a couple of weeks to test fully to completion, but I've tried it with a 1 minute sleep rather than a 1 day sleep, and it works just fine.

; script to stop and restart BaseStation.exe every 24 hours
;Program BaseStation.exe will not be running when this script starts
For $i = 1 to 15; number of days to run  
  Run("C:\BaseStation\BaseStation.exe")  
Sleep(86400000) ; milliseconds in a day - exact time will drift a bit but that doesn't matter  
  WinClose("BaseStation SBS1")  
  WinWaitActive("Confirm", "")  
  Send("{! down}")  
  Send("y")  
  Send("{! up}")   
; BaseStation now takes a while to tidy up its sqlite database, save its data and close  
  WinWaitClose ("BaseStation SBS1", "", 60)
Next
; now return to normal operation in case I forget to restart manually  
Run("C:\BaseStation\BaseStation.exe")  
WinWaitActive("BaseStation SBS1")  
Sleep(10000)  
MsgBox(4160, "SBSrestart terminated.", "This script has restarted BaseStation " & String($i) & " times." , 0)

So, many thanks again, I'd forgotten how useful AutoIt can be. There may be odd inconsistencies in the formatting above, for some reason the message posted itself before I'd finished, and when I tried to edit it everything went haywire !

Caravelle

Edited by Caravelle

Share this post


Link to post
Share on other sites

ControlClick("Confirm","","[CLASS:TButton; INSTANCE:2]","main")
should work. No idea why the program is resisting automation.

Try nabbing the handle of the window before trying to click the button. I know it sounds like extra steps, but stranger things have happened.

$hwnd_Confirm = WinWaitActive("Confirm","",30)
If $hwnd_Confirm = 0 Then
    ConsoleWrite("Timeout occured waiting for Confirm window." & @CRLF)
    Exit
EndIf
ControlClick($hwnd_Confirm,"","[CLASS:TButton; INSTANCE:2]","main")

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

or Send("!y") ?

You can try ControlClick("BaseStation SBS1", "", "TButton2")

First make sure that "BaseStation SBS1" actually works for a window name

If WinWaitActive("BaseStation SBS1", "", 5) Then
  ConsoleWrite("YES" & @CRLF)
Else
  ConsoleWrite("NO" & @CRLF)
EndIf

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites

The script is as you saw it, the looping bit will be added later. It does not exit, it's waiting for something.

That means that this line

WinWaitActive("Confirm", "Do you really want to shut BaseStation down?")

Is the culprit, the window is either not becoming active or it is not being detected.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Thanks to Blue_Drache. I'll try, and see what happens, though I have it working now with TJ123's simple solution (even if I don't understand why it works).

Thanks to Mechaflash, though I'm not sure what you're saying. I tried !y (see my original post). The Button isn't on the BaseStation window, it's on a modal MessageBox called "Confirm". And we know that "BaseStation SBS1" works as a Window name, otherwise it would not produce the "Confirm" MessageBox when the script tries to close it.

And thanks to JohnOne, who wrote "'WinWaitActive("Confirm", "Do you really want to shut BaseStation down?")' is the culprit, the window is either not becoming active or it is not being detected." I tried it with and without the text attribute. Strange that the script works with TJ123's answer, which suggests that the window is indeed being detected and becoming active, it just won't respond to the usual Send() commands in a straightforward way.

Oh well.

Caravelle

Caravelle

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