T3RRYT3RR0R

Batch Wallpaper Changer

Feb 13th, 2021 (edited)
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 7.64 KB | None | 0 0
  1. <# : batch portion
  2. :# The above line marks the beginning of a powershell comment block; and the Batch component of the Script. Do not modify.
  3. ::# Author: T3RRY ; Creation Date 12/02/2021 ; Version: 1.0.3
  4. ::# * Batch Powershell Hybrid * Resource: https://www.dostips.com/forum/viewtopic.php?f=3&t=5543
  5. ::# Script Purpose:
  6. ::# - Change the wallpaper from command prompt through the use of Parameter; Or by Input if no Parameter.
  7. ::# - Script Designed for use with pictures in the %Userprofile%\Pictures Directory
  8. ::#   or sub directories and should be placed in the %Userprofile%\Pictures Directory.
  9. ::#   - Hot tip: Add the %Userprofile%\Pictures Directory to your System environment Path variable.
  10. ::#     https://helpdeskgeek.com/windows-10/add-windows-path-environment-variable/
  11.  
  12. @Echo off & Mode 120,40
  13.  
  14. :# Test for Arg 1 ; Usage output ; Offer Input or Abort
  15.  
  16.  Set "Arg1=%~1"
  17.  If "%Arg1%" == "" (
  18.   Call "%~f0" "/?"
  19.   Echo/
  20.   Set "Wallpaper="
  21.   Set /P "Wallpaper=Press ENTER to abort, or enter filepath / Search Term: "
  22.   Setlocal EnableDelayedExpansion
  23.   If "!Wallpaper!" == "" Exit /B
  24.   Call "%~f0" "!Wallpaper!"
  25.   Endlocal
  26.   Exit /B
  27.  )
  28.  
  29. :# Test for Unsupported Arg Count ; Notify Ignored Args; Show Help; Offer Abort
  30.  
  31.  Set ParamErr=%*
  32.  If Not "%~2" == "" (
  33.   Setlocal EnableDelayedExpansion
  34.   Echo/Args:"!ParamErr:%Arg1% =!" Ignored. %~n0 only accepts 1 Arg.
  35.   Call "%~f0" "/?"
  36.   Endlocal
  37.   Echo/Continue with Arg1:"%Arg1%" [Y]/[N]?
  38.   For /F "Delims=" %%G in ('Choice /N /C:YN')Do if "%%G" == "N" Exit /B
  39.  )
  40. :# /Dir Switch - Display all image paths with matching extensions in tree of current Directory
  41.  If Not "%Arg1:/Dir=%" == "%Arg1%" (
  42.   Dir /B /S "*.jpg" "*.png" "*.bmp" | More
  43.   Exit /B
  44.  )
  45.  
  46. :# Usage test and output
  47.  
  48.  If Not "%Arg1:/?=%" == "%Arg1%" (
  49.   Echo/ %~n0 Usage:
  50.   Echo/
  51.   Echo/ %~n0 ["wallpaper filepath" ^| "Search term"]
  52.   Echo/      Search times should include wildcard/s: * ? and / or extension as appropriate
  53.   Echo/ Example:
  54.   Echo/      Search for and select from .jpg files containing Dragon in the filename:
  55.   Echo/     %~n0 "*Dragon*.jpg"
  56.   Echo/
  57.   Echo/ %~n0 [/Dir] - output list of available .jpg .png and .bmp files in the directory tree
  58.   Echo/ %~n0 [/?] - help output
  59.   Exit /B
  60.  )
  61.  
  62.  Set "Wallpaper=%Arg1%"
  63.  
  64. :# Arg1 Not a valid path; Offer Addition of Wildcards to SearchTerm If not Present as Bookends
  65.  If not exist "%Wallpaper%" If not "%Wallpaper:~0,1%" == "*" If not "%Wallpaper:~,-1%" == "*" (
  66.   Echo/Add wildcards to "%Wallpaper%" {"*%Wallpaper%*"} [Y]/[N]?
  67.   For /F "Delims=" %%G in ('Choice /N /C:YN')Do if "%%G" == "Y" Set "Wallpaper=*%Wallpaper%*"
  68.  )
  69.  
  70. :# To support Search Terms run script in Top level of Directory containing Images; Find Full Path in Tree.
  71.  
  72.  PUSHD "%Userprofile%\Pictures"
  73.  Set "Matches=0"
  74.  (For /F "Delims=" %%G in ('Dir /B /S "%Wallpaper%"')Do (
  75.    Set "Wallpaper=%%~fG"
  76.    Set /A Matches+=1
  77.    Call Set "Img[%%Matches%%]=%%~fG"
  78.  )) 2> Nul
  79.  
  80. :# Determine if Target Wallpaper is Current Wallpaper; Notify and Exit
  81.  reg query "HKEY_Current_User\Control Panel\desktop" -v wallpaper | %__AppDir__%findstr.exe /LIC:"%Wallpaper%" && (
  82.   Echo/Wallpaper already applied.
  83.   Exit /B
  84.  )
  85.  
  86. :# Enable environment for macro expansion, Arrays and code block variable operations
  87.   Setlocal EnableExtensions EnableDelayedExpansion
  88.  
  89.  If NOT %Matches% GTR 1 Goto :Apply
  90.  
  91. :# Report When Multiple Matches found; Offer menu containing up to first 36 matches [ limit of menu macro ]
  92.  
  93. If %Matches% GTR 36 Set Matches=36
  94.  
  95. ==================================================================
  96. :# Menu Macro Author: T3RRY
  97. :# IMPORTANT - RESERVED VARIABLES: Menu CH# CHCS Options Option Opt[i] Option.Output Cholist DIV
  98.  
  99. (Set \n=^^^
  100.  
  101. %= Newline var \n for multi-line macro definition - Do not modify. =%)
  102.  
  103. :# Key index list Allows 36 menu options. Component of Menu Macro
  104.  Set "ChoList=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  105.  
  106. :# Get console width for dividing line
  107.  for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| %__APPDIR__%findstr.exe /LIC:"Columns"`) do Set /A "Console_Width=%%W"
  108.  
  109. :# Menu macro escaped for Definition with DelayedExpansion Enabled. Ensures correct Environment:
  110.  If not "!" == "" (
  111.   Setlocal EnableExtensions EnableDelayedExpansion
  112.  )
  113.  
  114. :# Build dividing line for menu output.
  115.  Set "DIV=" & For /L %%i in (2 1 %Console_Width%)Do Set "DIV=!DIV!-"
  116.  
  117. :# Menu macro Usage: %Menu% "quoted" "list of" "options"
  118.  
  119.      Set Menu=For %%n in (1 2)Do if %%n==2 (%\n%
  120. %= Output Dividing Line                 =%  Echo(^^!DIV^^!%\n%
  121. %= Reset CH# index value for Opt[#]     =%  Set "CH#=0"%\n%
  122. %= Undefine choice option key list      =%  Set "CHCS="%\n%
  123. %= For Each in list;                    =%  For %%G in (^^!Options^^!)Do (%\n%
  124. %= For Option Index value               =%   For %%i in (^^!CH#^^!)Do (%\n%
  125. %= Build the Choice key list and Opt[#] =%    Set "CHCS=^!CHCS^!^!ChoList:~%%i,1^!"%\n%
  126. %= array using the character at the     =%    Set "Opt[^!ChoList:~%%i,1^!]=%%~G"%\n%
  127. %= current substring index.             =%    Set "option.output=%%~G"%\n%
  128. %!!%    Echo([^^!ChoList:~%%i,1^^!] ^^!Option.output:#=^^!%\n%
  129. %= Increment Opt[#] Index var 'CH#'     =%    Set /A "CH#+=1"%\n%
  130. %= Close CH# loop                       =%   )%\n%
  131. %= Close Options loop                   =%  )%\n%
  132. %= Output Dividing Line                 =%  Echo(^^!DIV^^!%\n%
  133. %= Select option by character index     =%  For /F "Delims=" %%o in ('%__APPDIR__%Choice.exe /N /C:^^!CHCS^^!')Do (%\n%
  134. %= Assign return var 'OPTION' with the  =%   Set "Option=^!Opt[%%o]^!"%\n%
  135. %= value selected from Opt[CH#] array.  =%   If /I "^!Option^!" == "Exit" Exit /B 2%\n%
  136. %= Return to previous script on Exit    =%  )%\n%
  137. %= Capture Macro input - Options List   =% )Else Set Options=
  138. ========================================== :# End Definition of Menu Macro
  139.  
  140. :# Notify match count
  141.  
  142.  Echo/%Matches% Files Matched:"!Arg1!"
  143.  
  144. :# Use match count to build options list for Menu macro from IMG[ Array. Restricted to first 36 matches.
  145.  
  146.  Set "Menu.Options="
  147.  For /L %%i in (1 1 %Matches%)Do Set "Menu.Options=!Menu.Options! "!Img[%%i]!""
  148.  
  149. :# Call Menu macro
  150.  
  151.  %Menu% %Menu.Options%
  152.  
  153. :# Notify selected option
  154.  
  155.  Echo(Wallpaper Selected = !Option!
  156.  Set "Wallpaper=%Option%"
  157.  
  158. :Apply
  159.  
  160. :# Pipe Filepath to Powershell; Capture as Powershell Variable within Pipe; Exit on Return.
  161.  Echo/!Wallpaper!| powershell.exe -noprofile "$Image = $input | ?{$_}; iex (${%~f0} | out-string)"
  162.  Endlocal
  163.  POPD
  164. Exit /B 0
  165.  
  166. :# The below line Marks the end of a Powershell comment Block; And the End of the Batch Script.
  167. : end batch / begin powershell #>
  168.  
  169. <#
  170.  Function Source: https://www.joseespitia.com/2017/09/15/set-wallpaper-powershell-function/
  171. #>
  172.  
  173. Function Set-WallPaper {
  174.  [CmdletBinding()]
  175.     param (
  176.         [Parameter(Mandatory)]
  177.         [string]$Image
  178.     )
  179.  
  180. Add-Type -TypeDefinition @"
  181. using System;
  182. using System.Runtime.InteropServices;
  183.  
  184. public class Params
  185. {
  186.     [DllImport("User32.dll",CharSet=CharSet.Unicode)]
  187.     public static extern int SystemParametersInfo (Int32 uAction,
  188.                                                    Int32 uParam,
  189.                                                    String lpvParam,
  190.                                                    Int32 fuWinIni);
  191. }
  192. "@
  193.  
  194. $SPI_SETDESKWALLPAPER = 0x0014
  195. $UpdateIniFile = 0x01
  196. $SendChangeEvent = 0x02
  197.  
  198. $RefreshIni = $UpdateIniFile -bor $SendChangeEvent
  199.  
  200. $ret = [Params]::SystemParametersInfo($SPI_SETDESKWALLPAPER, 0, $Image, $RefreshIni)
  201. }
  202.  
  203. If (Test-Path $Image) {
  204.     Set-WallPaper -Image $Image
  205.     write-output "Wallpaper Updated."
  206. }else {
  207.     write-output "Wallpaper Does not exist in the Directory Tree."
  208. }
  209.  
  210.  
Add Comment
Please, Sign In to add comment