How Can I Minimize or Hide an HTML Application for 30 Seconds?

How Can I Minimize or Hide an HTML Application for 30 Seconds?

  • Comments 4
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I minimize or hide an HTA for 30 seconds?

-- MF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MF. You know, with the possible exception of baseball, the Scripting Guy who writes this column rarely claims to know much about anything. And for good reason: every time he thinks he knows something, well ….

Take your question, for example. Minimize or hide an HTA? No problem, he thought. After all, HTA’s have a property named windowStyle that can be used to configure an HTA to run minimized, maximized, or in a normal window. And if that’s not good enough for you, then you can simply set the Visible property of the HTA to False. Take your pick.

So, sure, take your pick; just don’t expect either choice to solve your problem. As it turns out, HTAs don’t have a Visible property, something this Scripting Guy would have realized had he bothered to think about this question for a second. As for the windowStyle property, well, that property does exist; however, it can be configured only at startup time. After the HTA is up and running you can no longer change the windowStyle property; that means you can’t easily switch between, say, a minimized window and a normal window.

Uh-oh.

In other words, this Scripting Guy is no genius. (Well, the Scripting Editor thinks he is. But he’s really not.)

Editor’s Note: We’re not sure where the Scripting Guy got the impression anyone thinks he’s a genius, let alone the Scripting Editor. Could it have been the baffled looks, or the comments like “What are you talking about?” and “Are you out of your mind?” We’re not sure, but he’s on vacation this week and won’t see this, so if it makes him happy we’ll let him remain in his own imaginary little world. And we’ll know that the next time we say to him “Do you have a brain?” he’s taking it as a compliment.

On the other hand, there is one advantage to being dumb: you’re not smart enough to know when you’re licked. Maybe we can’t minimize or hide an HTA, but we can do this:

<html>

<SCRIPT Language="VBScript">

    Dim intLeft
    Dim intTop

    Sub HideWindow
        intLeft = window.screenLeft
        intTop = window.screenTop
        window.moveTo -2000,-2000
        idTimer = window.setTimeout("ShowWindow", 5000, "VBScript")
    End Sub

    Sub ShowWindow
        window.moveTo intLeft,intTop
        window.clearTimeout(idTimer)
    End Sub
</SCRIPT>

<body>
    <input type="button" value="Hide Window" onClick="HideWindow">
</body>
</html>

If you run this script and click the button labeled Hide Window, the HTA will disappear. However, that’s not because we’ve minimized the window or because we’ve made the application invisible; instead, we’ve simply moved the HTA clear off the screen. After a five-second pause (a time interval you can modify as needed), we restore the window to its previous location. Not bad, huh?

Let’s see if we can figure out how this thing works. To begin with, the body of the HTA contains just one thing -- a single button labeled Hide Window:

<input type="button" value="Hide Window" onClick="HideWindow">

Click this button, and you’ll run a subroutine named HideWindow. That subroutine looks like this:

Sub HideWindow
    intLeft = window.screenLeft
    intTop = window.screenTop
    window.moveTo -2000,-2000
    idTimer = window.setTimeout("ShowWindow", 5000, "VBScript")
End Sub

As you can see, we don’t really do much in this HTA. We start out by assigning values to a pair of global variables named intLeft and intTop:

intLeft = window.screenLeft
intTop = window.screenTop

What we’re doing here is making a note of the upper (window.screenTop) left-hand (window.screenLeft) corner of the HTA window. Why do we do that? That’s easy: by knowing the location of the upper left-hand corner we’ll be able to restore the window to its current spot. After grabbing this information we then move the window off the screen:

window.moveTo -2000,-2000

How do we know this moves the window off the screen? Well, the upper left-hand corner of the screen has the coordinates 0, 0. A window with coordinates -2000, -2000 is going to be way off the screen.

With the window no longer visible this would probably be the spot where you would run whatever code you need the HTA to run. Instead of putting some phony-baloney code in here we simply set a timer that pauses the HTA for 5 seconds (5,000 milliseconds) and then calls a subroutine named ShowWindow:

idTimer = window.setTimeout("ShowWindow", 5000, "VBScript")

Note. Did someone say they didn’t understand how the timer-setting code works? If so, take a look at this Hey, Scripting Guy! column for more information.

And what does the ShowWindow subroutine do? Actually, it does two things. First, it moves the window back to its original location:

window.moveTo intLeft,intTop

And then it clears the timer:

window.clearTimeout(idTimer)

Do we really need to clear the timer? Well, it’s a pretty good idea; otherwise every 5 seconds the timer is going to fire and move the window back to its original position.

Give this a try, MF, and see if it helps; we think it will. And that’s a good point: imagine what a cool solution we could have come up with had we actually know what we were doing.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • kinda "thinking-outside-the-box"...

  • great!...

  • Me again, just found out a straight forward way to do this. If the HTA form is your active window (which in most case it is), then you could simply use the minimize button! just like below:

    Set oShell = CreateObject("WScript.Shell")

    oShell.SendKeys "% n"

    hope this helps..

  • how to make this window active on top of other windows. After 5,000 milliseconds, it pops up at the backgroud . how to bring it front?