Jump to content
Sign in to follow this  

Closing Random Windows / Documents

Recommended Posts


:) Gee welcome to the help forums.... For my second post can someone please tell me what the heck just happened to my previous topic? I didn't put in everything I was trying to do in my original post as I was trying to spare you guys a novel since I've never posted anything before. I like to try to figure out things and I don't expect to have all the answers handed to me or code written for me.

If you want more detail I can provide it. I got busy at work today so I didn't get a chance to reply to anything until my topic had been locked several hours later. If I did something wrong please let me know. My original post is below.

The primary goal of all of this is lengthy. I'm working on creating a large script that will upgrade an older version of MS Office for our users. I've got the scripting side figured out to do the actual upgrade. I'd like the script to be able to close anything they have open and default save any word, excel, powerpoint, wordpad, notepad document they may have open. I'm not worried about IE windows and I'm DEFINITELY NOT TRYING TO DO ANYTHING MALICIOUS. I'm the guy that cleans that crap up so i'm not making more of it. I'd like to avoid multiple users screaming that I deleted the document they didn't save.

One catch to my Office upgrade is half the machines i'm working with need an upgrade and half need an uninstall / reinstall. The uninstall / reinstall I'm pretty sure is going to require a reboot after the uninstall part. Is it possible to have anything pick up after a reboot and continue with an install? I do realize there are probably other coding languages that could make this easier, though I am trying to learn more of AUTOIT and how it works so this is a learning experience too.

First post here, basically very new to scripting with this so any suggestions or direction would be awesome.

I'm not sure if what i'm trying to do is even going to be possible. It seems complex to me but might seem easier to someone with more experience.

I'd like to create a script that will first check what programs / windows are open on a computer and then close them. This will be completely random.. Computer A might have 3 IE windows open and 2 Word Docs, Computer B might have 0 IE windows open, 0 word docs and 1 excel workbook open.

I don't really care about the IE windows just outright closing. The documents i'd just like to do a default save on them and then close. This part doesn't seem like it would be too difficult. The randomness part has me a bit stumped. I was fiddling around with ALT F4 and some Enter commands to attempt to close and save everything that way.

I'm sure there has to be a better way if this is possible but I'm very new to coding this language. I'm open to giving anything a try and I've figured out some basics and written a nice install script already. Any direction on this would be greatly appreciated. I have searched around a good bit and read the FAQs before posting this topic. Just didn't come across anything that sounds similar to what I'm looking for.


Share this post

Link to post
Share on other sites

No need to kick off dude, I think your post got locked because of another.

Edited by Mobius


Share this post

Link to post
Share on other sites

So many things could go wrong, I think it would be better to simply ask the user(s) to close the application themselves

MsgBox(0, "Hello", "Please close all office documents!")

Share this post

Link to post
Share on other sites

I agree, many things could go wrong. But if you are still determined you should look into WinList.


Share this post

Link to post
Share on other sites

This doesn't seem like a very smart thing to do. If your "people" are working in something ( say spent 4 hours typing up a report for the boss ) and your product comes in and just arbitrarily shuts down their work ... well, that's ridiculous.

If your intentions are pure, then you need to just prompt your users that they still have script breaking executables open. Or just proceed with a message box as suggested ( to keep it simple ), saying that the install may fail if something was left open, the first time it fails, they'll be sure to save their work and close. But you taking responsibility for it is not a bright idea.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post

Link to post
Share on other sites

The best you can do is to check if any window exist and terminate/restart your script if this condition is met.


MsgBox(0, "Hello", "Please close all windows/documents!")

$var = WinList()

For $i = 1 to $var[0][0]
  ; Only display visble windows that have a title
  If $var[$i][0] <> "" AND IsVisible($var[$i][1]) And $var[$i][0] <> "Program Manager" And $var[$i][0] <> @ScriptName Then
    $win &= "--> "&$var[$i][0] & @CRLF

If $win <> "" Then
    MsgBox(0,"Opps..","You haven't closed all windows"&@CRLF&"Visible window(s):"&@CRLF&"------------"&@CRLF&$win&"------------"&@CRLF&"Exiting...")

; - your script here...

Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then 
    Return 1
    Return 0

Edited by Mison

Hi ;)

Share this post

Link to post
Share on other sites

Thank you all for the replies and the suggestions. I really wish I could rely on the end users to interact with this process but unfortunately I need something that won't require them to do anything. Ideally I'd love to do it the way some of you have suggested. This is mainly why I wanted to design something to save whatever they were working on at the time. I would say 98% of the time or better, they leave something open for hours and sometimes days at a time. It would actually be a very rare case if someone was working on a document that wasn't saved at the time I was planning to implement this if I can figure it out, though I still don't want anyone to lose anything.

Most of our users don't understand what we are capable of doing remotely and many don't follow even crystal clear instructions. I've tried a few user interaction things in the past and it was kind of a headache. The work I need to do is like pulling teeth a lot of the time. If they can avoid it or close it out they will. That's why I really like the lock keyboard and mouse features I used in my last script to push a video codec for mp4, it worked perfectly but it was pretty basic stuff.

Here's some of the code I've got going so far. Feel free to throw some advice out about what I have here. It's very basic and I just started getting into it a little bit today so yeah I know I have a long way to go still :) Let me know if my thought process is heading in the right direction.

;Testing Save Documents & Close Programs
If WinActive("[CLASS:IEFRAME]", "") Then Close()
If WinActive("[CLASS:Shell_TrayWnd]", "") Then SaveClose()  
Func Close()
WinClose("[ACTIVE]", "")

Func SaveClose()

I haven't written the SaveClose function yet. My thought process so far was to open a command prompt on the target workstation first. I chose command prompt as a starting point since no one will have a command prompt open. I can use this as my starting active window since it will automatically be active once it is opened. ALT TAB to the next open window (whatever that may be) and put in the different variables for closing windows by class. I can add a variable for every possible program they could have open on the workstation. Additionaly when I get to the window classes that I'm worried about saving I could do some ALT functions to save the files. Once the script cycles through the different ALT TABS once it reaches the command prompt again all the other windows should be closed.

Am I on the right track here hopefully? Any comments or suggestions on that?

Share this post

Link to post
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
Sign in to follow this