Page 1 of 1

VBScript I made to shutdown computer when playlist completes

Posted: 10 Jan 2010 15:10
by eyehawk78
Hey Everyone,

I made this script as I like to watch some of my programs before I go to bed, but most of the time I fall asleep and leave my computer on all night. Or I can't be bothered to shut my computer down at the end. Lazy I know.

Anyhoo, I've made this VBScript where you can drag and drop a bunch of files onto the script and set a shutdown time when it completes. You can also double click the file to build a playlist by hand, though it is much slower.

I figured this would be the best place to share it, so tell me what you think

N.B. supports avi, mp4, mov, wmv, 3gp files only thus far. But its pretty simple to add more (VLC Saved playlists [.xspf, .m3u, .html] do not work however)
N.B. Only tested on Windows XP but should work on all Windows OS

Code: Select all

'By eyehawk78, posted on http://forum.videolan.org/viewtopic.php?f=16&t=70391 Dim oShell, FSO, FileData, vlc_path, video_dir, user, programs Dim files, seconds force_shutdown = FALSE 'Set to true to force quit all other applications set to false otherwise Function SelectFile() Set file = CreateObject("UserAccounts.CommonDialog") file.Filter = "Video Files (avi, mp4, mov, wmv, 3gp)|*.avi;*.mp4;*.mov;*.wmv;*.3gp;" file.FilterIndex = 1 file.InitialDir = video_dir InitFSO = file.ShowOpen If InitFSO = True Then SelectFile = file.FileName Else WScript.Quit End If End Function Sub InputError(ErrorString) Wscript.Echo ErrorString Wscript.Quit End Sub files = "" Set FSO = CreateObject("Scripting.FileSystemObject") Set oShell = CreateObject("WScript.Shell") If Wscript.Arguments.Count > 0 Then For Each FileData In Wscript.Arguments Set FileInfo = FSO.GetFile(FileData) If InStr(FileInfo.Type, ".avi") or InStr(FileInfo.Type, ".mp4") or InStr(FileInfo.Type, ".mov") or InStr(FileInfo.Type, ".wmv") or InStr(FileInfo.Type, ".3gp") Then files = files & " " & CHR(34) & FileData & CHR(34) Else InputError("File " & CHR(34) & FileInfo.Name & CHR(34) & " has an unrecognised file type - Must be of type .avi, .mp4, .mov, .wmv or .3gp") End If Next Else user = oShell.ExpandEnvironmentStrings("%USERPROFILE%") video_dir = oShell.ExpandEnvironmentStrings("%VLC_SHUTDOWN_VIDEOS_DIRECTORY%") 'If this if first run, we must save where the default video directory is If video_dir = "%VLC_SHUTDOWN_VIDEOS_DIRECTORY%" Then video_dir = InputBox("Please input the directory where your Videos are kept." & vbcrlf & vbNewLine & "E.g. C:\Documents and Settings\User Name\My Documents\My Videos", "First Run", user) If video_dir <> "" Then strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_ objVariable.Name = "VLC_SHUTDOWN_VIDEOS_DIRECTORY" objVariable.UserName = "<System>" objVariable.VariableValue = video_dir objVariable.Put_ Else WScript.Quit End If End If answer = 6 'Loop while user wishes to add more files to playlist Do While answer = 6 files = files & " " & CHR(34) & SelectFile() & CHR(34) answer = MsgBox("Would you like to add another file to the playlist?", 3, "Continue?") Loop If answer = 2 Then WScript.Quit End If End If 'If this if first run, we must save where the default VLC directory is programs = oShell.ExpandEnvironmentStrings("%PROGRAMFILES%") vlc_path = oShell.ExpandEnvironmentStrings("%VLC_SHUTDOWN_VLC_LOCATION%") If vlc_path = "%VLC_SHUTDOWN_VLC_LOCATION%" Then vlc_path = InputBox("Please input the directory where VLC program file is kept." & vbcrlf & vbNewLine & "E.g. C:\Program Files\VideoLAN\VLC", "First Run", programs) If vlc_path <> "" Then strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set objVariable = objWMIService.Get("Win32_Environment").SpawnInstance_ objVariable.Name = "VLC_SHUTDOWN_VLC_LOCATION" objVariable.UserName = "<System>" objVariable.VariableValue = vlc_path objVariable.Put_ Else WScript.Quit End If End If vlc_path = CHR(34) & vlc_path & "\vlc.exe" & CHR(34) 'VLC directory location seconds = InputBox("Please enter the number of seconds the system should delay before commencing shutdown" & vbNewLine & vbNewLine & "You will be able to abort the shutdown during this time", "Enter Number of Seconds", "10") If seconds <> "" Then If IsNumeric(seconds) And seconds > 0 Then oShell.Run vlc_path & " " & files & " vlc://quit", 1, TRUE For i = Round(seconds) To 0 Step -1 intRet = oShell.Popup("Cancel the shutdown" & vbNewLine & vbNewLine & "Shutdown down in: "& i, 1, "Cancel Shutdown?", 4 + 32) If intRet = 6 Then 'Shutdown aborted oShell.Run "shutdown -a" oShell.Popup "Shutdown Aborted", , "Aborted", 0 + 64 Wscript.Quit Elseif intRet = 7 Then Exit For End If Next 'Shutdown now If force_shutdown Then oShell.Run "shutdown -s -f -t 0" Else oShell.Run "shutdown -s -t 0" End If Wscript.Quit Else InputError("Input not a number or negative") End If End If Wscript.Quit
Save the above code as a .vbs file

Note: Set line 6 to TRUE to force all other applications to quit before shutdown

Tell me what you think?

Re: VBScript I made to shutdown computer when playlist completes

Posted: 22 Jan 2010 10:14
by ColdfireTrilogy
Script works great in XP but unfortunately VISTA (or windows7) does not accept "UserAccounts.CommonDialog" as a valid creatobject function. To be honest I have no idea what to do to fix such a thing as I don't even remember what little VBscripting knowledge I had from years ago; so this VBscript ultimately only works on XP unless you are able to modify it to run in Vista as well which would be awesome since that is my primary OS.

Re: VBScript I made to shutdown computer when playlist completes

Posted: 25 Jan 2010 01:08
by eyehawk78
Ah that sucks, I've done a quick Google search and can't find any quick fix solutions (without opening Word or Internet Explorer in the background, which sound pretty dodgy to me).

Does dragging and dropping files on to the script work?

Re: VBScript I made to shutdown computer when playlist compl

Posted: 10 Apr 2010 17:22
by gedge11
I made this script a while ago. it's a bit unfinished but it works for windows 7. Drag files on to the script to play them and the computer will shutdown after.

Posted by Gedge11 on posting.php?mode=reply&f=16&t=70391

countargs = wscript.arguments.count - 1

set oshell = createobject("wscript.shell")

set fso = createobject("scripting.filesystemobject")

vlcpath64 = "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"

vlcpath32 = "C:\Program Files\VideoLAN\VLC\vlc.exe"

if fso.fileexists(vlcpath32) then

vlcpath = vlcpath32

elseif fso.fileexists(vlcpath64) then

vlcpath = vlcpath64

else

vlcpath = inputbox("Please enter the path to VLC media player" & chr(13) & chr(13) & "Example: " & chr(13) & "C:\Program Files\VideoLAN\VLC\vlc.exe","VLC is not installed in the default directory")

if not fso.fileexists(vlcpath) then

wscript.echo "invalid path"

wscript.quit

end if

end if

playlist = chr(34) & wscript.arguments.item(0) & chr(34)

for i = 1 to countargs

playlist = playlist & " " & chr(34) & wscript.arguments.item(i) & chr(34)

next

oShell.Run chr(34) & vlcpath & chr(34) & " " & playlist & " vlc://quit"

vlcstate = 0

loopProcesses

do while vlcstate = 0

loopProcesses

loop

do until vlcState <1

vlcstate = 0

loopProcesses

loop

CancelPressed = oshell.popup ("Shutting Down",20,"ATTENTION",1)

if cancelPressed = 2 then

wscript.echo "Shutdown canceled"

wscript.quit

else

oshell.run "shutdown -s -f -t 60 -c " & chr(34) & "shutting down since media has finished" & chr(34)

end if


sub loopProcesses

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.InstancesOf("Win32_process")

For Each objItem In colItems

if objitem.name = "vlc.exe" then

vlcState = 1

end if

Next

Set objWMIService = Nothing
Set colItems = Nothing
wscript.sleep 1000 'time in ms to check vlc

end sub