Jump to content

Toggle minimize/restore CMD Window with a VBScript?


Recommended Posts

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? :wacko:

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 by Mike86
Link to comment
Share on other sites


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)? :dubbio:

I would rather try Nircmd, cmdow or similar. :unsure:

jaclaz

Link to comment
Share on other sites

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 by Mike86
Link to comment
Share on other sites

20 hours ago, jaclaz said:

Which (simulated) keypresses need to be sent (to minimize/restore the command prompt window)? :dubbio:

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.

Link to comment
Share on other sites

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 by Mike86
Link to comment
Share on other sites

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. :dubbio:

jaclaz

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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 by Mike86
Link to comment
Share on other sites

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 :whistle:) 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 by jaclaz
Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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 by Mike86
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...