Mike86 Posted June 7, 2018 Posted June 7, 2018 (edited) Hi, Would it be possible to create a VBScript that will minimize/restore a CMD window, when executing a comment-line in a CMD file? Like: @ECHO OFF Code-Code-Code-Code-Code-Code-... "Scripts\MinRes.vbs" /minimize Code-Code-Code-Code-Code-Code-... "Scripts\MinRes.vbs" /restore Code-Code-Code-Code-Code-Code-... PAUSE EXIT Edited June 7, 2018 by Mike86
gunsmokingman Posted June 7, 2018 Posted June 7, 2018 The only way you might be able to do this is with VBS sendkeys method https://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx
jaclaz Posted June 7, 2018 Posted June 7, 2018 34 minutes ago, gunsmokingman said: The only way you might be able to do this is with VBS sendkeys method https://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx Which (simulated) keypresses need to be sent (to minimize/restore the command prompt window)? I would rather try Nircmd, cmdow or similar. jaclaz
Mike86 Posted June 7, 2018 Author Posted June 7, 2018 (edited) I'm already using the tool "SetConsole.exe" for this. But the stuff i do is for public free use and i try to avoid using any .exe files, so that people would feel more save using it. @gunsmokingman My VBScript skills are very low, do have something lying around that would do this? Edited June 8, 2018 by Mike86
Tripredacus Posted June 8, 2018 Posted June 8, 2018 20 hours ago, jaclaz said: Which (simulated) keypresses need to be sent (to minimize/restore the command prompt window)? jaclaz With focus, your typical CMD or program can be minimized using a keyboard like so: ALT+SPACE, N And to restore it is: ALT+SPACE, R Presuming focus is retained. If focus is lost, then any such script would need to select the UI object first before sending those keys. So it would probably be best if that method is used in both instances. BUT... I'm not sure that I am grasping the design of the application. If the desire is that some of the code is to be hidden from view so that the users do not see it, then shown again later when things are safe to view or as a message indicating the task is complete, then perhaps a different approach should be used instead.
Mike86 Posted June 8, 2018 Author Posted June 8, 2018 (edited) I want to minimize the first CMD window so that the second CMD window would not have the chance to hide behind the first CMD window. Edited June 8, 2018 by Mike86
jaclaz Posted June 8, 2018 Posted June 8, 2018 1 hour ago, Tripredacus said: With focus, your typical CMD or program can be minimized using a keyboard like so: ALT+SPACE, N And to restore it is: ALT+SPACE, R That is in English. In Italian it is ALT+SPACE, I , in other words it is language dependent.. 22 hours ago, gunsmokingman said: He was asking a specific VBS question but I guess the art of reading is lost to you. I know, I did read and perfectly understood the question, then I asked another question, strictly related, and made a suggestion, not strictly related, but solving the issue nonetheless. I have to guess that the art of being polite is lost to you. jaclaz
gunsmokingman Posted June 8, 2018 Posted June 8, 2018 Mike88 if you want a simple way to do what you want could you run the cmd from vbs Example [CODE} Dim Act :Set Act = CreateObject("Wscript.Shell") '-> First Cmd Show Windows Wait Until Finished Act.Run("Some1.cmd /Switches"),1,True '-> Second Cmd Hide Windows Wait Until Finished Act.Run("Some2.cmd /Switches"),0,True '-> Third Cmd Show Windows Wait Until Finished Act.Run("Some3.cmd /Switches"),1,True {/CODE] Table 3.9 Integers Accepted by the Run Method for the Window Style https://technet.microsoft.com/en-us/library/ee156605.aspx
Mike86 Posted June 8, 2018 Author Posted June 8, 2018 (edited) I think the next best option for me would be to trigger the HTA script with a JavaScript to put itself on focus again after a command has been run. Like: .......... if (cbo.value==null==false && cbo.checked) { try { display.innerHTML=(cbo.indicator); pause(3000); Act.Run(cbo.value.split("%CurDir%").join(basepath),1,true);<--- Command to put the HTA Script on Focuse again ---> pause(6000); } .......... Edited June 8, 2018 by Mike86
jaclaz Posted June 9, 2018 Posted June 9, 2018 (edited) Just for the record, besides the language dependent issue, I tested using sendkeys, and it doesn't work (on my XP), the cmd prompt doesn't open the menu with ALT+SPACE (it works just fine with - say - Notepad). It seems like half the internet is full of this, here is a clear sample of the VBS code: https://stackoverflow.com/questions/10964630/how-to-mock-the-background-window-to-active-state-using-vb-script but it simply doesn't work for a CMD.EXE window, here is said that is should work on 2K and XP (but it doesn't ) and that it doesn't work on Vista and 7: https://groups.google.com/forum/#!topic/microsoft.public.scripting.wsh/rPS5OUmixU4 A workaround (not a solution) may be to issue a (say) MODE 14,1 directly from batch, then issue a (say) MODE 80,25 to "restore" it jaclaz Edited June 9, 2018 by jaclaz
gunsmokingman Posted June 9, 2018 Posted June 9, 2018 Here is a SendKey Demo in VBS [CODE} Dim Act :Set Act = CreateObject("Wscript.Shell") Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") Act.Run("Notepad.exe"),1,False Do Until Success = True Success = Act.AppActivate("Notepad") Wscript.Sleep 1000 Loop '-> Send keys Body Message Act.SendKeys "This is a test of AppActivate." WScript.Sleep 1000 Act.SendKeys "{ENTER}" Act.SendKeys "T" :WScript.Sleep 500 Act.SendKeys "e" :WScript.Sleep 500 Act.SendKeys "s" :WScript.Sleep 500 Act.SendKeys "t" :WScript.Sleep 500 '-> Send keys For saving File Act.SendKeys "%F" Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{ENTER}" :WScript.Sleep 500 '-> Send key Save Text File Name Act.SendKeys "T" :WScript.Sleep 500 Act.SendKeys "e" :WScript.Sleep 500 Act.SendKeys "m" :WScript.Sleep 500 Act.SendKeys "p" :WScript.Sleep 500 Act.SendKeys ".txt":WScript.Sleep 500 Act.SendKeys "{ENTER}",500 '-> Send File Keys To Close Act.SendKeys "%F" :WScript.Sleep 1000 Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{DOWN}" :WScript.Sleep 500 Act.SendKeys "{ENTER}" :WScript.Sleep 500 '_> Remove The Created Text File Dim F, P P = Act.ExpandEnvironmentStrings("%Userprofile%") & "\Documents\Temp.txt" Set F = Fso.GetFile(P) F.Delete [/CODE} Rename Demo_SendKey.vbs.txt to Demo_SendKey.vbs to make active Demo_SendKey.vbs.txt
Mike86 Posted June 10, 2018 Author Posted June 10, 2018 (edited) @jaclaz The CMD mode command is an interesting idea but the little CMD box will hide some text of the HTA file and if a user accidentally clicks on the close (X) CMD window then everything would stoped. @gunsmokingman Do you know by any chance, how to do this minimize thing in a HTA file with a JavaScript without send keys or using auto focus loop? I have searched in google for a long time but i found only send keys or body loop focus scripts. Edited June 10, 2018 by Mike86
jaclaz Posted June 10, 2018 Posted June 10, 2018 2 hours ago, Mike86 said: @jaclaz The CMD mode command is an interesting idea but the little CMD box will hide some text of the HTA file and if a user accidentally clicks on the close (X) CMD window then everything would stoped. The "close" X can be disabled, but then again that would require *something* that you don't want, i.e.: http://www.uwe-sieber.de/files/consolenoclose.zip But if the (small) cmd.exe window falls "inside" a largish HTA Windows you should be able to bring that HTA window to the front via VBS, can't you? But is it a HTA now?, wasn't it before a second cmd window? @gunsmokingman Any idea why the sendkeys method that as said works just fine in Notepad doesn't work in the CMD.EXE window? :unsure: jaclaz
gunsmokingman Posted June 10, 2018 Posted June 10, 2018 The SendKey method is not a very reliable way of doing things. The best way I think to do what Mike86 wants is use CreateObject("Wscript.Shell").run method and use it own built in method of displaying how the app windows appear. Table 3.9 Integers Accepted by the Run Method for the Window Style https://technet.microsoft.com/en-us/library/ee156605.aspx Integer = Window Style Description 0 = Hides the window and activates another window. 1 = Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. 2 = Activates the window and displays it as a minimized window. 3 = Activates the window and displays it as a maximized window. 4 = Displays a window in its most recent size and position. The active window remains active. 5 = Activates the window and displays it in its current size and position. 6 = Minimizes the specified window and activates the next top-level window in the Z order. The Z order is nothing more than the list detailing the order in which windows are to be activated. If you press ALT+TAB, you will see a graphical representation of the Z list. 7 = Displays the window as a minimized window. The active window remains active. 8 = Displays the window in its current state. The active window remains active. 9 = Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. 10 = Sets the show-state based on the state of the program that started the application.
gunsmokingman Posted June 10, 2018 Posted June 10, 2018 I did a test of cmd.exe using Sendkeys I did manage to get something to work. The problem is the loop for cmd.exe it just stays stuck in the loop. Dim Act :Set Act = CreateObject("Wscript.Shell") Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject") Act.Run("cmd.exe"),1,False Success = Act.AppActivate("cmd.exe") Wscript.Sleep 1000 Act.SendKeys "COLOR 9F" :WScript.Sleep 100 Act.SendKeys "{ENTER}" :WScript.Sleep 500 WScript.Sleep 100 Act.SendKeys "Test of AppActivate." :WScript.Sleep 1000 Act.SendKeys "{ENTER}" Act.SendKeys "CLS" :WScript.Sleep 100 Act.SendKeys "{ENTER}" Act.SendKeys "Dir /B" :WScript.Sleep 500 Act.SendKeys "" :WScript.Sleep 500 Act.SendKeys "{ENTER}" Act.SendKeys "Exit" :WScript.Sleep 3500 Act.SendKeys "{ENTER}" [/CODE]
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now