In connection with another, very different project, I am trying to write a VBScript that will, when executed, do the following:
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With