Jump to content

Exit script after "While" loop


Recommended Posts

I've written a small script that should close a window that automatically pops up at logon. But I can't get it to exit after the window has been closed.

Can anyone see what's wrong with it?

$Title = "BGInfo"
While WinExists($Title)
   If WinExists($Title) Then
      WinActivate($Title)
      Send("{ESC}")
      Sleep(100)
   Else
      Exitloop
   EndIf
WEnd
Edited by scanie
Link to comment
Share on other sites

$Title = "BGInfo"
While WinExists($Title)
   If WinExists($Title) Then
      WinActivate($Title)
      Send("{ESC}")
      Sleep(100)
      Exitloop
   EndIf
WEnd

or alternatively:

bginfo /nolicprompt

Ok, so I don't need the 'else'?

I know about the switches for bginfo, although if you do the /taskbar switch it pops up automatically in logon, that's why I need autoit, to close that for me.

EDIT: Just tried your script and it worked as intended when I double clicked the file. But if another program gets in front BGInfo at logon it doesn't do anything.

Edited by scanie
Link to comment
Share on other sites

The 'else' will never be true - right?

You can also substitute the ExitLoop with an Exit statement.

True, I'm too tired for this..

I just can't understand what the problem is. All I want is to launch two programs at logon - Bginfo and an autoit script that closes the BGinfo popup.

I have managed to create 3-4 different scripts that all work, except that if another program gets in front of Bginfo it doesn't work at logon. But if I run the script manually after logon WITH Bginfo in the background it brings the window to front and then closes it..

Edited by scanie
Link to comment
Share on other sites

What exactly are you attempting to do with BGInfo that isn't working the way you want it to? I understand there's a popup window you're getting when you run it, which by the way doesn't happen for me if I use bginfo /taskbar. But what exactly is it doing that you don't want it to do?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I'll try to explain the best I can

in this script

$Title = "BGInfo"
While WinExists($Title)
    If WinExists($Title) Then
       WinActivate($Title)
       Send("{ESC}")
       Sleep(100)
    Else
       Exitloop
    EndIf
WEnd

if the popup is not there yet when autoit runs, it will hit the else condition (because the win does not exist) and exit. So when it does pop up, autoit is already done and can't close it.

for the 2nd part (it doesn't work if another window is on top of it)

you may try control sending the esc to the window instead of just send, or you could use WinSetOnTop before the activate and send.

Edited by kaotkbliss

010101000110100001101001011100110010000001101001011100110010000

001101101011110010010000001110011011010010110011100100001

My Android cat and mouse game
https://play.google.com/store/apps/details?id=com.KaosVisions.WhiskersNSqueek

We're gonna need another Timmy!

Link to comment
Share on other sites

What exactly are you attempting to do with BGInfo that isn't working the way you want it to? I understand there's a popup window you're getting when you run it, which by the way doesn't happen for me if I use bginfo /taskbar. But what exactly is it doing that you don't want it to do?

I'm using /taskbar /silent /timer:0 /nolicprompt. I have to put it in taskbar because our users are complaining about Win7 themes and backgrounds, they want to personalize their machines and they think that BGInfo on the wallpaper is interfering. If i launch BGInfo with these switches from the cmd prompt everything works OK, it ends up in the taskbar just as I want. But if I put it in the Startup folder it launches the popup automatically at logon.. That's why I created a little exe-file with autoit which is launched after BGInfo.

It works as long as anything else doesn't get in the popup's way.

I'll try to explain the best I can

in this script

$Title = "BGInfo"
While WinExists($Title)
    If WinExists($Title) Then
       WinActivate($Title)
       Send("{ESC}")
       Sleep(100)
    Else
       Exitloop
    EndIf
WEnd

if the popup is not there yet when autoit runs, it will hit the else condition (because the win does not exist) and exit. So when it does pop up, autoit is already done and can't close it.

for the 2nd part (it doesn't work if another window is on top of it)

you may try control sending the esc to the window instead of just send, or you could use WinSetOnTop before the activate and send.

The script works fine if anything is on top of it, at least when I do everything manually. I tested by putting several windows on top of the BGInfo window and then executed the script. It brought BGInfo to front and closed it. But if I do the same thing automatically with just two lines of batch code at logon, it doesn't work.

Thanks for the explanation btw!

Link to comment
Share on other sites

You either need a Sleep in there before the While loop, to give the BGInfo window time to open or use a WinWait with a timeout, before it to make sure the window is there when the script runs, if you run it right after running the program, the window may not exist yet, so the script ends.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You either need a Sleep in there before the While loop, to give the BGInfo window time to open or use a WinWait with a timeout, before it to make sure the window is there when the script runs, if you run it right after running the program, the window may not exist yet, so the script ends.

Yeah ok, I added a 5 second sleep before the While loop. But I still think that it should be possible to have the script running with a loop until it finds the BGInfo window, and then it closes it and exits the script. That's what I thought my Sleep(100) did, made the script run every second until it finds BGInfo.

But the sleep before the While loop seems to work, but I don't like hard coded sleeps..

Thanks for the help everyone! You all have received a "like".

Edited by scanie
Link to comment
Share on other sites

Instead of the Sleep, you could use the WinWait function to wait until the window exists, with a time out it will eventually stop waiting for the window, in case it doesn't exist, and then continue on with the script.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

This is just another way of doing what BrewManNH suggested.

Global $Title = "BGInfo"
AdlibRegister("CloseBGInfo")
While 1
   Sleep(1000)
WEnd
Func CloseBGInfo()
   If WinExists($Title) Then
   WinActivate($Title)
   Send("{ESC}")
   If WinExists($Title) <> 1 Then Exit ;Replace Exit with AdlibUnRegister("CloseBGInfo") to continue executing the script.
   EndIf
EndFunc
Edited by Decipher
Spoiler

censored.jpg

 

Link to comment
Share on other sites

  • 1 month later...

I'm still not satisfied with my results. I have tried the different suggestions in this thread.

I followed AoRaTos advice to launch BGInfo inside the .exe file. It works fine if I manually launch the .exe but when it starts automatically via Autostart it doesn't work. I think it's because other applications pop up at logon as well.

I have tried to put a sleep in the beginning of the script to make BGInfo launch after all the other stuff but it's not consistant. I need it to work all the time because the users are primarily doctors and nurses and they don't want to be bothered with unnecessary stuff.

These are the two scripts that both work when I manually launch the .exe:

Sleep(5000)
Run(@ComSpec & " /c " & "C:\BGInfo\Bginfo.lnk", @TempDir, @SW_HIDE)
Global $Title = "BGInfo"
AdlibRegister("CloseBGInfo")
While 1
Sleep(500)
WEnd
Func CloseBGInfo()
If WinExists($Title) Then
WinActivate($Title)
Send("{ESC}")
Exit
EndIf
EndFunc

and

Sleep(10000)
Run(@ComSpec & " /c " & "C:\BGInfo\Bginfo.lnk", @TempDir, @SW_HIDE)


$Title = "BGInfo"
Sleep(500)
While WinExists($Title)
If WinExists($Title) Then
WinActivate($Title)
Send("{ESC}")
Sleep(100)
Exitloop
EndIf
WEnd

It seems to me that the WinActivate command doesn't work.

Does anyone have some input or tips?

Link to comment
Share on other sites

Instead of using WinActivate and sending Escape, try using ProcessClose("bginfo.exe") and see if that works.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Instead of using WinActivate and sending Escape, try using ProcessClose("bginfo.exe") and see if that works.

Yes, that works. I have tried it. But then the app closes for good, I want it to stay in the system tray.

Good suggestion though! :D

Link to comment
Share on other sites

Why do you want it to stay running? BGinfo merely sets the desktop background image to whatever you've set it to display, once it's done that it has no further functionality. Also hitting escape when the window is open on my system closes it and the program stops running. I see nothing in the system tray even when it's running. What is in your shortcut that keeps it open, and what is it doing that you need it to stay running for?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Why do you want it to stay running? BGinfo merely sets the desktop background image to whatever you've set it to display, once it's done that it has no further functionality. Also hitting escape when the window is open on my system closes it and the program stops running. I see nothing in the system tray even when it's running. What is in your shortcut that keeps it open, and what is it doing that you need it to stay running for?

The organization that hires me doesn't want BGInfo on the wallpaper. They want in the taskbar. BGInfo supports this but it only works as intended on XP. On XP the application starts minimized to the taskbar automatically. On Win7 it starts in a window AND in the taskbar, if you close the window it stays in the taskbar but if you kill the process you close both the window and the icon in the taskbar.

C:BGInfoBginfo.exe C:BGInfotaskbar_settings.bgi /taskbar /timer:0 /nolicprompt
Link to comment
Share on other sites

Sleep(10000)
Run(@ComSpec & " /c " & "C:\BGInfo\Bginfo.lnk", @TempDir, @SW_HIDE)


$Title = "BGInfo"
Sleep(500)
While WinExists($Title)
If WinExists($Title) Then
WinActivate($Title)
Send("{ESC}")
Sleep(100)
Exitloop
EndIf
WEnd
The loop will not even start with WinExists(). Instead use WinWaitActive().

Run(@ComSpec & " /c " & "C:\BGInfo\Bginfo.lnk", @TempDir, @SW_HIDE)
$Title = "BGInfo"
WinWait($Title, "", 60)
WinActivate($Title)
WinWaitActive("title", "", 10)
Send("{ESC}")

Autoit will do all the looping internally.

Link to comment
Share on other sites

Yes, that works. I have tried it. But then the app closes for good, I want it to stay in the system tray.

Good suggestion though! :D

WinClose() ? Should be the same as hitting ESC or the window X

Edited by mechaflash213
Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...