4
\$\begingroup\$

I made myself a simple script (Batch/JScript) that download a pic from a blog (APOD) & set it as wallpaper.

@if (@a==@b) @end /*

Set BaseUrl=https://apod.nasa.gov/apod/
set yr=%date:~8,2%
set mn=%date:~3,2%
set dy=%date:~0,2%
set dte=%yr%%mn%%dy%
set SaveDest=C:\Users\ME\Desktop\Wallpaper\%dte%.jpg
Set Url=%baseUrl%ap%dte%.html


@echo off
setlocal
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%Url%"') do (
    rem 
    echo %%I
    set LastImg=%%I
)

set ImgUrl=%BaseUrl%%LastImg:~6%
bitsadmin.exe /transfer GETWALLPAPER %ImgUrl% %SaveDest%
timeout /t 01
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d %savedest% /f
timeout /t 01
for /l %%x in (1,1,80) do  (
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d %savedest% /f
timeout /t 01
)

goto :EOF

JScript */
function fetch(url) {
    var xhr=WSH.CreateObject("Microsoft.XMLHTTP");
    var dom=WSH.CreateObject('htmlfile');
    xhr.open("GET",WSH.Arguments(0),true);
    xhr.setRequestHeader('User-Agent','XMLHTTP/1.0');
    xhr.send('');
    while (xhr.readyState!=4) {WSH.Sleep(25)};
    dom.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
    dom.write(xhr.responseText);
    return dom;
}
var dom=fetch(WSH.Arguments(0));
var link=dom.getElementsByTagName('IMG');

WSH.Echo(link[0].src);

It's working. However, I am unhappy about the for /l %%x in (1,1,80) d loop: it seems not to work without the loop. Any Idea ?

\$\endgroup\$
3
\$\begingroup\$

First : I recommend you to change this line in your code in order to anybody can use it easily without changing or editing it.

set SaveDest=C:\Users\ME\Desktop\Wallpaper\%dte%.jpg

by those lines :

set WallpaperFolder=%userprofile%\Desktop\Wallpaper
If not exist "%WallpaperFolder%" MD "%WallpaperFolder%"
set SaveDest=%WallpaperFolder%\%dte%.jpg

Second : If you want to hide the output of the console you can redirect it to NUL

for example : reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d %savedest% /f>nul

And this line too : timeout /t 1 /nobreak>nul

Third : Refer to How to force Windows desktop background to update or refresh

You can write it simply by this line and avoid the loop : RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True

So, the whole code can be written like this :

@if (@a==@b) @end /*
@echo off
Title Download NASA images to use as Windows wallpaper
color 0A & Mode 70,3
Set BaseUrl=https://apod.nasa.gov/apod/
set yr=%date:~8,2%
set mn=%date:~3,2%
set dy=%date:~0,2%
set dte=%yr%%mn%%dy%
set WallpaperFolder=%userprofile%\Desktop\Wallpaper
If not exist "%WallpaperFolder%" MD "%WallpaperFolder%"
set SaveDest=%WallpaperFolder%\%dte%.jpg
Set Url=%baseUrl%ap%dte%.html

setlocal
for /f "delims=" %%I in ('cscript /nologo /e:jscript "%~f0" "%Url%"') do (
    echo %%I
    set LastImg=%%I
)

set ImgUrl=%BaseUrl%%LastImg:~6%

bitsadmin.exe /transfer GETWALLPAPER "%ImgUrl%" "%SaveDest%"
echo(
echo        Please wait a while ... Update  UserSystemParameters 
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v Wallpaper /t REG_SZ /d "%savedest%" /f>nul
Timeout /t 1 /nobreak>nul
RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters 1, True
goto :EOF

JScript */
function fetch(url) {
    var xhr=WSH.CreateObject("Microsoft.XMLHTTP");
    var dom=WSH.CreateObject('htmlfile');
    xhr.open("GET",WSH.Arguments(0),true);
    xhr.setRequestHeader('User-Agent','XMLHTTP/1.0');
    xhr.send('');
    while (xhr.readyState!=4) {WSH.Sleep(25)};
    dom.write('<meta http-equiv="x-ua-compatible" content="IE=9" />');
    dom.write(xhr.responseText);
    return dom;
}
var dom=fetch(WSH.Arguments(0));
var link=dom.getElementsByTagName('IMG');
WSH.Echo(link[0].src);
\$\endgroup\$
  • 1
    \$\begingroup\$ Thank you. very instructive. \$\endgroup\$ – a1a1a1a1a1 Jan 15 at 14:17
  • \$\begingroup\$ @a1a1a1a1a1 You can give a try for my vbscript version here NASA_Wallpaper.vbs \$\endgroup\$ – Hackoo Jan 20 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.