Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VBScript to control Notepad

In connection with another, very different project, I am trying to write a VBScript that will, when executed, do the following:

  1. Open an instance of Notepad (as a hidden or minimized window)
  2. Bring the instance of Notepad into focus
  3. Write "Hello world" into the open notepad file
  4. Issue the appropriate alt+S etc. commands to save the file on the Desktop as "PrototypeText.txt"
  5. Close Notepad.

My biggest points of confusion are in steps "2" and especially "4" above. I know how to do all the other things in VBScript. Can anyone help me with these two steps?

Lastly, I realize that there are much more practical ways of accomplishing this, such as AutoHotKey, or writing directly to the *.txt file, in some other language perhaps. Please understand that the above program is a proof-of-concept for another project, not a deliverable in itself.

Your time spend responding is greatly appreciated. A link (that I haven't found yet) that specifies how to do "2" and "4" above would work as well.

like image 787
the_photon Avatar asked Oct 26 '25 05:10

the_photon


2 Answers

In WSH object if you are running vbs in that environment (as is likely) then use appactivate. But a hidden window can't be activated so ...

Again sendkeys in the WSH object can do 4 (but not in a hidden window).

Here's a sample from vbscript help (https://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.84).aspx) using both commands and note using sleep to give apps the chance to process commands.

     set WshShell = WScript.CreateObject("WScript.Shell")
     WshShell.Run "calc"
     WScript.Sleep 100
     WshShell.AppActivate "Calculator"
     WScript.Sleep 100
     WshShell.SendKeys "1{+}"
     WScript.Sleep 500
     WshShell.SendKeys "2"
     WScript.Sleep 500
     WshShell.SendKeys "~"
     WScript.Sleep 500
     WshShell.SendKeys "*3"
     WScript.Sleep 500
     WshShell.SendKeys "~"
     WScript.Sleep 2500

Minimised windows can get the focus.

Use a string like this to get the desktop. Include quotes."%userprofile%\desktop\yourfilename.txt". Quotes are in case there are any spaces.

Also AppActivate returns a code. True if it can or is activated. You can use this to test for dialogs by apactivating the dialog title. It's really Window activating.

like image 85
bgalea Avatar answered Oct 29 '25 09:10

bgalea


Assuming you don't want to change/keep adding to this text file via scripting while it's open the below should work.

Dim objFSO, objFileToWrite, WShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WShell = CreateObject("WScript.Shell")

'Create Text File and write to it then close it    
Set objFileToWrite = objFSO.CreateTextFile("C:\Users\***YOUR USER ID HERE***\Desktop\PrototypeText.txt", 1)
objFileToWrite.Write "Hello World"
objFileToWrite.Close

'Open newly created and saved text file
WShell.Run("Notepad.exe C:\Users\***YOUR USER ID HERE***\Desktop\PrototypeText.txt") 
like image 25
ABrunette Avatar answered Oct 29 '25 08:10

ABrunette