Sign in to follow this  
Followers 0
ragiroti

Need help to exit Autoit script

13 posts in this topic

Hi All,

I am new to AutoIT and coding. So this might sound very stupid.

I have written a script to launch notepad++ and then enters few characters, with the intention to make the template and it works fine. The issue is when the application notepad++ is closed or minimized manually. At this point the script does not exit but starts writing the characters on different applications. The only way I could exit the script is from the Task manager :x. Please help me

The script:

Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
$answer = MsgBox(4, "ESTG v0.1 (English Only)", "IMP NOTE: Please ensure that Notepad++ is installed in the directory C:\Program Files\Notepad++\notepad++.exe. Also ensure to close all files opened in Notepad++ and close Notepad ++ before running the script. This script will launch Notepad++ and setup the template for AAAAAAAAAAA. Please note that the tool is still in developement phase. Run?")


; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If $answer = 7 Then
    MsgBox(0, "AAAAAAAAAAAAAaaa", "OK.  Bye!")
    Exit
EndIf

; Run Notepad++
Run("C:\Program Files\Notepad++\notepad++.exe")

; Wait for the Notepad become active - it is titled "Untitled - Notepad" on English systems
;WinWaitActive("[CLASS:Notepad]")


; Now that the Notepad window is active type some special characters
;Send("Sending some special characters:{ENTER 2}")

; Do it the first way
Send("=============================================================================================================================================================")
Send("{ENTER}")
Send ("SSSSSS {#} ")
$spnumber = InputBox("YYYYYY", "Please enter the  ""<1> <2> <3> <4> <5> <6>"" and click OK")
Send($spnumber)
Send("{ENTER}")
Send(" Test Sessions")
Send("{ENTER}")
Send("=============================================================================================================================================================")
Send("{ENTER}")
Send ("Environment:")
$environment = InputBox("Environment", "Please type in the Environment ""<PC> <Win 7> <Win XP>"" and click OK")
Send($environment)
Send("{ENTER}")

Send("=============================================================================================================================================================")
Send("{ENTER}")
Send ("------Enter Observations/Questions Here. Please add more points if required------")
Send("{ENTER}")
Send ("1.")
Send("{ENTER}")
Send ("2.")
Send("{ENTER}")
Send ("3.")
Send("{ENTER}")
Send ("4.")
Send("{ENTER}")
Send("=============================================================================================================================================================")
Send("{ENTER}")
Send("=============================================================================================================================================================")
Send("{ENTER}")
Send ("End of Session")
Send("{ENTER}")
Send("=============================================================================================================================================================")
MsgBox(0, "YYYY", "Please ensure to fill in End Time" & @CRLF & "Please ensure to fill in Duration" & @CRLF & "At the end of the session")
;Send("{ENTER}{ENTER}Finished")

; Finished!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Welcome to Autoit!

"Run" returns the PID (process ID) of the process that was launched. So you could use ProcessExists to check if notepad++ is still running.

$iPID = Run("C:\Program Files\Notepad++\notepad++.exe")
If ProcessExists($iPID) = 0 Then Exit
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for the quick reply Water.

I added your suggestion in the code but there is no difference. When I close the notepad++ manually during the script execution. The script starts writing characters from the script in any active window :x

Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
$answer = MsgBox(4, "ESTG v0.1 (English Only)", "IMP NOTE: Please ensure that Notepad++ is installed in the directory C:\Program Files\Notepad++\notepad++.exe. Also ensure to close all files opened in Notepad++ and close Notepad ++ before running the script. This script will launch Notepad++ and setup the template for AAAAAAAAAAA. Please note that the tool is still in developement phase. Run?")


; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If $answer = 7 Then
    MsgBox(0, "AAAAAAAAAAAAAaaa", "OK.  Bye!")
    Exit
EndIf

; Run Notepad++
$iPID = Run("C:\Program Files\Notepad++\notepad++.exe")
If ProcessExists($iPID) = 0 Then Exit

; Wait for the Notepad become active - it is titled "Untitled - Notepad" on English systems
;WinWaitActive("[CLASS:Notepad]")


; Now that the Notepad window is active type some special characters
;Send("Sending some special characters:{ENTER 2}")

; Do it the first way
Send("=============================================================================================================================================================")
Send("{ENTER}")
Send ("SSSSSS {#} ")
$spnumber = InputBox("YYYYYY", "Please enter the  ""<1> <2> <3> <4> <5> <6>"" and click OK")
Send($spnumber)
Send("{ENTER}")
Send(" Test Sessions")
Send("{ENTER}")
Send("=============================================================================================================================================================")
Send("{ENTER}")
Send ("Environment:")
$environment = InputBox("Environment", "Please type in the Environment ""<PC> <Win 7> <Win XP>"" and click OK")
Send($environment)
Send("{ENTER}")

Send("=============================================================================================================================================================")
Send("{ENTER}")
Send ("------Enter Observations/Questions Here. Please add more points if required------")
Send("{ENTER}")
Send ("1.")
Send("{ENTER}")
Send ("2.")
Send("{ENTER}")
Send ("3.")
Send("{ENTER}")
Send ("4.")
Send("{ENTER}")
Send("=============================================================================================================================================================")
Send("{ENTER}")
Send("=============================================================================================================================================================")
Send("{ENTER}")
Send ("End of Session")
Send("{ENTER}")
Send("=============================================================================================================================================================")
MsgBox(0, "YYYY", "Please ensure to fill in End Time" & @CRLF & "Please ensure to fill in Duration" & @CRLF & "At the end of the session")
;Send("{ENTER}{ENTER}Finished")

; Finished!
Edited by ragiroti

Share this post


Link to post
Share on other sites

ragiroti,

First a couple clarifications:

1 - you are not sending key strokes to "different applications", you are sending keystrokes to whatever app had focus when the Send was executed. I put a sleep(1000) to solve this on my PC.

2 - You can exit the script from the tray icon

Now, I'm sure that this can be made to work as a template generator, if that is your intent, however, I think you should consider other alternatives. The simplest of which is to make a common text skeleton file and read it into whatever app you are creating.

If you do not like the sleep alternative you csan certainly wait for the notepad++ window to become active using the technique that water alluded to.

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

Share this post


Link to post
Share on other sites

Firstly, tell you users not to close the application - Training issue!

Secondly, do as per water's suggestion, but the other way round - ie check the process exists and is active before each send [or group of sends] , else exit.

William

Share this post


Link to post
Share on other sites

I've changed the code a bit. Now it checks if notepad++ is installed or is already running.

The clipboard is used to enhance speed and to keep users from closing nodepad++ prematurely.

; Location of notepad++
$sNotePadProg = @ProgramFilesDir & "\Notepad++\notepad++.exe"

; Check if notepadd++ is already running
$aProcessList = ProcessList("notepad++.exe")
If IsArray($aProcessList) And $aProcessList[0][0] > 0 then
    MsgBox(0, "AAAAAAAAAAAAAaaa", "Notepad++ is still running. Please exit all notepad++ instances before running this script  Bye!")
    Exit
EndIf

;Prompt the user to run the script - use a Yes/No prompt (4 - see help file)
$answer = MsgBox(4, "ESTG v0.1 (English Only)", "IMP NOTE: This script will launch Notepad++ and setup the template for AAAAAAAAAAA. Please note that the tool is still in developement phase. Run?")
If Not FileExists($sNotePadProg) Then
    MsgBox(0, "AAAAAAAAAAAAAaaa", "Notepad++ is not installed.  Bye!")
    Exit
EndIf

; Check the user's answer to the prompt (see the help file for MsgBox return values)
; If "No" was clicked (7) then exit the script
If $answer = 7 Then
    MsgBox(0, "AAAAAAAAAAAAAaaa", "OK.  Bye!")
    Exit
EndIf

$spnumber = InputBox("YYYYYY", "Please enter the  ""<1> <2> <3> <4> <5> <6>"" and click OK")
If @error = 1 Then Exit ; User pressed the Cancel button
$environment = InputBox("Environment", "Please type in the Environment ""<PC> <Win 7> <Win XP>"" and click OK")
If @error = 1 Then Exit ; User pressed the Cancel button

$iPID = Run($sNotePadProg)
$Result = WinWaitActive("[CLASS:Notepad++]")
$Result = ControlClick("[CLASS:Notepad++]", "", "SysTabControl325")
$sString = _
"=============================================================================================================================================================" & @CRLF & _
"SSSSSS {#} " & $spnumber & @CRLF & _
"Test Sessions" & @CRLF & _
"=============================================================================================================================================================" & @CRLF & _
"Environment: " & $environment & @CRLF & _
"=============================================================================================================================================================" & @CRLF & _
"------Enter Observations/Questions Here. Please add more points if required------" & @CRLF & _
"1." & @CRLF & _
"2." & @CRLF & _
"3." & @CRLF & _
"4." & @CRLF & _
"=============================================================================================================================================================" & @CRLF & _
"=============================================================================================================================================================" & @CRLF & _
"End of Session" & @CRLF & _
"=============================================================================================================================================================" & @CRLF

ClipPut($sString )
$Result = Send("^v")

MsgBox(0, "YYYY", "Please ensure to fill in End Time" & @CRLF & "Please ensure to fill in Duration" & @CRLF & "At the end of the session")

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I'm writing a program that [inter alia] does what you're doing but in Word doc.

I use Melba23's excellent _ExtMessageBox udf * to open a pretty-coloured window in the top right of the scree [always on top] that covers the x-close box. When the user has finished editing the document, they click 'OK' in the box which reads the document content, does some other stuff and closes the word window. That puts the program in charge and as users don't need to close the app at all, they're less likely to close it prematurely.

Perhaps you'd find something similar to be helpful?

William

* link in M23's signature

Share this post


Link to post
Share on other sites

Thanks to all of you. I am able to fix the issue. This forums is great.

I would like to use Auto IT scripting more regularly now.

Cheers

:x

Share this post


Link to post
Share on other sites

Use ControlSend() instead of Send().

This way it will work also if Notepad is not active or minimized.

I tried to use ControlSend with Notepad++ - couldn't get it working.

The WindowInfo Tool returns:

>>>> Control <<<<
Class:  SysTabControl32
Instance:   5
ClassnameNN:    SysTabControl325
Name:   
Advanced (Class):   [CLASS:SysTabControl32; INSTANCE:5]
ID: 
Text:   Tab
Position:   0, 28
Size:   1280, 936
ControlClick Coords:    468, 177
Style:  0x5600E000
ExStyle:    0x00000000
Handle: 0x00040C2E

but I was not able to insert text :x


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I tried to use ControlSend with Notepad++ - couldn't get it working.

but I was not able to insert text :x

Post your code.

What did you send? Text or commands?

Text needs to be sent to Edit (ClassName) control.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Here's my code:

$iPID = Run(@ProgramFilesDir & "\Notepad++\notepad++.exe")
$iResult = WinWaitActive("[CLASS:Notepad++]")
ConsoleWrite("WinwaitActive: " & $iResult & "-" & @error & @CRLF)
$iResult = ControlClick("[CLASS:Notepad++]", "", "SysTabControl325")
ConsoleWrite("Controlclick: " & $iResult & "-" & @error & @CRLF)
Sleep(1000)
$iResult = ControlSend("[CLASS:Notepad++]", "", "SysTabControl325", "Test",1)
ConsoleWrite("ControlSend: " & $iResult & "-" & @error & @CRLF)

The script displays on the console:

>Running:(3.3.6.0):C:\Programme\AutoIt3\autoit3.exe "C:\TEMP\Test.au3"  
WinwaitActive: 0x001A075C-0
Controlclick: 1-0
ControLSend: 1-0

As the Window Info tools shows no Edit control I think AutoIt can't be used the same way as it works for Notepad (there is an EDIT1 control).

That's why I proposed to use the clipboard to enhance speed and reduce the posibility of inserting the data into the wrong window.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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