Jump to content



Photo

Exit script after "While" loop


  • Please log in to reply
34 replies to this topic

#1 scanie

scanie

    Seeker

  • Active Members
  • 23 posts

Posted 24 May 2012 - 01:27 PM

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, 24 May 2012 - 01:28 PM.








#2 hannes08

hannes08

    my oh my

  • Active Members
  • PipPipPipPipPipPip
  • 930 posts

Posted 24 May 2012 - 01:29 PM

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

or alternatively:
bginfo /nolicprompt

Edited by hannes08, 24 May 2012 - 01:30 PM.

  • scanie likes this
Regards,Hannes
Spoiler

#3 scanie

scanie

    Seeker

  • Active Members
  • 23 posts

Posted 24 May 2012 - 01:37 PM

$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, 24 May 2012 - 01:41 PM.


#4 hannes08

hannes08

    my oh my

  • Active Members
  • PipPipPipPipPipPip
  • 930 posts

Posted 24 May 2012 - 01:42 PM

The 'else' will never be true - right?
You can also substitute the ExitLoop with an Exit statement.
  • scanie likes this
Regards,Hannes
Spoiler

#5 scanie

scanie

    Seeker

  • Active Members
  • 23 posts

Posted 24 May 2012 - 01:50 PM

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, 24 May 2012 - 01:51 PM.


#6 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 24 May 2012 - 02:06 PM

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?
  • scanie likes this

How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#7 kaotkbliss

kaotkbliss

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 2,106 posts

Posted 24 May 2012 - 02:59 PM

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, 24 May 2012 - 03:00 PM.

  • scanie likes this
Posted Image010101000110100001101001011100110010000001101001011100110010000001101101011110010010000001110011011010010110011100100001My first project Allin1 Program launcherBill CalculatorGot <Insert problem here>?There's an AutoIt script for that!

#8 scanie

scanie

    Seeker

  • Active Members
  • 23 posts

Posted 24 May 2012 - 06:58 PM

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!

#9 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 24 May 2012 - 07:06 PM

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.
  • scanie likes this

How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#10 scanie

scanie

    Seeker

  • Active Members
  • 23 posts

Posted 25 May 2012 - 07:40 AM

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, 25 May 2012 - 07:41 AM.


#11 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 25 May 2012 - 12:45 PM

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.

How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#12 Decipher

Decipher

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 337 posts

Posted 25 May 2012 - 10:54 PM

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, 25 May 2012 - 10:56 PM.

Posted Image

#13 AoRaToS

AoRaToS

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 324 posts

Posted 26 May 2012 - 10:14 PM

why don't you use your .exe to launch BGInfo using cmd with the switches?

s!mpL3 LAN MessengerCurrent version 2.9.7.7 [15/02/2013]Download HeresimpL3


#14 scanie

scanie

    Seeker

  • Active Members
  • 23 posts

Posted 28 June 2012 - 09:19 AM

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?

#15 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 28 June 2012 - 01:27 PM

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

How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#16 scanie

scanie

    Seeker

  • Active Members
  • 23 posts

Posted 29 June 2012 - 07:30 AM

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

#17 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 29 June 2012 - 01:00 PM

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?

How to ask questions the smart way!

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 editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#18 scanie

scanie

    Seeker

  • Active Members
  • 23 posts

Posted 29 June 2012 - 01:21 PM

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


#19 John

John

    Polymath

  • Active Members
  • PipPipPipPip
  • 223 posts

Posted 29 June 2012 - 02:45 PM

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.

#20 Mechaflash

Mechaflash

    Sons of Anarchy

  • Active Members
  • PipPipPipPipPipPip
  • 1,358 posts

Posted 29 June 2012 - 03:12 PM

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, 29 June 2012 - 03:12 PM.

“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.”




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users