Hey, Scripting Guy! How Can I Change the Desktop Wallpaper to One of Six Predefined Selections?

Hey, Scripting Guy! How Can I Change the Desktop Wallpaper to One of Six Predefined Selections?

  • Comments 7
  • Likes

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the desktop wallpaper to one of six pre-defined selections?

-- SB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SB. You know, when the Scripting Guy who writes this column first sat down to tackle this problem he thought he had a really cool solution for you. Recently this Scripting Guy read about Johannes Trithemius, a Benedictine monk from the 1400s who was one of the fathers of modern cryptography. That’s all well and good, but what really caught the Scripting Guy’s attention was Johannes’ system for communicating by having angels pass messages from one person to another. Imagine how cool that would be: someone wants to change their desktop wallpaper, and an angel comes down from heaven and makes the change for them. Talk about making an impression on your users, eh?

Unfortunately, though, the Scripting Guy who writes this column could never quite figure out how to conjure up a heavenly angel. On two separate occasions he did manage to conjure up outfielders from the Los Angeles Angels of Anaheim, but neither of them would agree to go out and change people’s wallpaper. Of course, that could be due to the fact that the Scripting Guy who writes this column never actually asked them about changing the wallpaper, but instead spent the whole time making fun of their team name.

Still, he’s a little surprised they wouldn’t do it. After all, they’ll have plenty of spare time in October, when other teams are in the playoffs.

 

Note. Why the slam on the Angels, and totally out of nowhere? No reason, really, other than the fact that the team keeps changing its name, and the Script Guy who writes this column gets dizzy trying to keep up with them. After all, first they were the Los Angeles Angels, then they were the California Angels, then the Anaheim Angels, now the Los Angeles Angels of Anaheim. The Scripting Guy who writes this column figured he could go ahead and make fun of them because, by the time this column is published, the team will likely have changed its name again and none of the players will realize he’s talking about them.

 

Regardless, the Scripting Guy who writes this column was unable to get angels of any kind to change the wallpaper. Instead, the best he could come up with was a script that could do this:

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

intLowNumber = 1
intHighNumber = 6

Randomize

intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)

Select Case intNumber
    Case 1
        strValue = "C:\WINDOWS\System32\Wallpaper1.bmp"
    Case 2
        strValue = "C:\WINDOWS\System32\Wallpaper2.bmp"
    Case 3
        strValue = "C:\WINDOWS\System32\Wallpaper3.bmp"
    Case 4
        strValue = "C:\WINDOWS\System32\Wallpaper4.bmp"
    Case 5
        strValue = "C:\WINDOWS\System32\Wallpaper5.bmp"
    Case 6
        strValue = "C:\WINDOWS\System32\Wallpaper6.bmp"
End Select

strKeyPath = "Control Panel\Desktop"
ValueName = "Wallpaper"

objReg.SetStringValue HKEY_USERS, strKeyPath, ValueName, strValue

One quick note before we explain how this script works. It’s actually quite easy to change the wallpaper: you just assign the wallpaper path to a value in the registry (HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper). The only problem is that the change does not take place immediately; in fact, it doesn’t take place until the user logs off and then logs back on. (Why not? Because, without downloading a third-party utility from somewhere, there’s no straightforward and reliable way to refresh desktop settings using a script.) That doesn’t mean that you can’t change the wallpaper, it just means that you can’t make dynamic changes during the current logon session. Consequently, you might want to assign this script as a logoff script. That way, when the user logs off the script will run and the wallpaper will change. In turn, the change will take effect the next time the user logs on.

At any rate, let’s see if we can figure out how the script works. We start out by defining a constant named HKEY_CURRENT_USER and assigning it the value &H80000001; we’ll use this constant to tell the script which registry hive to work with. We then connect to the WMI service on the local computer (although we could also perform this task remotely), taking care to bind to the root\default namespace:

Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

Now it’s time to randomly select one of our 6 wallpapers. Admittedly, generating random numbers in VBScript can be a little confusing. Our advice? Don’t worry too much about the details. Just follow the basic template we’ve used here and you’ll be fine.

 

Note: The Scripting Guy who writes the Sesame Script column, which apparently the Scripting Guy who writes the Hey, Scripting Guy! column never reads, would tell you that if you really want to learn about generating random numbers, take a look at the Sesame Script column Working with Numbers in VBScript. And if you want to see a much more fun (or at least a little more fun) use of random numbers than simply assigning screen savers, take a look at the Dr. Scripto Concentration Game.

 

With that in mind we start out by assigning values to two variables, variables which represent the high and low numbers in our number range. Because we have 6 wallpapers to choose from that means we want our numbers to range from 1 to 6; hence we assign those values (1 and 6) to our variables:

intLowNumber = 1intHighNumber = 6

Next we call the Randomize function, a function that “seeds” the random number generator with the current system time. Is that important? You bet it is: if you don’t use the Randomize function the script will simply generate the same “random” number each time it runs. We then use this line of code and the Rnd function to randomly select a number between 1 and 6:

intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)

 

Note. Again, don’t worry too much about how this works. If you want to generate random numbers just use the preceding code as a template, and change the values of intHighNumber and intLowNumber as needed.

 

At this point the variable intNumber contains one of the following values: 1, 2, 3, 4, 5, or 6. To convert this random number into desktop wallpaper we use the following Select Case statement, in which we assign the variable strValue a different wallpaper path based on the value of intNumber:

Select Case intNumber
    Case 1
        strValue = "C:\WINDOWS\System32\Wallpaper1.bmp"
    Case 2
        strValue = "C:\WINDOWS\System32\Wallpaper2.bmp"
    Case 3
        strValue = "C:\WINDOWS\System32\Wallpaper3.bmp"
    Case 4
        strValue = "C:\WINDOWS\System32\Wallpaper4.bmp"
    Case 5
        strValue = "C:\WINDOWS\System32\Wallpaper5.bmp"
    Case 6
        strValue = "C:\WINDOWS\System32\Wallpaper6.bmp"
End Select

See how that works? If intNumber is equal to 1 the variable strValue gets assigned the path C:\WINDOWS\System32\Wallpaper1.bmp. If intNumber is equal to 2 the variable strValue gets assigned the path C:\WINDOWS\System32\Wallpaper2.bmp. And so on.

At this point we’re almost done. After assigning the wallpaper path to strValue we then use these two lines of code to assign values to variables that represent the appropriate registry key within HKEY_CURRENT_USER (Control Panel\Desktop) and the appropriate registry value within that key (Wallpaper):

strKeyPath = "Control Panel\Desktop"ValueName = "Wallpaper"

We then call the SetStringValue method to write the changes to the registry:

objReg.SetStringValue HKEY_USERS, strKeyPath, ValueName, strValue

That will change the registry although, again, the user won’t actually see the new wallpaper until he or she logs off and logs back on.

As we noted, in addition to trying to find a way to use angels to deliver messages Johannes Trithemius was also interested in more mundane forms of cryptography, particularly steganography, which involves hiding secret messages in plain sight. (For example, you might read the message by taking every other letter of every other word.) That, of course, leads to an interesting question: have the Scripting Guys been including secret messages in the things that they write?

Well, we can’t speak for the other Scripting Guys. But if you can find any kind of message or meaning in a Hey, Scripting Guy! column (let alone a hidden one) we can only assume that you must have made a mistake.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi,

    I tried this code but got an error that says "expect end of statement". here is my syntax

    any help is appreciated.

    Const HKEY_CURRENT_USER = &H80000001

    strComputer = "."

    Set objReg = GetObject("winmgmts:\\" & strComputer)

    Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

    intLowNumber = 1

    intHighNumber = 6

    intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)

    Select Case intNumber

    Case 1

    strValue = "C:\Documents and Settings\TED\Desktop\Scripts\Wallpapers\Wallpaper1.bmp"    

    Case 2

    strValue = "C:\Documents and Settings\TED\Desktop\Scripts\Wallpapers\Wallpaper2.bmp"

    Case 3

    strValue = "C:\Documents and Settings\TED\Desktop\Scripts\Wallpapers\Wallpaper3.bmp"

    Case 4

    strValue = "C:\Documents and Settings\TED\Desktop\Scripts\Wallpapers\Wallpaper4.bmp"

    Case 5

    strValue = "C:\Documents and Settings\TED\Desktop\Scripts\Wallpapers\Wallpaper5.bmp"

    Case 6

    strValue = "C:\Documents and Settings\TED\Desktop\Scripts\Wallpapers\Wallpaper6.bmp"

    End Select

    strKeyPath = "Control Panel\Desktop"ValueName = "Wallpaper"

    bjReg.SetStringValue HKEY_USERS, strKeyPath, ValueName, strValue

  • The Scripting Guy was a little bit hurried on his cut and paste:

    Const HKEY_CURRENT_USER = &H80000001

    strComputer = "."

    Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

    intLowNumber = 1

    intHighNumber = 6

    intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)

    Select Case intNumber    

    Case 1        

    strValue = "C:\WINDOWS\System32\Wallpaper1.bmp"    

    Case 2        

    strValue = "c:\Windows\Web\Wallpaper\Wallpaper_NIPR.bmp"

    Case 3        

    strValue = "c:\Windows\Web\Wallpaper\Wallpaper_NIPR.bmp"

    Case 4        

    strValue = "c:\Windows\Web\Wallpaper\Wallpaper_NIPR.bmp"

    Case 5        

    strValue = "c:\Windows\Web\Wallpaper\Wallpaper_NIPR.bmp"

    Case 6      

    strValue = "c:\Windows\Web\Wallpaper\Wallpaper_NIPR.bmp"

    End Select

    strKeyPath = "Control Panel\Desktop"

    ValueName = "Wallpaper"

    objReg.SetStringValue HKEY_CURRENT_USER, strKeyPath, ValueName, strValue

  • Hi,

    running this command applies the modified user configurations:

    Set oShell = CreateObject("WScript.Shell")

    oShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 1, True

    Found this on another page.

    Cheers

  • Hey guys just run the below script .. It think it will work for you

    Dim WshShell

    Const HKEY_CURRENT_USER = &H80000001

    strComputer = "."

    Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

    intLowNumber = 1

    intHighNumber = 6

    Randomize

    intNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)

    msgbox intNumber

    Select Case intNumber

        Case 1

           strValue = "C:\WINDOWS\System32\Wallpaper1.bmp"

       Case 2

           strValue = "C:\WINDOWS\System32\Wallpaper2.bmp"

       Case 3

           strValue = "C:\WINDOWS\System32\Wallpaper3.bmp"

       Case 4

           strValue = "C:\WINDOWS\System32\Wallpaper4.bmp"

       Case 5

           strValue = "C:\WINDOWS\System32\Wallpaper5.bmp"

       Case 6

           strValue = "C:\WINDOWS\System32\Wallpaper6.bmp"

    End Select

    strKeyPath = "Control Panel\Desktop"

    ValueName = "Wallpaper"

    objReg.SetStringValue HKEY_USERS, strKeyPath, ValueName, strValue

    Set WshShell = WScript.CreateObject("Wscript.Shell")

    WshShell.RegWrite "HKCU\Control Panel\Desktop\Wallpaper", strValue

    WshShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 1, False

    Set WshShell = Nothing

  • HBK, I just had to change "WshShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 1, False" to "WshShell.Run "%windir%\System32\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 1, true"....

    Thank you

  • Hi,

    I've searched all over and this is probably the closest to what I want to accomplish.

    Is it possible to put a time (say 30mn) between each of the wallpaper randomization?

    Thanks

  • Very useful tips. I has used ac-digital.com free wallpapers. it has bundles of color on your desktop,laptop or other devise with this stunning image of beautiful, colorful flowers.Sure to light up your day.if you can try this one.