How to close the personalization window after a certain time using the command line

I have some vbs code that automatically changes my Windows theme via cmd and also closes it after the operation is completed. A personalization window will open, Windows will change the theme, and then the personalization window will close. The problem is that sometimes the window does not close after changing the theme, and I wonder why. Also, is there a one-line code in cmd (or vbs that can be executed via cmd) that just closes the personalization window? Thanks in advance for your help! My used code is as follows:

Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""C:\Windows\Resources\Ease of Access Themes\basic.theme""" Wscript.Sleep 1600 WshShell.AppActivate("Desktop Properties") WshShell.Sendkeys "%FC" WshShell.Sendkeys "{F4}" 
0
source share
2 answers

Your Run call is not running synchronously, so your script will continue without waiting for Run complete. This is great, and this is what you need in your situation. But if it takes more than 1600 ms to run the Desktop Properties dialog box, then AppActivate and your SendKeys commands are sent to a nonexistent window. Have you tried to increase your sleep time to make sure it works?

You can also check for window availability in a loop. AppActivate returns True if the window is found and False otherwise. For example, here is a fragment that tries to see for 10 seconds whether a window appears (checking every second) ...

 For i = 1 To 10 WScript.Sleep 1000 If WshShell.AppActivate("Desktop Properties") Then WshShell.Sendkeys "%FC" WshShell.Sendkeys "{F4}" Exit For End If Next ' If i > 10, it failed to find the window. 
0
source

After trying a similar solution, I came up with the following powershell command:

 Function Get-WindowHandle($title,$class="") { $code = @' [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); '@ Add-Type -MemberDefinition $code -Namespace MyWinAPI -Name GetWindowHandle return [MyWinAPI.GetWindowHandle]::FindWindow($class, $title) } Function Close-WindowHandle($windowHandle) { $code = @' [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern bool PostMessage(IntPtr hWnd, int flags, int idk, int idk2); '@ Add-Type -MemberDefinition $code -Namespace MyWinAPI -Name CloseWindowHandle #https://msdn.microsoft.com/en-us/library/windows/desktop/ms632617(v=vs.85).aspx $WM_CLOSE = 0x0010 return [MyWinAPI.CloseWindowHandle]::PostMessage($windowHandle, $WM_CLOSE, 0, 0) } Close-WindowHandle $(Get-WindowHandle 'Personalization' 'CabinetWClass') 
0
source

Source: https://habr.com/ru/post/989617/


All Articles