The Complete Script
So let's review the script. I have added comments to document the script ? to make it more readable and easy to follow. You can use almost anything that isn't a recognised script command for comments. There is a reserved word called Remark which can be used:
Remark>This is a comment
But I've used // to indicate my comments. Some people like to use **. Use what you feel comfortable with ? as long as it isn't a recognised script command ? or it will try to execute your comments. Which could be interesting!
Remember to modify the section of the script that does the printing depending on whether or not your Notepad displays the Print dialog. There are clever ways that we could make the script generic to cope with both cases, but that's for a later discussion. But if you're interested look at the WW_TIMEOUT variable that you can set for WaitWindowOpen, and the IfWindowOpen command.
If you have any problems running this script you may want to slow it down a bit. We have made it run as fast as it possibly can, but some systems react too slowly for windows to be ready in time for the script. Consider adding Wait statements at key places, such as before a SetFocus, or before sending keystrokes:
//This will wait 2 seconds.
Wait>2
//Set the name of our file
Let>%USERDOCUMENTS_DIR%\sample.txt
//Start Notepad
Run>notepad.exe
WaitWindowOpen>Untitled - Notepad
//Construct the text
Let>firstline=This is the first line of my notepad file.
Let>secondline=This is line two.
Let>thirdline=This is the third and final line.
Let>entiretext=%firstline%%CR%%secondline%%CR%%thirdline%
//Introduce a small delay in between keystrokes
Let>SK_DELAY=20
//Send the text to Notepad
Send>entiretext
//Save As
Press ALT
Send>fa
Release ALT
WaitWindowOpen>Save As
//Delete the file if it already exists
IfFileExists>filename
DeleteFile>filename
Endif
//Send the filename to the Save As dialog
SetFocus>Save As
Send>filename
Press Enter
//Print the file
SetFocus>notepad*
Press CTRL
Send>p
Release CTRL
//Remove the next two lines if your version of
//Notepad does not display the Print dialog
WaitWindowOpen>Print
Press Enter
//Wait for the print status dialog to finish
WaitWindowOpen>Notepad
WaitWindowClosed>Notepad
//Now close Notepad
SetFocus>Notepad*
Press ALT
Send>fx
Release ALT