Skip to main content

NightWatchman Enterprise 7.3

An annotated power-off script

An annotated script for Notepad

This section provides an annotated guide for a simple script to handle Notepad.exe shutdowns. The problems dealt with are general ones, though obviously the shutdown procedures for some applications may be far more complex than the ones used for Notepad.

  1. Error checking.

    • All scripts should include an error checking section to catch errors and exit the script gracefully. As a convention, this is usually placed at the start of the power-off scripts.

      Sub ThrowError (Number, Description) 
         ObjApp = "" 
         WScript.Echo WScript.ScriptName + " " + CStr(number) + " " + description 
         NwmScript.Log WScript.ScriptName + " " + CStr(number) + " " + description 
         WScript.Quit number
      End Sub
  2. The main script body.

    • The main body of the script deals with manipulating the application in whatever state it may be in during a power-off.

  3. Initiating variables.

    • The first task is to initialize the variables used in the remainder of the script.

      hwnd = 0 '** window handle ** 
      debugx = 0
  4. Command-line arguments.

    • Two arguments are passed in by the service when the script is started – the handle to the window and a debug level parameter – this allows debugging to be enabled globally without changing the scripts.

      Set objArgs = WScript.Arguments
      For i = 0 To objArgs.Count – 1
         If objArgs = "-DEBUG" Then 
            debugx = 1 
         ElseIf UCase(Left(objArgs, 6)) = "-HWND=" Then 
            hWnd = Clng("&H" + right(objArgs, 8))
         Else 
            WScript.Echo "Usage: " + WScript.ScriptName + " [-hWnd=0xnnnnn] [-DEBUG]" 
            WSCript.Quit -2
         End If
      Next
  5. Initializing the script helper object.

    • Get the Script Helper object and initialize a few properties. Debug logging is normally off but can be turned on to help with debugging new scripts. Each call to the helper will be logged with its input parameters and the returned value. The backup directory is set to the user's profile application data area.

      '** get the NightWatchman script helper object **
      Set NwmScript = WScript.CreateObject("NWMSCRIPT.NWMScriptHelperCtrl.2") 
      NwmScript.logFileName = "c:\temp\nwmNotepad.log" 
      NwmScript.debug = debugx  '** can increase this to 3 for max logging ** 
      NwmScript.TimeoutSecs = 2 
      dumpdir = NwmScript.BackupDirectory
  6. Changing the default NightWatchman backup directory.

    • The NwmScript.BackupDirectory property returns the default location where the NightWatchman backups are saved. This is set by NightWatchman to:

      C:\documents and settings\<username>\application data\1e\NightWatchman, 

      where <username> is current user's logon name. If you want to change the save location for NightWatchman backups, change the code that sets the dumpdir variable. For example, to change the dumpdir variable to point to another location:

      dumpdir = "u:\temp"

      This example makes use of a user mapped drive u:. You can set this to any mapped drive the user may have set up on his computer. The advantage of using a user mapped drive is that they will be able to keep their NightWatchman backup files separate from all other users.

  7. Code to help with testing.

    • The following line is only used when writing/testing the scripts using "cscript.exe" . Normally hWnd is passed in by the service so there is no possibility of interacting with the wrong window.

      if hWnd = 0 Then hWnd = NwmScript.FindWindowLike ( 0, 0, "Notepad", "" )
      if hWnd = 0 Then ThrowError -1, "No Window" 
  8. getting the application process ID.

    • The NightWatchman Script Helper's ActivateApp method stores the process ID for the currently active application. This may be used subsequently to ensure that keystrokes are only sent to the application being scripted.

      NwmScript.ActivateApp ( hWnd )
  9. Closing active application dialogs.

    • Child and popup windows make it difficult for the script to continue – this function closes dialog boxes by clicking ESC, Cancel, Close or OK if that is the only option.

      NwmScript.CloseActiveAppDialogs ( "Notepad" )
  10. Getting the currently open filename.

    • For Notepad, the filename is extracted from the window caption.

      '** Get filename from window title ** 
      WndwName=NwmScript.GetWindowText ( hWnd ) 
      WScript.Echo ( WndwName ) 
      FileName = Replace( WndwName, " - Notepad", "" )
  11. Attempting to close the application.

    • Try and close the application by typing Alt-F4. If the application contains unsaved data, the popup window Do you want to save changes will appear. We cancel this for now by sending ESC.

      '** find out if it contains unsaved data ** 
      ExpectId = NwmScript.ExpectWindow("Notepad","","{ESC}","Do you want to save the changes") 
      NwmScript.SendKeys ( "%{F4}" ) '** Alt-F, 4 (Exit) ** 
      WScript.Sleep ( 1000 )
  12. Does the open file contain unsaved data.

    • ExpectCancel returns the number of times that the expected window has been seen – this is used to decide if the file contains unsaved data.

      UnSaved = NwmScript.ExpectCancel ( ExpectId )
      if UnSaved > 0 Then
  13. Handling the Save As dialog.

    • The Save As dialog box is brought up.

      NwmScript.SendKeys ( "%FA" ) '** Alt-F, A (Save As) ** 
      h = NwmScript.WaitForWindow ( 0, 0, "", "Save As" )
      if h > 0 Then 
  14. Creating a new file name.

    • The Script Helper creates a new filename using the input name as a base. The helper also makes sure that this file does not already exist as this could cause failures later. The extension and path parameters are optional but the file existence check cannot be performed without these.

      NewName = NwmScript.MakeNWMFilename ( FileName, ".txt", dumpdir ) 

      This new filename is sent to the Save As dialog.

            NwmScript.SendKeys ( "(" + dumpdir + NewName + "){WAIT_300}%S" ) 
            WScript.Echo " Saving to " + dumpdir + NewName
         end if
      end if
      
  15. Closing the application.

    • Notepad is closed.

      NwmScript.SendKeys ( "%{F4}" ) '** Alt-F, 4 (Exit) ** 
      WScript.Echo " Closing"
  16. Tidying up the environment.

    • Clean up. This section should be common to all scripts.

      WScript.Echo "Script Complete" 
      WScript.Quit 0