Sign in to follow this  
Followers 0
hima

Script executes only once

11 posts in this topic

Hi,

I have written a test script to open a notepad document, send some text and close it. It executes only once. I close that document by clicking on the X button. The second time I run it, it doesnt open another notepad document. Could you please advice?

Share this post


Link to post
Share on other sites



#cs ----------------------------------------------------------------------------
 
 AutoIt Version: 3.3.10.2
 Author:         myName
 
 Script Function:
Template AutoIt script.
 
#ce ----------------------------------------------------------------------------
 
; Script Start - Add your code below here
Run ("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is some text" & "This is on a new line! ")
WinClose("Untitled - Notepad")
WinWaitActive("Notepad","Do you want to save")
Send("!n")
 
Thank you for replying. This is the script I am trying to execute.

Share this post


Link to post
Share on other sites

I am able to run it by double clicking on the script. When I try to run it from SciTE using F5, it doesnt execute more than once.

Share this post


Link to post
Share on other sites

That is because it doesn't complete, it is stuck waiting at the following line:

WinWaitActive("Notepad","Do you want to save")

Try this instead:

Run ("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is some text" & "This is on a new line! ")
WinClose("Untitled - Notepad")
WinWaitActive("Notepad", "")
Send("!n")

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
I changed the script to :
 
Run ("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is some text" & "This is on a new line! ")
WinClose("Untitled - Notepad")
WinWaitActive("Notepad", "")
Send("!n")
 
Its the same. I am able to run it by double clicking. Tools --> Go in SciTE is grayed out.

Share this post


Link to post
Share on other sites

Not sure how, it started working now. When I press F5, its getting executed everytime.

What I noticed what there were many scripts running in the system tray. When I closed all of them, it started working.

Is it a bug. Or am I doing something incorrectly. Has anyone come across such an issue?

Share this post


Link to post
Share on other sites

I changed the script back to :

Run ("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is some text" & "This is on a new line! ")
WinClose("Untitled - Notepad")
WinWaitActive("Notepad","Do you want to save")
Send("!n")
 
And I am facing the same issue again. 
what does adding "Do you want to save" do there? How does it get stuck there.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

When you have multiple scripts interacting with the same application, with indefinite waits, it's first come first serve.

So, your particular run may not be the executor, and then waits until it can be.

This is not a bug, and expected, given how you scripted.

edit: your particurlar reason for failure, is that the text you think is visible, is not (on the do you want to save window).  It's probably painted, or on child window of the popup.

Run ("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("This is some text" & "This is on a new line! ")
WinClose("Untitled - Notepad")
WinWaitActive("[TITLE:Notepad;CLASS:#32770]")
Send("!n")

I would do something more like this, but even then, I would grab the window based on the PID of the Run, and not just blindly wait for any notepad window

$pid = Run ("notepad.exe")
$hwnd = WinWaitActive("Untitled - Notepad")
ControlSend($hwnd,"","Edit1", "This is some text" & @CRLF & "This is on a new line! ")
WinClose("Untitled - Notepad")
$hwnd2 = WinWaitActive("[TITLE:Notepad;CLASS:#32770]")
ControlClick($hwnd2,"","[CLASS:Button;TEXT:Do&n't Save]")

Better still:

#include <WinAPI.au3>

$pid = Run ("notepad.exe")
$hwnd = GetWindowMatchingProcess ($pid,"[CLASS:Notepad]")
If Not IsHWnd($hwnd) Then
    ConsoleWrite("Unable to find window in time" & @crlf)
    Exit 1
EndIf
WinActivate($hwnd)
$hControl = ControlGetHandle($hwnd,"","Edit1")
ControlSend($hwnd,"",$hControl, "This is some text" & @CRLF & "This is on a new line! ")
WinClose($hwnd)
$hwnd2 = WaitForEnabledPopup($hwnd)
If Not IsHWnd($hwnd2) Then
    ConsoleWrite("Unable to find popup in time" & @crlf)
    Exit 1
EndIf
ControlClick($hwnd2,"","[CLASS:Button;TEXT:Do&n't Save]")
Exit 0

Func GetWindowMatchingProcess($pid,$windowID,$iMaxWaitMilliSec = 2000)
    $iTimer = TimerInit()
    While TimerDiff($iTimer)<$iMaxWaitMilliSec
        $aWin = WinList($windowID)
        For $i = 0 To UBound($aWin)-1
            Local $tempPid = ""
            _WinAPI_GetWindowThreadProcessId($aWin[$i][1],$tempPid)
            If $tempPid = $pid Then
                Return $aWin[$i][1]
            EndIf
        Next
        Sleep (100)
    WEnd
    Return False
EndFunc

Func WaitForEnabledPopup($hWin,$iMaxWaitMilliSec = 2000)
    $hPopup = ""
    $iTimer = TimerInit()
    While TimerDiff($iTimer)<$iMaxWaitMilliSec
        $hPopup = _WinAPI_GetWindow($hWin,6)
        If IsHWnd($hPopup) Then
            Return $hPopup
        EndIf
    WEnd
    Return False
EndFunc
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

If the GO menu item is greyed out, then there is a script still running which needs to be terminated.

I'll hazard a guess that if you click on SciTE before the script terminates, the SciTE interface steals focus from notepad causing the script to hang waiting for the save dialog to become active (although it might be something else). I can't test this theory right now because I'm not on windows ATM, so don't quote me on this.

Edited by czardas

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Thank you for helping, everyone. It works now.

Edited by himaggi

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