Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ::: ** A template for batch scripts to allow control over display properties and the playing of sound files. ** /
- :::
- ::: Features:
- ::: - Support for automatically centering the console on screen, or positioning the console screen top left.
- ::: - calculates centre position relative to console size and screen resolution. (mode 1000 not supported)
- :::
- ::: - Supports a variety of options for displaying text in the console including:
- ::: - Aligning text Left, Centre or Right within the console in the chosen color
- ::: - Outputing text in multiple colors on the same line - even within the same word.
- :::
- ::: - Can play any music or sound file compatable with windows media player
- ::: - Volume range of 0-100
- ::: - Single play or Looping of Sounds
- :::
- ::: - No extra windows are visible during the use of the vbs or bat programs used to play, monitor or stop the music.
- ::: - Automaticaly stops all vbs scripts when no cmd.exe windows are open
- :::
- ::: - Subroutines limited to single use code elements wherever possible
- ::: - Macro's used for code elements that will be heavily reused for faster performance
- ::: \
- ::: ** Note: Pastebin does note display all lines of this code correctly.
- ::: - you'll need to either copy it from the raw paste data, or download the file.
- %= This remark style is reserved for use within macro definition as it does not disrupt the definition =%
- @ECHO OFF
- ::: / Program must be stored in a folder with read / write access. change directory to this programs folder
- CD "%~dp0"
- ::: \
- ::: / Define pathways and create subfolders used for self generated component files
- ::: - calls subroutines to create component files if they do not already exist
- Set "ProgDir=%~dp0"
- If not exist "%ProgDir%Sounds" MD "%ProgDir%Sounds"
- Set "Sounds=%ProgDir%Sounds"
- Set "Player=%Sounds%\BatchMusicPlayer.bat"
- Set "MusicStopper=%Sounds%\StopMusic.bat"
- Set "Monitor=%Sounds%\BatchMonitor.vbs"
- If not Exist "%Player%" Call :makeplayer
- If not Exist "%MusicStopper%" Call :makestopper
- If not Exist "%Monitor%" Call :makemonitor
- ::: \
- ::: / Creates variable /AE = Ascii-27 escape code, used to change text color.
- ::: - http://www.dostips.com/forum/viewtopic.php?t=1733
- ::: - https://stackoverflow.com/a/34923514/12343998
- :::
- ::: - /AE can be used with and without DelayedExpansion.
- Setlocal
- For /F "tokens=2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
- Endlocal
- Set "/AE=%%a"
- )
- ::: \
- ::: / Assigns this Batch's home path to a variable for use in recalling itself after positioning the console
- Set "ThisProgram=%~dpnx0"
- ::: \
- Setlocal DisableDelayedExpansion
- (Set LF=^
- %= NewLine =%)
- Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"
- ::: / mini-Macro to Pseudo pipe complex strings into Macros.
- Set "Params|=Set Args-In="
- ::: \
- ::: / Macro for safely taking user input for assignment to variables.
- Set @Input=^&for /L %%n in (1 1 2) do if %%n==2 (%\n%
- for /F "tokens=1,2,3 delims=, " %%G in ("!argv!") do (%\n%
- %= Assign default value for variable =%
- Set "%%G=%%I"%\n%
- %= Give user 3 opportunities to provide their own value before default is finalised =%
- For %%a in (!Args-In!) do For /L %%C in (1,1,3) do If "!%%~G!" == "%%~I" Set /P "%%~G=!%%H!%%~a!Off!"%\n%
- ) %\n%
- ) ELSE set argv=,
- ::: \
- ::: - Calculate console width and values for MSG alignment for the Align macro
- Set @Align_Centre=Set /A "%%G=(Console_Width / 2) - (Len / 2)"
- Set @Align_Right=Set /A "%%G=(Console_Width - Len)"
- Set @Align_Left=Set /A "%%G=0"
- ::: / @Align Macro calculates string length then uses 2nd Parameter to Act on Alignment Calculation
- ::: - Macro appends spaces to the string depending on Alignment value / mode chosen to position, then output string to console
- Set @Align=^&for /L %%n in (1 1 2) do if %%n==2 (%\n%
- For /F "tokens=1,* delims=, " %%G in ("!argv!") do (%\n%
- Set "MSGOut=!Args-In!"%\n%
- Set LenTrim=Start%\n%
- For /L %%a in (1,1,!Console_Width!) Do (%\n%
- IF NOT "!LenTrim!"=="" (%\n%
- Set LenTrim=!MSGOut:~0,-%%a!%\n%
- If "!LenTrim!"=="" Set "Len=%%a"%\n%
- ) %\n%
- ) %\n%
- IF /I "%%G"=="C" %@Align_Centre% %\n%
- IF /I "%%G"=="R" %@Align_Right% %\n%
- IF /I "%%G"=="L" %@Align_Left% %\n%
- For /L %%# in (1,1,!%%G!) Do Set "MSGOut= !MSGOut!" %\n%
- Echo(!Color!!MSGOut!!white!^&^& Endlocal %\n%
- ) %\n%
- ) ELSE setlocal enableDelayedExpansion ^& set argv=,
- ::: \
- ::: / custom pause prompt macro that accepts long strings as input when paired with the Param macro
- Set @Hold=^&for /L %%n in (1 1 2) do if %%n==2 (%\n%
- For /F "tokens=1 delims=, " %%G in ("!argv!") do (%\n%
- (For %%A in (!Args-In!) Do Call :Color%%~G "%%~A") ^& Echo.!Off! ^& Pause^>nul%\n%
- ) %\n%
- ) ELSE set argv=,
- ::: \
- ::: * / Color Macro Variables
- :::/ 'Color Change'
- Set "@CC=Set Color="
- ::: \
- ::: / Color code assignment
- Setlocal EnableDelayedExpansion
- Set /A Red=31,Green=32,Yellow=33,dark.blue=34,Purple=35,light.Blue=36,White=0,Grey=90,Pink=91,Beige=93,Aqua=94,Magenta=95,Teal=96,Off=0,Black=30
- For %%A in (Red,Green,Yellow,dark.blue,Purple,light.Blue,White,Grey,Pink,Beige,Aqua,Magenta,Teal,Off,Black) do Call Set "%%A=%/AE%[!%%A!m"
- Setlocal DisableDelayedExpansion
- ::: \
- ::: * \
- ::: / Detects when this program has been restarted after resizing / repositioning the console and restores
- ::: - required variables before continuing from the specified label
- If Not "%~3"=="" (
- Set "Console_Width=%~2"
- Set "Console_Hieght=%~3"
- Set "ThisProgram=%~4"
- Goto :%~1
- ) Else (Goto :ConsolePROPS)
- ::: \
- :::: **** Subroutines /
- ::: * / Subroutine to process output of wmic command into usable variables for screen dimensions (resolution)
- :ChangeConsole <Lines> <Columns> <Label to Resume From> <If a 4th parameter is Defined, Aligns screen at top left>
- Setlocal EnableDelayedExpansion
- ::: - Get screen Dimensions
- For /f "delims=" %%# in ('"wmic path Win32_VideoController get CurrentHorizontalResolution,CurrentVerticalResolution /format:value"') do (
- Set "%%#">nul
- )
- ::: - Calculation of X axis relative to screen resolution and console size. Resolution scales to Max Columns ~ 165
- Set /A XresScale=CurrentHorizontalResolution / 165
- Set /A HorzCentre=CurrentHorizontalResolution / 2
- Set /A CentreX= ( HorzCentre - ( ( %~2 * XresScale ) / 2 ) ) - 8
- ::: - Calculation of Y axis relative to screen resolution and console size. Resolution scales to Max Lines ~ 43
- Set /A YresScale= CurrentVerticalResolution / 43
- Set /A VertCentre=CurrentVerticalResolution / 2
- Set /A CentreY= VertCentre - ( ( %~1 * YresScale ) / 2 )
- ::: - Optional 4th parameter can be used to align console at top left of screen instead of screen centre
- If Not "%~4"=="" (Set /A CentreY=0,CentreX=-8)
- ::: - .Vbs script creation and launch to reopen batch with new console settings, combines with
- Set "Console_Width=%~2"
- ::: / Creates a batch file to reopen the ConsolePROPS script using Call with parameters to define properties for console
- ::: - change and the label to resume from.
- (
- Echo.@Mode Con: lines=%~1 cols=%~2
- Echo.@Title Res: %CurrentHorizontalResolution%x%CurrentVerticalResolution% X,Y Pos: %CentreX%,%CentreY% Con Size: Cols = %~2 Lines = %~1
- Echo.@Call "%ThisProgram%" "%~3" "%~2" "%~1" "%ThisProgram%"
- )>"%temp%\ChangeConsole.bat"
- ::: \
- ::: / Vbs used to position the console, and open the above companion file used to reload this program
- (
- Echo.Set objWMIService = GetObject^("winmgmts:\\.\root\cimv2"^)
- Echo.Set objConfig = objWMIService.Get^("Win32_ProcessStartup"^)
- Echo.objConfig.SpawnInstance_
- Echo.objConfig.X = %CentreX%
- Echo.objConfig.Y = %CentreY%
- Echo.Set objNewProcess = objWMIService.Get^("Win32_Process"^)
- Echo.intReturn = objNewProcess.Create^("%temp%\ChangeConsole.bat", Null, objConfig, intProcessID^)
- )>"%temp%\Consolepos.vbs"
- %= Starts the companion batch script to Change Console properties, ends the parent =%
- Start "" "%temp%\Consolepos.vbs" & Exit
- ::: * \
- :Colorwords
- Setlocal EnableDelayedExpansion
- IF "!#A!" == "" Set "#A=31"
- For %%A in (%*) do (
- Set "Word=%%~A"
- Call :ColorPrint "!Word!"
- <nul set /p=%/AE%[30m.%/AE%[0m
- )
- Endlocal
- Exit /B
- :ColorLetters
- Setlocal EnableDelayedExpansion
- IF "!#A!" == "" Set "#A=31"
- For %%A in (%*) do (
- Set "Word=%%~A"
- For %%B In (a b c d e f g h i j k l m n o p q r s t u v w x y z . [ ] ) do Set "Word=!Word:%%~B=%%~B !
- Call :ColorPrint "!Word!"
- <nul set /p=%/AE%[30m.%/AE%[0m
- )
- Endlocal
- Exit /B
- :ColorPrint
- IF "!#A!" == "" Set #A=31
- For %%C in (%~1) do (
- <nul set /p=%/AE%[!#A!m%%~C
- Set /A #A+=1
- IF "!#A!"=="37" (Set #A=31)
- )
- Exit /B
- ::: / creates a companion batch file thats called with arguments for trackpath, volume and loop tf values
- :makeplayer
- (
- Echo.@ECHO OFF
- Echo.Set "MusicPath=%%~1"
- Echo.Set "vol=%%~2"
- Echo.Set "Loop_TF=%%~3"
- ::: - Change to the Directory you want to create the Music Launcher in.
- Echo.PUSHD %%sounds%%
- ::: - Ensure no Conflict with the Previous Script.
- Echo.IF exist PlayMusic.vbs ^(
- Echo.DEL PlayMusic.vbs
- Echo.^)
- ::: - Creates a vbs Script to Launch the music (Occurs without any visual indication or prompting)
- Echo.^( echo Set Sound = CreateObject^("WMPlayer.OCX.7"^^^)
- Echo.echo Sound.URL = "%%MusicPath%%"
- Echo.echo Sound.settings.volume = %%vol%%
- Echo.echo Sound.settings.setMode "loop", %%Loop_TF%%
- Echo.echo Sound.Controls.play
- Echo.echo While Sound.playState ^^^<^^^> 1
- Echo.echo WScript.Sleep 100
- Echo.echo Wend
- Echo.^)^>PlayMusic.vbs
- Echo.start /min PlayMusic.vbs
- ::: - Return to the Previous Directory
- Echo.POPD
- ::: - Exit the Launcher and return to Previous batch program.
- Echo.Goto :EOF
- )>"%Player%"
- Exit /B
- ::: \
- ::: / Monitor checks process status of cmd.exe every 1500 ms and calls StopMusic batch file to taskill all vbs scripts when
- ::: - cmd.exe process count is 0. Delay is to reduce CPU usage of the WMI service
- :makemonitor
- (
- ECHO Set objWMIService = GetObject ("winmgmts:"^)
- ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^)
- ECHO DO while proc.count ^> 0
- ECHO Set proc = objWMIService.ExecQuery("select * from Win32_Process Where Name='cmd.exe'"^)
- ECHO if proc.count ^< 1 then exit do
- ECHO wscript.sleep 1500
- ECHO loop
- ECHO Set WshShell=createobject("wscript.shell"^)
- ECHO WshShell.run "%MusicStopper%", 0, true
- )>"%Monitor%"
- Exit /B
- ::: \
- ::: / Simple taskkill script to terminate vbs scripts on program exit
- :makestopper
- (
- Echo.@ECHO OFF
- Echo.taskkill /pid WScript.exe /f /t ^>nul
- Echo.Goto :EOF
- )>"%MusicStopper%"
- Exit /B
- ::: \
- ::: **** End of Subroutines \
- :ConsolePROPS
- ::: / Call subroutine with desired console properties.
- ::: - Param 1: Lines (Y Axis)
- ::: - Param 2: Columns (X Axis)
- ::: - Param 3: Label Name to resume from
- ::: - Param 4: Optional. Aligns console at top left of screen instead of screen Centre (any 4th parameter will do this)
- Call :ChangeConsole 38 120 Main
- ::: / **** MAIN script body of your program goes below. **** \ :::
- :Main
- ::: / Delayed expansion required for the expansion of script within Macro variables
- Setlocal EnableDelayedExpansion
- ::: \
- ::: / Begins vbs monitor to call the taskkill script and end all vbs's once cmd.exe is closed.
- ::: - Must be started after resizing / aligning console
- ::: - Should be started before calling upon the music player
- Start "" "%Monitor%"
- ::: \
- %@CC%!red!& For %%B in ("Right aligned" "text display example") do %!!|%%%~B%@Align% R
- %@CC%!magenta!& %!!|%left aligned text%@Align% L
- %@CC%!light.blue!& %!!|%Centre aligned text.%@Align% C
- ::: / Music player call example
- ::: - volume must be an integer between 0 and 100
- Set "vol=100"
- ::: - loop value must be 'true' or 'false'
- Set "Loop_TF=true"
- ::: - a full and valid track path needs to be assigned to the variable in order to use the music player
- Set "track="
- ::: / Example usage of custom prompt for user input to variables. Doubleqouting of Params required for special Characters.
- ::: - If no input is given, the 3rd Arg, %~d0\Windows\Media\Ring01.wav will be set to the example track variable as a default
- ::: - %~d0\ assumes the windows directory and this batch share the same drive, without assuming it's on the C: drive.
- %!!|%"Enter a path for your song %yellow%:>"%@Input% track green %~d0\Windows\Media\Ring01.wav
- %@CC%!teal!& For %%B in ("You entered:" "%track%") do %!!|%%%~B%@Align% C
- ::: \
- ::: - Calls the music player with the desired sound, volume and repeat state if all variables set
- ::: - If conditions used to deactivate the call until a track path is provided.
- IF NOT "%track%" == "" (
- If Exist "%track%" (
- CALL "%player%" "!track!" !vol! !Loop_TF!
- ) Else ( %@CC%!red!& %!!|%No file at that filepath%@Align% C )
- ) Else (
- %@CC%!red!& %!!|%No track selected%@Align% C
- )
- ::: \
- ::: / Example of a custom pause prompt. 'letters' or 'words' are valid for Arg 1.
- %!!|%Example of a custom pause - The song [or ringing] will close next.%@hold% letters
- ::: \
- :end
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement