I have some vbs code that will automatically change my windows theme via cmd as well as close it after the operation completes. The personalization window opens, Windows changes the theme, and then the personalization window closes. The problem is, sometimes the window doesn't close after changing the theme and I'm wondering why. Also, is there a one-liner code in cmd (or vbs that can execute through cmd) that just closes the personalization window? Thanks in advance for your help! My code used 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}"
After trying a similar solution, I came up with the following powershell:
Your
Run
call is being done asynchonously so your script will continue without waiting forRun
to complete. This is fine, and it's what you need in your situation. But if it takes longer than 1600ms to launch the Desktop Properties dialog thenAppActivate
and yourSendKeys
commands are being sent to a nonexistent window. Have you tried increasing the sleep time to see if it works?You can also test the availability of the window in a loop.
AppActivate
returnsTrue
if the window is found andFalse
otherwise. For example, here's a snippet that tries for 10 seconds to see if the window appears (checking each second)...