I've been making a script for school, but I've encountered a few problems...
This if my script ">" shows the error.
Complete error:
Line: 26
Char: 14
Error: Syntax Error
' Macro starter script -- basically for any teachers or students to understand and or improve my script.
'oShell.AppActivate("Paint"); // Set focus to a program
'WScript.Sleep(500); // Delay for 1/2 second
'oShell.Run("notepad c:\\temp\\somefile.txt", 1, false); // launch a program
'----------------------------- can add ,2 to run minimized, true=wait for exit
'oShell.SendKeys("%F"); // Alt+F ----- SendKeys strings to jog my memory
'oShell.SendKeys("^C"); // Ctrl+C
'oShell.SendKeys("{ENTER}"); // Send Enter key (also tilde {~} )
'oShell.SendKeys("{{TAB} 2"); // Send Tab key twice )
'oShell.SendKeys(" "); // Send a space
'var nResult= oShell.Popup("Do you want to continue?", 0, "My Macro", 4+32 );
'if ( nResult==6 ) { // 6=Yes, 7=No,
' WScript.Echo( "you said 'Yes!'" );
'}
'WScript.Echo( "done!" );
'Script Starts Here
var oShell = WScript.CreateObject("WScript.Shell")
WScript.Echo( "Script made by Joshua Hughes" )
var sResult = oShell.Popup("Do you want to save all work and shut down?", 0, "Shutdown", 4+32)
>if ( sResult==6 ) {
WScript.Echo( "Starting, do not press any buttons or click!" )
oShell.AppActivate("Word");
WScript.Sleep(500);
oShell.SendKeys("^S");
oShell.SendKeys("{ENTER}");
WScript.Sleep(500);
oShell.SendKeys("%F4")
oShell.AppActivate("Notepad");
WScript.Sleep(500)
oShell.SendKeys("^S");
oShell.SendKeys("{ENTER}");
WScript.Sleep(500);
oShell.SendKeys("%F4")
oShell.AppActivate("Powerpoint");
WScript.Sleep(500)
oShell.SendKeys("^S");
oShell.SendKeys("{ENTER}");
WScript.Sleep(500);
oShell.SendKeys("%F4")
oShell.AppActivate("Paint");
WScript.Sleep(500)
oShell.SendKeys("^S");
oShell.SendKeys("{ENTER}");
WScript.Sleep(500);
oShell.SendKeys("%F4")
end
]
WScript.Echo( "Program Complete! Shutting Down..." )
'Script Ends Here
The story:
I had finished my assessment, an E-Zine, for grade eight at my high school, and I've been fiddling with code, I managed to make a visual basic script save all files, close it, then shutdown, the script only worked for notepad though.Since I've gotten home today, I've been expanding the script, and adding in a bit more, such as the classic 'Are you sure' prompt. But my computer is disagreeing...
I added that because it said:
'It looks like your post is mostly code; please add some more details.'
>if ( sResult==6 ) {
'...
end
]
is not VBScript. Proper syntax is
If sResult = 6 Then
'...
End If
Related
i have some code in which user has to give some information and based on the user information subsequent code will be executed. strong textcan we make emeditor to wait till the user window is closed.strong text
clipboardData.setData("Text", "");
wnd = shell.FindWindow( "", "Playlist" );
wnd.SetForeground();
Sleep( 1000 );
shell.SendKeys( "^a" );
Sleep( 100 );
shell.SendKeys( "^%c" );
Editor.newfile();
Sleep(500);
document.selection.Paste(eeCopyUnicode);
if( !editor.EnableTab ){
editor.EnableTab = true;
alert( "Please run this macro again." );
Quit();
}
**strong text**sFind = prompt( "This macro extracts lines that do contain any of the specified multiple strings separated by |:", "" );**strong text**
if( sFind == "" ){
Quit();
}
var sArr = sFind.split("|");
batch_list = editor.filters;
for( i = 0; i < sArr.length; ++i ) {
batch_list.AddFind(sArr[i],0,0);
}
//document.selection.BatchFind(batch_list, eeFindExtract | eeFindLineOnly,0);
document.selection.BatchFind(batch_list, eeFindExtract | eeFindLineOnly,0);
document.selection.SelectAll(); // select all text
document.selection.Copy(eeCopyUnicode); // copy the seleciton to the Clipboard
WshShell = new ActiveXObject( "WScript.Shell" );
WshShell.Run ( "PotPlayerMini64.exe /clipboard" );
in the above code, strong textbefore i close prompt window subsequent code is getting executed. i want to stop execution of subsequent code, before prompt window is closed.strong text is it possible.
I am automating a software installation in Windows7 using AutoIt.
During the installation, in between if a error window appears. I want to click ENTER.
If the error window not appears then I should NOT do anything. Simply its should go to the next section.
I have tried "WinActive and WinWaitActive" But its waiting for the window to appear. If window not appears its not going to the next screen.
Any idea how to handle this situation?
Do a while loop:
$w = 0
While($w = 0)
If(WinActive("ERROR WINDOW"))Then
Send("{ENTER}")
$w = 1
ElseIf(ControlGetText("YOUR WINDOW", "", "[CLASS:Static; INSTANCE:2]") <> "SOME TEXT") Then
$w = 1
;and something else
EndIf
Sleep(1000)
WEnd
AdlibRegister() is the right choice. From the help file:
"... typically to check for unforeseen errors. For example, you could use adlib in a script which causes an error window to pop up unpredictably."
Each 100 ms (may be adjusted) the function is called to check the appearing of your error dialog:
Global $sErrorWindow = 'ErrorDialogName'
Global $iDelayHowOftenDoTheFunctionCall = 100
AdlibRegister('_isErrorWindowDisplayed', $iDelayHowOftenDoTheFunctionCall)
Func _isErrorWindowDisplayed()
If WinActive($sErrorWindow) <> 0 Then
WinActivate($sErrorWindow) ; just to be sure that the ENTER command is on the correct window/dialog
; either do
Send('{ENTER}')
; or
ControlClick('title', 'text', 'controlID')
EndIf
EndFunc
; do your software installation processing here
; ...
; ...
; don't forget to unregister the function at the end
AdlibUnRegister('_isErrorWindowDisplayed')
I've collected a script from the AutoHotKey forum which lets me open a command prompt at the location I'm open in windows explorer. If the current window is not a explorer window then the prompt opens at the location where the script is present. I would like to change this behavior and make it open from C:\ if the current window is not a explorer window. I've tried to edit the script but its not working as desired.
#ifwinactive, ahk_class CabinetWClass
ControlGetText, address , edit1, ahk_class CabinetWClass
if (address <> "") {
Run, cmd.exe, %address%
}
else {
Run, cmd.exe, "C:"
}
ExitApp
#ifwinactive
The command to run cmd.exe in the c:\ path is
run, cmd.exe, c:\
A full script that would run the cmd window every time would look like this
SetTitleMatchMode, 2
ifwinactive, ahk_class CabinetWClass
ControlGetText, address , edit1, ahk_class CabinetWClass
else
address =
; Exclude specific windows
ifwinactive, My Computer
address =
ifwinactive, My Documents
address =
if (address <> "")
Run, cmd.exe, %address%
else
Run, cmd.exe, C:\
ExitApp
I realize this is an old question, but I was looking into this myself and have a better solution.
Windows has two in-built ways to start cmd at the path of a current explorer window. Shift+RightClick and then click Open Command Window Here (or press w). You can also press alt+d, type cmd, and press enter. So...
LWin & Return::
if WinActive("ahk_class CabinetWClass")
or WinActive("ahk_class ExploreWClass")
{
Send {Shift Down}{AppsKey}{Shift Up}
Sleep 10
Send w{enter}
}
else
{
run, cmd, C:\
}
return
No magically grabbing the address directly from explorer! :)
Couldn't get other answers to work (it has been a few years since they've been written).
I ended up writing this script:
#o::
Send {Alt down}D{Alt up}cmd{enter}
return
Here's a pretty sophisticated script from the AHK forums:
#NoEnv
#SingleInstance Force
#NoTrayIcon
SendMode Input
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode RegEx
#IfWinActive ahk_class ExploreWClass|CabinetWClass|Progman
#c::
WinGetClass WinClass
If ( WinClass = "Progman" )
{
Run %ComSpec% /K cd /D "C:\"
Return
}
If ( InStr( "WIN_7,WIN_VISTA" , A_OSVersion ) )
{
ControlGetText, Path, ToolbarWindow322
RegExMatch(Path, ":\s*(.*)", Path)
Path := Path1
}
Else
{
; Windows XP doesn't know the Edit1 control exists if
; the Address Bar is hidden, so check if it exists and temporarly
; show the Address bar if needed. Temporarly showing the Address bar
; will register the Edit1 control, which contains the path.
ControlGetPos Edit1Pos , , , , Edit1
If ( !Edit1Pos )
{
PostMessage 0x111 , 41477 , 0 , , A ; Show Address Bar
Sleep 100
PostMessage 0x111 , 41477 , 0 , , A ; Hide Address Bar
}
ControlGetText Path , Edit1
}
If ( InStr( Path , ":" ) )
; If( InStr( Path , ":" ) && FileExist(Path) )
Run %ComSpec% /K cd /D "%Path%"
Else
Run %ComSpec% /K cd /D "C:\"
Return
I tweaked the WIN_7 part a little, so that the code is independent of the unreliable Edit1 control, which doesn't always expose the current explorer location or an incorrect one. If ( InStr( Path , ":" ) ) makes sure that there's no custom path like Computer on Windows 7 or My Computer on Windows XP. I also added an alternative condition that additionally checks for the path to exist, if you want to hedge your bets.
Keep it simple. Unless of course you need complexity.
!f1::
run, C:\Windows\System32\cmd.exe
return
!f1 means Alt+F1. For my personal preference. Change it to whatever you like.
Another solution hacked together from here. Works for me on Windows 10, but I admit it's total copy-pasta. Posting in the hopes of saving someone else's eyes from the horror of AHK scripting.
;; Open terminal in current Explorer window folder
#If WinActive("ahk_class CabinetWClass") ; explorer
F4::
WinGetTitle, ActiveTitle, A
If InStr(ActiveTitle, "\") ; If the full path is displayed in the title bar (Folder Options)
Fullpath := ActiveTitle
else
If InStr(ActiveTitle, ":") ; If the title displayed is something like "DriveName (C:)"
{
Fullpath := SubStr(ActiveTitle, -2)
Fullpath := SubStr(Fullpath, 1, -1)
}
else ; If the full path is NOT displayed in the title bar
; https://autohotkey.com/boards/viewtopic.php?p=28751#p28751
for window in ComObjCreate("Shell.Application").Windows
{
try Fullpath := window.Document.Folder.Self.Path
SplitPath, Fullpath, title
If (title = ActiveTitle)
break
}
Run, cmd.exe, %Fullpath%
return
#If
I am trying to write a LotusScript to control a motor. The script I have for reading the bits seem to work fine, but I wish to add a stop button. I have all the commands for making the device stop, but the trouble I'm having is that whenever LotusScript is running through a loop, I cannot click on any of the other buttons.
Does anyone know a way around this???
The scripts I am using are below.
Thank-you kindly.
Andy Barlow
Sub readpositionsub
Dim send_string As String
Dim readString As String
Dim tempString As String
readString = ""
REM Sets the "movement" cell to 6 (the movement int)
[b1].contents = "6"
Do While [b1].contents <> "7"
readString = ""
statusBitString = ""
REM READ STATUS ===!!!===
REM Open the handle to the motor
handle = init_RS232(19200)
REM #1$ reads the status from the controller.
send_string = "#1$"+Chr$(13)
REM Ask the controller to store the results in bits
resultStatus=write_RS232 (handle,send_string)
REM Read Status by looping through all of the bits
For n=0 To 8
tempString = "*1234567"
sendReadCommand = read_RS232(handle,tempString)
If Mid(tempString,1,1) = Chr$(13) Then
Exit For
Else
statusBitString = statusBitString + Mid(tempString,1,1)
End If
Next
[b1].contents = Mid(statusBitString,7, 1)
close_RS232(handle)
REM End Read Status
REM READ POSITION ===!!!===
REM Open the handle to the motor
handle = init_RS232(19200)
send_string = "#1C"+Chr$(13)
t=write_RS232 (handle,send_string)
REM Reading Position
For n=0 To 20
tempString = "*1234567"
r = read_RS232(handle,tempString)
If Mid(tempString,1,1) = Chr$(13) Then
Exit For
Else
readString = readString + Mid(tempString,1,1)
End If
Next
REM End Read Position
[a1].contents=Mid(readString, 4)
close_RS232(handle)
Loop
End Sub
And the stop button that should work should be...
Object btnStop
Sub Click(Source As Buttoncontrol)
REM initialise
handle = init_RS232(19200)
REM Create the string for starting the motor
send_string = "#1S"+Chr$(13)
REM Send the string for starting the motor
resultStartMotor=write_RS232 (handle,send_string)
REM Close the spin handle
close_RS232(handle)
End Sub
You're not running your Lotusscript in a threaded environment, so how do you expect the code on a button to stop the code already running?
If you want to be able to cancel a loop you need to play with the timer object. Basically you start a time that executes one iteration of your loop. At the beginning you look for a changed field value or an ini variable to cancel the timer if set. Now you can use your button to set that variable.
The loop will run much slower than without a timer (after all it has a wait interval you set)
I want to allow my users to toggle the current user theme between Aero and Windows Classic(1). Is there a way that I can do this programatically?
I don't want to pop up the "Display properties", and I'm dubious about just changing the registry. (This requires a log out and a log back in for the changes to take effect).
Application skinning (using the Codejock libraries) doesn't work either.
Is there a way of doing this?
The application is hosted/run on a Windows Server 2008 over RDP.
(1) The application in question is a hosted "Remote App", and I want users to be able to change the look of the displayed application to match their desktop.
You can set it using the following command:
rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,#Themes /Action:OpenTheme /file:"C:\Windows\Resources\Themes\aero.theme"
Caveat is that this will show the theme selector dialog. You could kill that dialog straight after.
There are certainly good reasons for wanting to change the current theme programmatically. E.g. an automated test tool may need to switch between various themes to make sure the application works correctly with all of them.
As a user, you can change the theme by double-clicking a .theme file in Windwos Explorer and then closing the Control Panel applet that pops up. You can easily do the same from code. The steps below work just fine for me. I've only tested on Windows 7.
Use SHGetKnownFolderPath() to get the "Local AppData" folder for the user. Theme files are stored in the Microsoft\Windows\Themes subfolder. Theme files stored there are applied directly, while theme files stored elsewhere are duplicated when you execute them. So it's best to use files from that folder only.
Use ShellExecute() to execute the .theme file you located in step 1.
Wait for the theme to be applied. I simply let my app sleep for 2 seconds.
Call FindWindow('CabinetWClass', 'Personalization') to get the handle of the Control Panel window that popped up when the theme was applied. The "Personalization" caption will likely be different on non-US-English versions of Windows.
Call PostMessage(HWND, WM_CLOSE, 0, 0) to close the Control Panel window.
This isn't a very elegant solution, but it does the job.
I know this is an old ticket, but somebody asked me how to do this today. So starting from Mike's post above I cleaned things up, added comments, and will post full C# console app code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
namespace Windows7Basic
{
class Theming
{
/// Handles to Win 32 API
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string sClassName, string sAppName);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
/// Windows Constants
private const uint WM_CLOSE = 0x10;
private String StartProcessAndWait(string filename, string arguments, int seconds, ref Boolean bExited)
{
String msg = String.Empty;
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p.StartInfo.FileName = filename;
p.StartInfo.Arguments = arguments;
p.Start();
bExited = false;
int counter = 0;
/// give it "seconds" seconds to run
while (!bExited && counter < seconds)
{
bExited = p.HasExited;
counter++;
System.Threading.Thread.Sleep(1000);
}//while
if (counter == seconds)
{
msg = "Program did not close in expected time.";
}//if
return msg;
}
public Boolean SwitchTheme(string themePath)
{
try
{
//String themePath = System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + #"\Resources\Ease of Access Themes\basic.theme";
/// Set the theme
Boolean bExited = false;
/// essentially runs the command line: rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,#Themes /Action:OpenTheme /file:"%WINDIR%\Resources\Ease of Access Themes\classic.theme"
String ThemeOutput = this.StartProcessAndWait("rundll32.exe", System.Environment.GetFolderPath(Environment.SpecialFolder.System) + #"\shell32.dll,Control_RunDLL " + System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\desk.cpl desk,#Themes /Action:OpenTheme /file:\"" + themePath + "\"", 30, ref bExited);
Console.WriteLine(ThemeOutput);
/// Wait for the theme to be set
System.Threading.Thread.Sleep(1000);
/// Close the Theme UI Window
IntPtr hWndTheming = FindWindow("CabinetWClass", null);
SendMessage(hWndTheming, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}//try
catch (Exception ex)
{
Console.WriteLine("An exception occured while setting the theme: " + ex.Message);
return false;
}//catch
return true;
}
public Boolean SwitchToClassicTheme()
{
return SwitchTheme(System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + #"\Resources\Ease of Access Themes\basic.theme");
}
public Boolean SwitchToAeroTheme()
{
return SwitchTheme(System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + #"\Resources\Themes\aero.theme");
}
public string GetTheme()
{
string RegistryKey = #"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes";
string theme;
theme = (string)Registry.GetValue(RegistryKey, "CurrentTheme", string.Empty);
theme = theme.Split('\\').Last().Split('.').First().ToString();
return theme;
}
// end of object Theming
}
//---------------------------------------------------------------------------------------------------------------
class Program
{
[DllImport("dwmapi.dll")]
public static extern IntPtr DwmIsCompositionEnabled(out bool pfEnabled);
/// ;RunProgram("%USERPROFILE%\AppData\Local\Microsoft\Windows\Themes\themeName.theme") ;For User Themes
/// RunProgram("%WINDIR%\Resources\Ease of Access Themes\classic.theme") ;For Basic Themes
/// ;RunProgram("%WINDIR%\Resources\Themes\aero.theme") ;For Aero Themes
static void Main(string[] args)
{
bool aeroEnabled = false;
Theming thm = new Theming();
Console.WriteLine("The current theme is " + thm.GetTheme());
/// The only real difference between Aero and Basic theme is Composition=0 in the [VisualStyles] in Basic (line omitted in Aero)
/// So test if Composition is enabled
DwmIsCompositionEnabled(out aeroEnabled);
if (args.Length == 0 || (args.Length > 0 && args[0].ToLower(CultureInfo.InvariantCulture).Equals("basic")))
{
if (aeroEnabled)
{
Console.WriteLine("Setting to basic...");
thm.SwitchToClassicTheme();
}//if
}//if
else if (args.Length > 0 || args[0].ToLower(CultureInfo.InvariantCulture).Equals("aero"))
{
if (!aeroEnabled)
{
Console.WriteLine("Setting to aero...");
thm.SwitchToAeroTheme();
}//if
}//else if
}
// end of object Program
}
}
I'm not sure if this is a new thing, but you can just double click the .theme file and Windows 10 will apply the theme. Hence, you can do this with PowerShell easily:
$Windows10Theme = "C:\Windows\Resources\Themes\aero.theme"
Invoke-Expression $Windows10Theme
The command for newer Windows versions (Windows 8 and 8.1, haven't tried it on W10 yet) is:
rundll32.exe themecpl.dll,OpenThemeAction %1
or with full paths:
C:\WINDOWS\system32\rundll32.exe C:\WINDOWS\system32\themecpl.dll,OpenThemeAction %LocalAppData%\Microsoft\Windows\Themes\yourtheme.theme
Basically it's the Personalisation CPL "open" command for .theme & .themepack extensions taken from registry...
You'll still end up with the Personalisation window beeing open after using this command so to close it down programatically you'll have to use one of the suggested methods mentioned above... (I personally prefer the Powershell script)
I have been experimenting about changing the windows theme via command line and I learned that by executing the theme file it is being applied by the Windows 10 as well. So in your batch file, you could use one of the following lines:
C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Themes\Dark_Mode.theme
or
C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Themes\Light_Mode.theme
Please note the path to the theme files might be needed to adjust depending on your system user configuration. I strongly advise saving your themes with names excluding spaces as it makes much easier moving forward. Executing such line leaving you with the Settings window opened. To deal with I considered using VBS script instead. Thanks to Patrick Haugh user1390106 there is a much easier way to close the Settings window.
taskkill /F /IM systemsettings.exe
So the updated version of batch file could look like this:
#echo off
if %1 == dark (
REM ================== Go Dark ==================
color 09
echo.
echo Applying DARK MODE
echo Windows Theme ...
C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Themes\Dark_Mode.theme
timeout /T 1 /nobreak > nul
taskkill /F /IM systemsettings.exe > nul
echo DONE
) else (
REM ============== Return to Light ==============
color 30
echo.
echo Applying LIGHT MODE
echo Windows Theme ...
C:\Users\%USERNAME%\AppData\Local\Microsoft\Windows\Themes\Light_Mode.theme
timeout /T 1 /nobreak > nul
taskkill /F /IM systemsettings.exe > nul
echo DONE
)
REM ================== Goodbye ==================
echo.
echo Goodbye
cls
exit
Please note the path to the theme files might be needed to adjust depending on your system user configuration. Save above script with the name theme.bat somewhere in your drive.
This batch file taking one parameter which needs to be either dark or any other string. Then you could prepare two shortcuts to this batch file each with one of the following in the box called “Target” on the “Shortcut” tab in its properties:
C:\full-path-to-your-batch-file\theme.bat dark
or
C:\full-path-to-your-batch-file\theme.bat light
Please replace “full-path-to-your-batch-file” with actual path to that file.
Here are links to the videos showing how this works:
a) Going Dark – https://youtu.be/cBcDNhAmfyM
b) Returning to the Light – https://youtu.be/2kYJaJHubi4
Please note that my script in those videos also activating/deactivating the Stylish plug-in for chrome. I have omitted to explain how I accomplished that part as it is not a subject of this article.
I believe the best you can do is open your target .msstyles file (in c:\windows\resources\themes), which will pop up the display properties box. At this point you could use window subclassing to programmatically click the right buttons.
In addition of the post of "Jan Goyvaerts":
I use SendMessage instead of PostMessage. The difference is that SendMessage waits for the command to be taken in by the window. Meaning that in the SendMessages returns, you know that the theme dialog is closed.
So if you start it with the monstrous (but genious) rundll32.exe method suggested by "Campbell". You should wait a sec before sending WM_CLOSE. Otherwise the theme will not be set and the application closes right away.
The code snippet below extracts a file from resource (a themepack). Then executes the desk.cpl with rundll32.exe, waits 3 sceonds, then sends WM_CLOSE (0x0010), waits for the command to be process (the time it takes for the theme to be set).
private Boolean SwitchToClassicTheme()
{
//First unpack the theme
try
{
//Extract the theme from the resource
String ThemePath = System.Environment.GetFolderPath(Environment.SpecialFolder.Windows) + #"\Resources\Themes\ClassicTheme.themepack";
//WriteFileToCurrentDirectory("ClassicTheme.theme", TabletConfigurator.Resources.ClassicTheme);
if(File.Exists(ThemePath))
{
File.Delete(ThemePath);
}
if(File.Exists(ThemePath))
{
throw new Exception("The file '" + ThemePath + "' exists and can not be deleted. You can try to delete it manually.");
}
using (BinaryWriter sw = new BinaryWriter(new FileStream(ThemePath, FileMode.OpenOrCreate)))
{
sw.Write(TabletConfigurator.Resources.ClassicTheme);
sw.Flush();
sw.Close();
}
if(!File.Exists(ThemePath))
{
throw new Exception("The resource theme file could not be extracted");
}
//Set the theme file as like a user would have clicked it
Boolean bTimedOut = false;
String ThemeOutput = StartProcessAndWait("rundll32.exe", System.Environment.GetFolderPath(Environment.SpecialFolder.System) + #"\shell32.dll,Control_RunDLL " + System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\desk.cpl desk,#Themes /Action:OpenTheme /file:\"" + ThemePath + "\"", ref bTimedOut);
System.Threading.Thread.Sleep(3000);
//Wait for the theme to be set
IntPtr hWndTheming = FindWindow("CabinetWClass", null);
SendMessage(hWndTheming, (uint)WM_CLOSE, 0, 0);
//using (Bitmap bm = CaptureScreenShot())
//{
// Boolean PixelIsGray = true;
// while (PixelIsGray)
// {
// System.Drawing.Color pixel = bm.GetPixel(0, 0)
// }
//}
}
catch(Exception ex)
{
ShowError("An exception occured while setting the theme: " + ex.Message);
return false;
}
return true;
}
I just realized you can double click the theme and it autoswitches it - much simpler, so just executing the theme works, ex batch file:
:: Reactivate my theme after an remote desktop session
:: We must select another theme first before we can select ours again and hence re-activate Aero, please wait..."
#echo Off
"C:\Windows\Resources\Themes\aero.theme"
::echo "Simulating a pause while"
ping 127.0.0.1 -n 10 > null && "D:\Users\danielsokolowski\Windows 7 Aero Themes\`danielsokolowski` Theme (without Glass).theme"
::or ping 127.0.0.1 -n 3 > null && "%userprofile%\AppData\Local\Microsoft\Windows\Themes\`danielsokolowski` Theme (without Glass).theme"
For Windows 10 I wrote this simple solution (it can also be used in DSC) in PowerShell
# Apply your theme
& "C:\Windows\Resources\Themes\Brand.theme"
# We need to wait for the theme to be applied
Start-Sleep -s 5
# Close the settings window that is opened by the action above
$window = Get-Process | Where-Object {$_.Name -eq "SystemSettings"}
Stop-Process -Id $window.Id
Okay so here is my take on this - a VB script. It's a bit nasty but the best I could come up with (sadly).
For a user that logs in, we simply run ChangeTheme.vbs as the user logs in (e.g. autorun). The script starts desk.cpl and passes the required parameters to it as well as the name of the selected theme.
One can run the script with or without parameters:
> ChangeTheme.vbs
> ChangeTheme.vbs AnyThemeName
The script:
' ////////////////////////////////////////////////////////////////////
'
' Changes the theme.
'
' Name:
' ChangeTheme.vbs
' Parameter 1:
' Theme name e.g. aero or anything
' located in in C:\Windows\Resources\Themes.
' If not present, a default theme will be used.
'
' Example:
' Inside a command line run
' > ChangeTheme.vbs TheThemeName
'
' ////////////////////////////////////////////////////////////////////
If(Wscript.Arguments.Count <= 0) Then
' If no parameter was given we set the following theme as default
selectedTheme = "aero"
Else
' Get theme via the first argument
selectedTheme = Wscript.Arguments(0)
End If
' Create WScript shell object
Set WshShell = WScript.CreateObject("WScript.Shell")
' Run the command to open the "theme application" (or whatever)
Set process = WshShell.Exec("rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,#Themes /Action:OpenTheme /file:""C:\Windows\Resources\Themes\" & selectedTheme & ".theme""")
' Wait for the application to start
Wscript.Sleep 250
Success = False
maxTries = 20
tryCount = 0
Do Until Success = True
Wscript.Sleep 1000
' Set focus to our application
' If this fails, or the application loses focus, it won't work!
Success = WshShell.AppActivate(process.ProcessId)
tryCount = tryCount + 1
If (tryCount >= maxTries) Then
' If it does not work after maxTries we give up ..
MsgBox("Cannot change theme - max tries exceeded ..")
Exit Do
End If
Loop
' The crucial part: Send keys ALT + B for applying the theme
WshShell.Sendkeys "%(B)"
' Send key "escape" to close the window
WshShell.Sendkeys "{ESCAPE}"
Hope that helps.
It works on Windows 10.
this is my script. It changes the theme and closes the window. I save it to a batch file and run this patch file from TaskScheduler:
C:\WINDOWS\system32\rundll32.exe C:\WINDOWS\system32\themecpl.dll,OpenThemeAction C:\Users\xxx\Misc_computer_stuff\themes\my_fav_gr.theme
TIMEOUT 1 & REM Waits 1 seconds before executing the next command
TASKKILL /F /IM systemsettings.exe & close window
exit
You can simply open any of the .theme files present in C:\Windows\Resources\Themes\ to change the theme.
The only catch is that the settings app is also opened after this. But we can kill it using Stop-Process in PowerShell
Invoke-Expression "C:\Windows\Resources\Themes\<theme_name>.theme"
Start-Sleep -Seconds 2
Stop-Process -Name SystemSettings
For Example:
Invoke-Expression "C:\Windows\Resources\Themes\dark.theme"