Friday, October 25, 2013

VBS Script to play random Halloween sounds at random intervals within a 10 minute window.

 This has been tested with Windows 7 and XP.
  1. You will need to download and install SMPlayer in "C:\Program Files\SMPlayer".
  2. You will need to get your own Halloween sound files and place them in a folder (minimum of 4 files).
  3. Save this script as "Halloween.vbs".  Edit the path to your files.
  4. Double-click the script file to run it.
'This program was written by Brian A. Hart
'10/25/2013
'This programs automatically plays or displays each file in the
'C:\Temp\Halloween folder
'It will not repeat the last 3 played

Dim fso, folder, files, WshShell, Return, FileCount, FileList(), FileNum, RandFile, PlayList(2), Played
 
'Get the folder info
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Temp\Halloween\")  'EDIT THIS!


'run the file(s) randomly
Randomize 'Init random number generator
do while true = true

    'load the files into the FileList array
    'We do this each time in case there are new files
    Set files = folder.Files
    ReDim FileList(CInt(files.Count - 1), 2)
    FileCount = 0
    for each folderIdx in files
        'load the file path/name
        FileList(FileCount, 0) = folderIdx.ParentFolder
        'load the file type
        FileList(FileCount, 1) = folderIdx.Type
        'load the short path
        FileList(FileCount, 2) = folderIdx.Name
        FileCount = FileCount + 1
        'msgbox(folderIdx.Type)
    next
   
    'Pick a random file
    RandFile = Int(((files.Count - 1) * Rnd) + 1)
    'msgbox(FileList(RandFile, 0) & " " & FileList(RandFile, 2))

    'Don't repeat the last three
    Do while InStr("," & Join(PlayList, ",") & ",", "," & RandFile & ",")
        RandFile = Int(((files.Count - 1) * Rnd) + 1)
        'msgbox(FileList(RandFile, 0) & chr(10) & RandFile & "-|-" & "," & Join(PlayList, ",") & "," & chr(10) & InStr("," & Join(PlayList, ",") & ",", "," & RandFile & ","))
    Loop

    'Run the file we picked
    Played = False
   
    'don't play if the file is locked
    'msgbox(fso.FileExists(FileList(RandFile, 0) & "\~$" & FileList(RandFile, 2)))
    if not (fso.FileExists(FileList(RandFile, 0) & "\~$" & FileList(RandFile, 2))) and not (Left(FileList(RandFile, 2),2)="~$") then
   
        'Is it MP3? (win 7)
        if FileList(RandFile, 1) = "MP3 Format Sound" then
            Set CmdExec = WshShell.Exec("C:\Program Files\SMPlayer\mplayer\mplayer.exe -really-quiet -fs " & """" & FileList(RandFile, 0) & "\" & FileList(RandFile, 2) & """")
            Do While CmdExec.Status = 0
                Wscript.Sleep 100
            Loop
            Played = True
        end if
       
        'Is it MP3? (win XP)
        if FileList(RandFile, 1) = "MP3 audio file (mp3)" then
            Set CmdExec = WshShell.Exec("C:\Program Files\SMPlayer\mplayer\mplayer.exe -really-quiet -fs " & """" & FileList(RandFile, 0) & "\" & FileList(RandFile, 2) & """")
            Do While CmdExec.Status = 0
                Wscript.Sleep 100
            Loop
            Played = True
        end if       
       
        'Is it Wave? (win 7 and XP)
        if FileList(RandFile, 1) = "Wave Sound" then
            Set CmdExec = WshShell.Exec("C:\Program Files\SMPlayer\mplayer\mplayer.exe -really-quiet -fs " & """" & FileList(RandFile, 0) & "\" & FileList(RandFile, 2) & """")
            Do While CmdExec.Status = 0
                Wscript.Sleep 100
            Loop
            Played = True
        end if
       
    end if 'locked?

    set CmdExec = Nothing

    if Played then
        'Keep track of the last 3 we played
        PlayList(0) = PlayList(1)
        PlayList(1) = PlayList(2)
        PlayList(2) = RandFile
    end if
   
    Randomize
    RandTime = Int(((600000 - 1) * Rnd) + 1)
    WScript.Sleep RandTime
Loop