MarioZac wrote: ↑19 May 2018 15:46
I'm not very familiar with hybrid batches, and some other readers probably too. Would you be able to take a moment to explain without details how this new approach works in general, and in particular to eliminate the Scheduled Task?
Sad to say it's not a new approach:
It's (nearly) the same, as before - the jscript part just executes the the batch portion that changes the icon within a hidden frame.
As i had not much to to with the task scheduler, i'm unsure if you could create that part using scripts only:
I will try to, but this might take some time.
If someone says something is a hybrid script/source file, then he means, that this file could be used by two or more interpreters/compilers.
In case of a hybrid JScript/batch file this single file is a valid JScript file and it is also a valid batch file:
Code: Select all
@if (true==false) @end /*
@echo off
powercfg.exe /S "%~1"
for %%a in ("HKCU\Software\Classes\DesktopBackground\Shell\Switch Power Plan") do (
powercfg /getactivescheme | findstr "%~1" && reg add "%%~a" /v "Icon" /d "%~2" /f || reg add "%%~a" /v "Icon" /d "%~3" /f
)
goto :eof
*/
if (WScript.Arguments.Length == 3) {
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.Run ("\"" + WScript.ScriptFullName + "\" \"" + WScript.Arguments(0) + "\" \"" + WScript.Arguments(1) + "\" \"" + WScript.Arguments(2) + "\" ", 0, false);
} else {
WScript.Echo("Usage: WScript.ScriptName \"powerplanId\" \"powerplanIcon\" \"fallbackIcon\".");
}
The trick in this case is, that the first line, from the perspective of the JScript, is a valid conditional execution (which is never executed because true isn't false; and also won't do anything even if because there is no text parsed between the ')' and "@end"). The "/*" starts a multiline comment, which ends after the batch code so that is completely ignored by JScript. After that the JScript starts itself as a hidden batch frame.
From the perspective of a batch file the "@" is a special character that tells the command processor not to display the command (within the same line) that should be executed. The next command is an if and the command processor also doesn't execute this line because the strings "(true" and "false)" don't match. So the command processor doesn't even try to interpret the " @end /*" command (which would raise an error message).
then the batch file updates the icon (based on parameters and success of enabling the new power plan.
I added a fallback item if the power plan couldn't be activated, because someone might delete those.
In that case you have to run the setup batch a second time.
Maybe one could also trigger that using the task scheduler, but on the one hand actually i don't know if there is an event that could trigger what is needed, and on the other hand batch simply cannot read the mind of the user, so i don't know which icons to use if someone wishes to have different than std ones, especially if he/she has added own power plans (like me).
Raising an info message probably would be better in such a case.
penpen