Stopping Vbscript After Completion - vbscript

Im making a game called cube, when I noticed a problem. I looked at Task Manager Fr Any Extra Programs, when I noticed tons of vbsscript running. I tried searching around, but no scripts worked for me. Heres my code (It plays A Sound)
Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "Sounds\Hurt.mp3"
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep (int(Sound.currentmedia.duration)+1)*1000
WScript.Quit 1
It still doesn't want to stop. Please Help.

Try the below example
With CreateObject("WMPlayer.OCX.7")
.URL = "C:\Users\DELL\Desktop\tmp\hit.wav"
.controls.play
Do
WScript.Sleep 100
Loop Until .playState = 1 Or .playState = 10
' .PlayState https://msdn.microsoft.com/en-us/library/windows/desktop/dd564085(v=vs.85).aspx
' "Stopped" (completed) or "Ready" (file not found)
End With

Related

VBScript behaves weirdly using SendKeys to send a new line

I'm working on a very simple VBS script and I've ran into a really weird problem:
I was originally using this code:
Set wshShell=wscript.CreateObject("WScript.Shell")
WScript.Sleep(2000)
Dim i
i = 0
Dim val
val = 25
Do While i < 5
wshshell.SendKeys("What the")
WScript.Sleep(val)
wshshell.SendKeys("{ENTER}")
WScript.Sleep(val)
i = i + 1
Loop
But for some reason, the output was this:
What theWhat theWhat theWhat theWhat the
But when I change this:
wshshell.SendKeys("What the")
To this:
wshshell.SendKeys("What the ")
It works fine and outputs:
What the
What the
What the
What the
What the
What's causing that? I'm extremely baffled as to why that happens. Thanks.

Switch between IE browser tabs for specific time intervals using script

Currently I'm using IE 11, I have 4 displays opened. I want to toggle between the tabs for specific intervals of time. We want this setup for monitoring purpose. I need a script for this task.
Kindly refer the following code :
set shellApp = createobject("shell.application")
do
for each sTitle in Array("v9", "Google", "Gmail", "ETC")
ShowIEWindow sTitle, shellApp, 10 ' sec
next
loop ' forever
sub ShowIEWindow(sTitle, oShell, nWaitsec)
for each w in oShell.windows
with w
if lCase(.LocationName) = lcase(sTitle) and InStr(lCase(.FullName),"iexplore") > 0then
w.Refresh
'w.visible = true ' show
wsh.sleep nWaitsec * 1000 ' milliseconds
' w.visible = false ' hide
end if
end with
next
end sub
Above code will refresh tabs that specified in the array if they are opened in internet explorer.
Now, instead of Refreshing the window, you need to find method that switches between different tabs.
I also tested the above code and working for me. Hope this will helps !! :)

It's Making A Continuous Loop!!! What Is Wrong?

Ok, so I have been given the task to create a script that can increase or decrease the volume. My problem is when I run it, and type in "Decrease" then type in "29" it goes down to 0 then starts to loop. Can you please tell me where the loop is and how to fix it?
set Keys = CreateObject("WScript.Shell") 'So The Script Can Simulate Key Presses
set oShell = CreateObject("WScript.Shell") 'So The Script Can Control The Master Volume
'Asks The User If They Wish To Increase Or Decrease The Volume
Answer = InputBox("Increase Or Decrease Volume?", "Increase/Decrease Volume:")
If Answer = "Increase" Then 'If The User Types In Increase The Following Happens
'Runs The Master Volume App.
oShell.run"%SystemRoot%\System32\SndVol.exe"
'Stops The Program For # Milliseconds
WScript.Sleep 1500
'Asks How Much To Increase The Volume By
Amount = InputBox("How Much Do You Want To Turn The Volume Up?", "Increment:")
'Pushes the Up Arrow Key The Amount Of Which The User Entered
For X = 0 To Amount Step 1
'Simulates The Pushing Of The Up Arrow
Keys.SendKeys("{Up}")
X =+ 1 'Counter Increment
Next
ElseIf Answer = "Decrease" Then 'If The User Types In Decrease The Following Happens
'Runs The Master Volume App.
oShell.run"%SystemRoot%\System32\SndVol.exe"
'Stops The Program For # Milliseconds
WScript.Sleep 1500
'Asks How Much To Decrease The Volume By
Amount = InputBox("How Much Do You Want To Turn The Volume Down?", "Decrement:")
'Pushes the Down Arrow Key The Amount Of Which The User Entered
For X = 0 To Amount Step 1
'Simulates The Pushing Of The Down Arrow
Keys.SendKeys("{Down}")
X =+ 1 'Counter Increment
Next
ElseIf Answer = "" Then 'If The User Pushes Cancel The Following Happens
Question = MsgBox("Do You Wish To Quit?",vbYesNo,"Quit:")
'If The User Pushes Yes Then The Script Will End
If Question = vbYes Then
WScript.Quit 0 'Stops The Script
End if
Else
MsgBox("The Values Allowed Are:" & vbNewLine & "Increase" & vbNewLine & "Decrease")
End If
Does VBScript have Increment Operators
X = X + 1 is the proper way of achieving what you're trying to do with X =+ 1 (which may just be setting X to 1 over and over again). In your usage however, you can take those lines out completely since the For X = 0 To Amount Step 1 should already be handling the increment for you.
There is no =+ (add and assign) operator in VBScript. Your
X =+ 1 'Counter Increment
is seen as
X = +1 ' assign +1 to X
Evidence:
>> X = 10
>> X =+ 1
>> WScript.Echo X
>>
1
You should delete those lines as the loop variable in a For To statement updates automagically.

How do I logout from vbscript?

I'm using a VBScript to run an application on my Win Server 2003, and I want it to log the user off after a set amount of time. Something along the lines of:
Set WshShell = WScript.CreateObject("WScript.Shell")
Set OExe = WshShell.exec("somecommand.exe")
WScript.Sleep 1000000
OExe.Terminate
<Insert LogOff code>
Something like
WshShell.Run "C:\windows\system32\shutdown.exe /l", 0, false
should do the trick
Wscript.Sleep(100000)
SET wshell = Wscript.CreateObject("Wscript.Shell")
wshell.exec("shutdown.exe -L -F")
Just tested this on a w7 box, seems to work alright.
Example using WMI:
Set oSystems = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
For Each oSystem in oSystems
'LOGOFF = 0
'SHUTDOWN = 1
'REBOOT = 2
'FORCE = 4
'POWEROFF = 8
oSystem.Win32Shutdown 0
Next

Call out to script to stop with attribute in wWWHomePage

I'm gettinga n error message in line 8 when I try to call out the script to stop when it finds teh attribute in the Web page: field in AD.
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUserDN)
strwWWHomePage = objItem.Get("wWWHomePage")
If wWWHomePage 6 Then
wscript.quit
Else
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = True
ppt.Presentations.Open "\\abngan01\tracking\ppt.pptx"
End If
You have:
If wWWHomePage 6 Then
I'm assuming you want it to say:
If wWWHomePage = 6 Then
Since the missing "=" will cause an error, but since that code really doesn't do anything anyway, other than just abort the script, you could simplify your code by only taking action if that value is not set, for example:
If objItem.Get("wWWHomePage") <> 6 Then
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = True
ppt.Presentations.Open "\\abngan01\tracking\ppt.pptx"
End If
I'm also assuming "6" is some sort of flag you've set yourself, you might want to use something a little more descriptive like "PPTSTATUS006", or something along those lines.

Resources