Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How can I use wscript.sleep from Internet Explorer menu script?

570 views
Skip to first unread message

Grant Schenck

unread,
Mar 23, 2006, 6:12:10 PM3/23/06
to
Hello,

I have a VB script which is called by internet explorer in response to a
context menu selection. I used the techinques in this article:

http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/ext/tutorials/context.asp

In my script, I can do lots of things but unlike VB scripts run from the
command line I can't do things like "wscript sleep 1000". If I try I get
"Error: Object required: 'WScript'".

So how can I use wscript features from an Internet Explorer VB script?

Thanks!

Grant Schenck


JTW

unread,
Mar 23, 2006, 8:06:55 PM3/23/06
to
If you're running a client-side VB script within the browser, the WSH
objects aren't available because you're not using the WSH to execute
the code. Although I could be wrong, I am not aware of any object
available to VBS within the browser that allows for the non-processor
intensive sleep function.

Michael Harris (MVP)

unread,
Mar 23, 2006, 8:21:05 PM3/23/06
to
JTW wrote:
>... Although I could be wrong, I am not aware of any object

> available to VBS within the browser that allows for the non-processor
> intensive sleep function.

Actually, ScriptX (from www.meadroid.com) has a basic free feature subset
(no license required except for mostly advanced printing/page layout
features). In the free subset is a Wait method that behaves like
WScript.Sleep.

--
Michael Harris
Microsoft MVP Scripting


"Crash" Dummy

unread,
Mar 23, 2006, 8:45:53 PM3/23/06
to

You are correct that there is no native "sleep" time function available, but
there are third party controls that can be installed. I use "ASPTime.dll," which
has been around for years. Although as the name implies, it is intended for
server side use, it works equally well for client side script.

http://www.cs.niu.edu/%7Ez951259/comlinks.html

There are probably other, newer, fancier ones around, but if it ain't broke...
--
Crash

"You can't build a reputation on what you're going to do."
~ Henry Ford ~


mayayana

unread,
Mar 23, 2006, 11:44:04 PM3/23/06
to
WScript is not in your scope because the WSH
is not running the script. But you can get around
that if it's a local page where security is not a
problem. You can put a function in your called
script that creates a WScript.Shell object and
uses the Run method to run a second script. In the
second script you call sleep. Control doesn't
return to the calling script until the second script
is done, so it works.

Sub to call sleep method:

Public Sub Sleep(MSecs)
Dim Ret, SH
Set SH = CreateObject("WScript.Shell")
On Error Resume Next
Ret = SH.Run("sleeper.vbs " & MSecs, , True)
Set SH = Nothing
End Sub
--------------------------------------

The second script, named "sleeper.vbs" needs to be
in the same folder. Its content is like so:

Dim Arg
on error resume next
Arg = WScript.Arguments(0)
wscript.sleep Arg

To use the method, just call the following from within
your first script:

Sleep x
'-- where x is number of milliseconds.

Grant Schenck

unread,
Mar 24, 2006, 7:23:10 AM3/24/06
to
Thanks! I'd come to the same conclusion so it is good to have it confirmed.

I kind of worked around it by invoking a second script from the first:

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run strCommandLine, 0

However, I'm now wondering if there is some way to use the WScript shell to
invoke commands like Sleep().

Any ideas?

Regards,

Grant Schenck

"JTW" <News...@Dx21.com> wrote in message
news:e11T$8tTGH...@TK2MSFTNGP09.phx.gbl...

Grant Schenck

unread,
Mar 24, 2006, 7:25:25 AM3/24/06
to
What I did was launch a second vbs from the client VB script like this:

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run strCommandLine, 0

Then in the new script I can do sleeps just fine.

However, I was wondering if there was some way to create the WScript shell
and use it from the client script to somehow invoke sleep. This way I can
have one script rather then two.
--
Grant Schenck
http://grantschenck.tripod.com

""Crash" Dummy" <dva...@deathstar.mil> wrote in message
news:%23%23GhwSuT...@tk2msftngp13.phx.gbl...

Csaba Gabor

unread,
Mar 24, 2006, 2:14:33 PM3/24/06
to
Grant Schenck wrote:
> Hello,
...

> In my script, I can do lots of things but unlike VB scripts run from the
> command line I can't do things like "wscript sleep 1000". If I try I get
> "Error: Object required: 'WScript'".
>
> So how can I use wscript features from an Internet Explorer VB script?

If you are just interested in a non processor intensive sleep, then an
alternate approach is to use (the javascript) window.setTimeout the IE
provides. Of course it means an architectural change in your function
because your function will have to stop at the point that it starts to
sleep, and then start again (or have another one start) when the
setTimeout fires. But that's very often the appropriate architecture
anyway. Course I don't recommend this cross language hopping if you
can avoid it...

Here's a starter script to get you going:

<script type='text/javascript'>
function waitFor(millisecs) {
alert(millisecs);
window.setTimeout(momDispatch, millisecs); }
function momDispatch() {
mom2(" Brush your teeth"); }
</script>

<script type='text/vbscript'>
Dim savedVal
Function mom()
MsgBox "Hi mom - please wait"
savedVal = "Honey, I'm baaaaack"
waitFor(1000) 'in milliseconds
End Function
Function mom2 (jsVal)
MsgBox savedVal + vbCrLf + jsVal
End Function
mom
</script>


Csaba Gabor from Vienna

wimZ

unread,
Mar 26, 2006, 6:06:33 PM3/26/06
to
I use the timer for this.

you'll have to cut the process in two (or more if you want to use the sleep
more often.
Or alternatively make your function state driven, which so that it can be be
called multiple times and depending on a global state will process a
different section of your code.
Below is an example of how I do it, All you need to do is replace
YourFunction in the function ItsTime and from your code you call
StartProcess(Yourfunction) and

(I have clipped it out of existing code, I think it will function as is, but
maybe overlooked something.


dim gStatusTimer
Function StartStatusTimer
if gStatusTimer = 0 Then
gStatusTimer = window.setInterval("ItsTime()", 1000)
End If
End Function

Function StopStatusTimer
if gStatusTimer <> 0 Then
window.clearInterval gStatusTimer
gStatusTimer = 0
end If
End Function

Dim gProcessName
Sub StartProcess(sProcessName)
gProcessName = sProcessName
End Sub

Function AtLeastOneMachineInstalling
AtLeastOneMachineInstalling = True
End Function

Function ItsTime
'Stop the timer to avoid a timer event during a lengthy process.
StopStatusTimer

If Len(gProcessName) > 0 Then
sTemp = gProcessName
gProcessName = ""

Select Case sTemp
Case "YourFunction"
YourFunction
Case Else
Msgbox "Programming error: process called: " & sTemp
End Select
End If

StartStatusTimer
End Function


"Grant Schenck" wrote:

> .
>

Grant Schenck

unread,
Mar 27, 2006, 10:48:18 AM3/27/06
to
This looks like a possible solution, I'm not familiar with using timers from
VB script.

Based on the code below, I'm not clear what the script is doing while this
periodic timer events happen?

For example, presumably after I kick off some activity which I want to check
up on later I call your StartStatusTimer function. Then what do I do? What
does the script wait on so that the timer interval task can get control?

By the way, the COM object I'm interfacing with generates an event. Can
somehow handle that directly from VB script and if so, again, what does is
my script doing when it is in the mode waiting for events?

Thanks!

"wimZ" <wi...@discussions.microsoft.com> wrote in message
news:1249C51B-CDDE-4110...@microsoft.com...

Highlander

unread,
Mar 28, 2006, 10:12:04 AM3/28/06
to

t0 = timer
ccSleep 5
MsgBox "Sleep duration: " & vbCrlf & vbCrlf & timer - t0 & " seconds."
Sub ccSleep(seconds)
set oShell = CreateObject("Wscript.Shell")
cmd = "%COMSPEC% /c ping -n " & 1 + seconds & " 127.0.0.1>nul"
oShell.Run cmd,0,1
End Sub

Obtained from the Rube Goldberg Memorial Scripting Page:
http://www.mvps.org/scripting/rube/index.htm

Look for "A Synthetic Sleep Function."

HTH.

- Dave

Robin P

unread,
Mar 28, 2006, 10:19:02 AM3/28/06
to

In 6.0 you can simulate a sleep with javascript:
function pause(numberMillis) {
var dialogScript = 'window.setTimeout(' + ' function () { window.close();
}, ' + numberMillis + ');';
getWindow().showModalDialog('javascript:document.writeln(' + '"<script>' +
dialogScript + '<' + '/script>")');
}

this is the only real working sleep function.

The 'simulation' with timer functions is not a real sleep at all.
(Unfortunately.)

Well, in IE7.0, the sleep function doesn't work anymore. So maybe you
shouldn't use it. I use it and probably a have to do a total rebuild of my
application when IE7.0 has no sleep functionality anymore.

(I wonder why in al those years no standard sleep function is built in IE
(and FireFox),
with all the asynchronous XML requests and form post we lack a sleep
function.)

Grant Schenck

unread,
Mar 28, 2006, 4:36:02 PM3/28/06
to
Any idea of how I'd do this from a VB Script?
--
Grant Schenck

"Robin P" <Rob...@discussions.microsoft.com> wrote in message
news:26066040-0A27-4C7E...@microsoft.com...

Robin P

unread,
Mar 29, 2006, 5:39:02 PM3/29/06
to
Here it is in VBScript:
but I warn you, at the moment it is not working in IE7.0
(how can we make it work in IE7.0????)


#### pause.html #####
<HTML>
<BODY onload="test()">
<SCRIPT LANGUAGE="VBScript">
Function pause(numberMillis)
Dim dialogScript
dialogScript = "window.setTimeout(" & " function () { window.close(); }, "
& numberMillis & ");"
window.showModalDialog("javascript:document.writeln(" & """<script>" &
dialogScript & "<" + "/script>"")")
End Function

Function test()
window.alert("Start")
pause (3000)
window.alert("End after (about) 3 seconds")
End Function
</SCRIPT>
</BODY>
</HTML>

#####################
(javascript: is now used but there is no doubt it could be converted to
vbscript:
but this is just an example...

good luck!

Grant Schenck

unread,
Mar 30, 2006, 9:26:48 AM3/30/06
to
I'm running version 6.0 but I get "object doesn't support this property or
method: 'window.showModalDialog'.

Any ideas?

Thanks
--
Grant Schenck

"Robin P" <Rob...@discussions.microsoft.com> wrote in message

news:5854883F-54CA-4A57...@microsoft.com...

Robin P

unread,
Mar 31, 2006, 6:26:02 AM3/31/06
to
No,

maybe you did a copy and paste, make sure the newlines are at the right
place( they are not at the right place after copy/paste)
it works for me (in IE 6.0)

so it should work for you.

0 new messages