1

is there anyway i can emulate pressing Home + D with a batch file?

I need it to minimize a IE window that is in full screen without loosing the full screen attribute.

Thanks

razstec
  • 93
  • 2
  • 11
  • You mean Meta+D? Look into [WshShell.SendKeys](http://ss64.com/vb/sendkeys.html). Unfortunately, SendKeys can't send the Windows meta key. However, you can try to send `[Alt]+[Space]` then `[n]` to minimize the active window. I just tried it with manual keypresses on IE in F11 mode and it minimized successfully, then restored to full screen when reactivated. [See this question](http://stackoverflow.com/questions/3824284) for example usage. Also consider [Shell.MinimizeAll()](http://msdn.microsoft.com/en-us/library/windows/desktop/bb774083%28v=vs.85%29.aspx). – rojo Dec 15 '14 at 15:48
  • thanks, i think this will work, can you tell me where i can find the codes for windows PT? thanks – razstec Dec 15 '14 at 16:00
  • Sorry, what's Windows PT? Is that Windows Portuguese? When you're in Internet Explorer, hit Alt+Space, then look for the option that minimizes. The appropriate letter will be underlined. – rojo Dec 15 '14 at 17:28
  • Sorry should have explained better, yes it windows portuguese. thanks – razstec Dec 16 '14 at 09:19
  • Its also n but cant seem to make it work like this dim wshell set wshell = CreateObject("WScript.Shell") wshell.SendKeys "% n" – razstec Dec 16 '14 at 09:20

2 Answers2

1

There is another way to do the trick with vb script c:\Windows\System32\cscript.exe //H:CScript yourscript.vbs the script file consist of two commands only

set objShell = CreateObject("shell.application")

objShell.ToggleDesktop

And also other way is to invoke AutoHotkey.exe winD.ahk the script file WinD.ahk consist of one row

Send, #d

ahk forum: minimize windows

msdn: IShellDispatch4.ToggleDesktop method

Yurij73
  • 5,281
  • 3
  • 15
  • 14
0

It appears that you mean Windows meta key+D. WshShell.SendKeys can emulate keypresses. Unfortunately, SendKeys can't send the Windows meta key key. However, you can try to send Alt+Space, then N to minimize the active window. You'll need a slight pause between Alt+Space and N to give the titlebar icon menu a chance to render -- otherwise, N will be sent to the main window and not to the menu.

Save this as a .bat file and run it for a demonstration.

@if (@a==@b) @end   /* JScript multiline comment

:: begin batch portion

@echo off
setlocal

for /f "tokens=2" %%I in ('tasklist /v /fi "imagename eq iexplore.exe" ^| find "Internet Explorer"') do set "PID=%%I"
if defined PID goto minimize

:: apparently IE is not running.  Launch, then wait for it to appear.
start "" "%programfiles%\Internet Explorer\iexplore.exe"
:ie 
ping -n 2 0.0.0.0 >NUL
for /f "tokens=2" %%I in ('tasklist /v /fi "imagename eq iexplore.exe" ^| find "Internet Explorer"') do set "PID=%%I"
if not defined PID goto ie

:minimize
cscript /nologo /e:Jscript "%~f0" "%PID%"

goto :EOF

:: end batch portion / begin JScript */

var oShell = WSH.CreateObject('wscript.shell');
WSH.Echo('Activating window with PID ' + WSH.Arguments(0));
oShell.AppActivate(WSH.Arguments(0));
WSH.Sleep(100);
WSH.Echo('Making full screen');
oShell.SendKeys('{F11}');
WSH.Sleep(1000);
WSH.Echo('Activating titlebar icon menu');
oShell.SendKeys('% ');
WSH.Sleep(100);
WSH.Echo('Minimizing');
oShell.SendKeys('n');
WSH.Sleep(100);

Edit: For what it's worth, Alt+Esc will also minimize most windows. It doesn't work with F11-mode IE though. Fullscreen IE gets pushed to the back of all windows rather than being minimized. But anyone else coming across this answer from a Google Search, try sending Alt+Esc. This is especially useful for minimizing a cmd window, as Alt+Space doesn't work on those.


Edit 2: To have your script minimize its own window, then minimize IE, then re-launch itself, try the following modifications:

@if (@a==@b) @end   /* JScript multiline comment

:: begin batch portion

@echo off
setlocal

for /f "tokens=2" %%I in ('tasklist /v /fi "imagename eq iexplore.exe" ^| find "Internet Explorer"') do set "PID=%%I"
if defined PID goto minimize

:: apparently IE is not running.  Launch, then wait for it to appear.
start "" "%programfiles%\Internet Explorer\iexplore.exe"
:ie 
ping -n 2 0.0.0.0 >NUL
for /f "tokens=2" %%I in ('tasklist /v /fi "imagename eq iexplore.exe" ^| find "Internet Explorer"') do set "PID=%%I"
if not defined PID goto ie

:minimize
cscript /nologo /e:Jscript "%~f0" "%PID%"

goto :EOF

:: end batch portion / begin JScript */

var oShell = WSH.CreateObject('wscript.shell');
if (WSH.Arguments.length == 1) {
    WSH.Echo('Activating window with PID ' + WSH.Arguments(0));
    oShell.SendKeys('%{ESC}')
    oShell.AppActivate(WSH.Arguments(0));
    WSH.Sleep(100);
    WSH.Echo('Making full screen');
    oShell.SendKeys('{F11}');
    WSH.Sleep(1000);
    WSH.Echo('Minimizing');
    oShell.SendKeys('% ');
    WSH.Sleep(100)
    oShell.SendKeys('n');
    oShell.Run('cscript /nologo /e:JScript ' + WSH.ScriptFullName + ' ' + WSH.Arguments(0) + ' ' + '1', 1, false);
    WSH.Sleep(100);
    WSH.Quit(0);
}
else {
    WSH.Echo('Done.  Hit ENTER to exit.');
    WSH.StdIn.ReadLine();
}
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Hi, thanks but it dont seem to work, also i notice that you check if ie is open, teres no need since the bat will be call from ie, it will be open. – razstec Dec 16 '14 at 14:14
  • My intention was not to manage your project from start to finish. This is merely a proof of concept. There could be other problems -- browser security inhibiting the functionality available to the script, for example. Try running the script from a `cmd` prompt. It works on my machine. Salt to taste. – rojo Dec 16 '14 at 14:20
  • its working the problem is that the cmd window become default there for it dont minimize the ie window – razstec Dec 16 '14 at 15:40
  • On my machine, `oShell.AppActivate` successfully focuses the IE window. It could be that since you are launching the script directly from within IE, browser security is limiting the ability for the applications it launches to manipulate window focus. Try doing `oShell.SendKeys('%{ESC}')` on the line before `oShell.AppActivate`. That should hopefully minimize the console and restore focus back to IE. Also, did you know there is a [Stack Overflow em Português](http://pt.stackoverflow.com/)? – rojo Dec 16 '14 at 16:20
  • But the window im trying to minimize is IE – razstec Dec 16 '14 at 17:08
  • it works but not in the script, i already have one to taskkill the ie and close why dont this work to run minimize? dim wshell set wshell = CreateObject("WScript.Shell") wshell.run "C:\inetpub\wwwroot\visua\minimiza.bat" set wshell = nothing – razstec Dec 16 '14 at 17:16
  • I suggest having this script minimize its own window, then having it minimize the IE window. Then, since there's no graceful way to restore a minimized window with windows scripting, have the script re-launch itself with an argument that makes it skip what has been run thus far. You should ask this in a separate question, as this is deviating too far beyond the scope of your original question above. It could also be that when IE spawns a process, it spawns in a sandbox that prevents the program from interacting with the open IE window, and therefore there is no solution to your problem. – rojo Dec 16 '14 at 17:21
  • Your original question asked how to simulate the "Show Desktop" keyboard shortcut anyway. That would've minimized not only IE, but also the running script at the same time. What I suggested is not much different from that. – rojo Dec 16 '14 at 17:27
  • @RicardoSimoes I made one final edit. Check out the second script and see if it gives you ideas on how to proceed. I tested it by navigating to `file:///c:/path/to/batfile.bat` in IE and running it directly from the download prompt. It worked as intended for me. – rojo Dec 16 '14 at 17:48