Jump to content

Script executes only once


Recommended Posts

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?

Link to comment
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.
Link to comment
Share on other sites

  • Moderators

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")

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
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.
Link to comment
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?

Link to comment
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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • 6 years later...
On 5/2/2014 at 2:49 PM, JohnOne said:

Sure.

Post your script.

Hello Sir, 

My email id is *snip*

Need your help.. 

Very hopeful from you sir, kindly reply for this..

Want the code for below Scenario 

If a script is run once in a day it should not run twice that day. 

I have the logic, like script will search the Log file and if the same entry is available for the day for that task script will pop up a message that "task already run" and close the script. 

Edited by Jos
Link to comment
Share on other sites

Hello Friends, 

My email id is *snip*

Need your help.. 

Very hopeful from you all, kindly reply for this..

Want the code for below Scenario 

If a script is run once in a day it should not run twice that day. 

I have the logic, like script will search the Log file and if the same entry is available for the day for that task script will pop up a message that "task already run" and close the script. 

If the log file is excel file than it is best. 

Edited by Jos
Link to comment
Share on other sites

 

Hello Friends, 

My email id is *snip*

Need your help.. 

Very hopeful from you all, kindly reply for this..

Want the code for below Scenario 

If a script is run once in a day it should not run twice that day. 

I have the logic, like script will search in Log file and if the same entry is available for the day for that task script will pop up a message that "task already run" and close the script
If the log file is excel file than it is best.

Edited by Jos
Link to comment
Share on other sites

Welcome to AutoIt and the forum!

You could simply store the date of the last successful run in a file (that only holds this date).
Read this file to into a variable and compare to the current date ... voilá

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

Are you going to spam every forum section and old irrelated posts for long?

Do what @water said that I didn't have enough time to type before he posted.

 

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

You noticed that this topic is more than 6 years old and that the OP has been absent for this time?
So don't hold your breath for a reply.

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

10 minutes ago, water said:

You noticed that this topic is more than 6 years old and that the OP has been absent for this time?
So don't hold your breath for a reply.

I still hope that someone will help me. I need this code badly friend. 

Link to comment
Share on other sites

  • Jos locked this topic
Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...