Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: How to close a software automatically when it's idle?

21 Sep 2016   #11
stan0ne

Windows 10 Pro
 
 

hello thank u for information.

but i have a program;
Any file opened with notepad, title is > "xxxx - Notepad"
in this case how the program will be shut down?

sorry my bad english..


My System SpecsSystem Spec
.
21 Sep 2016   #12
UsernameIssues

W7 Pro SP1 64bit
 
 

Quote   Quote: Originally Posted by stan0ne View Post
hello thank u for information.

but i have a program;
Any file opened with notepad, title is > "xxxx - Notepad"
in this case how the program will be shut down?

sorry my bad english..
Can you tell us why you would want to shut down any Notepad program?
My System SpecsSystem Spec
21 Sep 2016   #13
stan0ne

Windows 10 Pro
 
 

notepad is example. we have a domain build 300computer. my map program is limited license (netcad). nc32.exe I want to get close if not used.
My System SpecsSystem Spec
.

21 Sep 2016   #14
UsernameIssues

W7 Pro SP1 64bit
 
 

Quote   Quote: Originally Posted by stan0ne View Post
notepad is example. we have a domain build 300computer. my map program is limited license (netcad). nc32.exe I want to get close if not used.
Thanks for the additional information. I think that I understand your original question now: How can the script close a window if the window's title changes - depending on the name of the file that is opened in the program. I'll try and answer that in my comments below:

This is the code that I posted earlier:
Code:
Opt("WinTitleMatchMode", 3) ;3 = exact title match
Opt("TrayIconDebug", 1)

$S_running = "check-4-app" ;name the script
If WinExists($S_running) Then Exit
AutoItWinSetTitle($S_running)

$title = "Untitled - Notepad"
$count = 0
While 1
    $state = WinGetState($title)
    $count += 1

    If $state = 15 Or $state = 47 Or $state = 0 Then $count = 0

    If $count > 10 Then WinClose($title)

    ToolTip("count = " & $count, 0, 0, "state = " & $state)
    Sleep(1000)
WEnd
Below are my comments about some of the lines of code:

$title = "Untitled - Notepad"
This is the title of the window to be closed. You would change that to the title of the application that you want closed. If the title of the netcad window contains the name of the open file, then change this:
$title = "Untitled - Notepad"
to this:
$title = "- Notepad"
And change this:
Opt("WinTitleMatchMode", 3) ;3 = exact title match
to this:
Opt("WinTitleMatchMode", 2) ;2 = any substring

The goal would be to find text in netcad's window's title that does not change.


While 1
This starts a loop that does not end. It keeps the script running all of the time.


$state = WinGetState($title)
This gets the state of the window of interest.
The window could be minimized or behind another window or active or in some other state.


If $count > 6 Then WinClose($title)
If the counter exceed 6, then the window has not been active for 6 times thru the loop and the code will try to close the application. To make the video, I picked a low number like 6. That will try to close an inactive window in about 6 seconds. You would probably want to pick a value that is much larger than 6.


User can leave the netcad program open and its window active for hours without actually using the netcad program. If that happens then the script will probably not close the netcad program. If the operating system is set to lock the computer's screen after so many minutes of inactivity, then that might change the state of the netcad window. The netcad window might not be seen as active and the script would attempt to close the netcad program. That is true for Notepad. If a Notepad window is active when the computer is locked, then the Notepad window will no longer be active and the script tries to close Notepad.

The script uses WinClose. This is about the same as clicking the "X" in the upper right corn of most Windows programs. If there is unsaved work, most Windows programs prompt the user to save their work before the program will exit. The script will not force the program to close.

The script could be changed to save a user's work or the script can be changed to force the program to close and lose any unsaved work. If netcad automatically saves a user's work periodically, then WinClose might work just fine.

The code in that script is just a starting place. You would need to change the code to handle issues that you see come up in your use of the script. For example, you could add a timer to the script that will close the netcad program after some time interval - maybe 2 hours - no matter what the netcad's window's state is. You can warn the user that the script is going to close netcad - if the user responds, then the script won't try to close netcad.

The script could be changed to measure a user's inactivity (no typing and no mouse movements). It can close the netcad based on user inactivity - no matter what state the netcad window is in. Or the script could try to close the netcad program based on user inactivity and the window's state, whichever limit is reached first.

Lots of choices :-)
My System SpecsSystem Spec
22 Sep 2016   #15
stan0ne

Windows 10 Pro
 
 

thank you so much for the detailed information. i will use 'WinKill' for force close. document will not save , no problem. timeout is 2hours yes, 7200s

i will try

i was tried, thanks
but i change
Opt("WinTitleMatchMode", 2) to 1
and $title = "Netcad"
program so that it works properly.

thank u so much
My System SpecsSystem Spec
22 Sep 2016   #16
stan0ne

Windows 10 Pro
 
 

and another question for information;

What does this code?
If $state = 15 or $state = 0 Then $count = 0
why 15? what happen state=15 ?

and another question;
how to close multiple program? Is this possible?
e.g. winamp.exe 1hour, word.exe 2hours..

this is professional system o.O
My System SpecsSystem Spec
22 Sep 2016   #17
UsernameIssues

W7 Pro SP1 64bit
 
 

Quote   Quote: Originally Posted by stan0ne View Post
thank you so much for the detailed information. i will use 'WinKill' for force close. document will not save , no problem. timeout is 2hours yes, 7200s

i will try

i was tried, thanks
but i change
Opt("WinTitleMatchMode", 2) to 1
and $title = "Netcad"
program so that it works properly.

thank u so much
Opt("WinTitleMatchMode", 1)
[matches if "Netcad" is at the start of the window's title]

Opt("WinTitleMatchMode", 2)
[matches if "Netcad" is anywhere in the window's title]

If a file is open in Netcad, then Opt("WinTitleMatchMode", 1) should not work.
Here is a random/sample image from Google:
How to close a software automatically when it's idle?-title.png
Since the text "Netcad" is not at the start of the Window's title, the script will not find a match if you use Opt("WinTitleMatchMode", 1)

If you use Opt("WinTitleMatchMode", 2), then the script should match the window's title shown above. If it is your intent to only close the Netcad program if no file is open, then maybe Opt("WinTitleMatchMode", 1) will work for you.


I'll answer your second post/question about "state 15" in a bit. I need to post this reply before my attached image gets automatically deleted.


My System SpecsSystem Spec
22 Sep 2016   #18
UsernameIssues

W7 Pro SP1 64bit
 
 

Quote   Quote: Originally Posted by stan0ne View Post
and another question for information;

What does this code?
If $state = 15 or $state = 0 Then $count = 0
why 15? what happen state=15 ?

and another question;
how to close multiple program? Is this possible?
e.g. winamp.exe 1hour, word.exe 2hours..

this is professional system o.O
The original post in the thread asked to close a program that had not been used for a certain amount of time. If the person starts using the program before that time limit is reached, then the script resets the "not used" time to 0. This might not be what you want. You might want a two hour limit - no matter what the person using it is doing.

The line of code that you quoted should probably be:
If $state = 15 Or $state = 47 Or $state = 0 Then $count = 0
I have updated the two posts that contain the script.

I used WinGetState to get the state of the window of interest.
WinGetState returns these possible states:

$WIN_STATE_EXISTS (1) = Window exists
$WIN_STATE_VISIBLE (2) = Window is visible
$WIN_STATE_ENABLED (4) = Window is enabled
$WIN_STATE_ACTIVE (8) = Window is active
$WIN_STATE_MINIMIZED (16) = Window is minimized
$WIN_STATE_MAXIMIZED (32) = Window is maximized

Those states add up.

State 15 is for a window that is active (8), enabled (4), visible (2) and the window exists (1).

8 + 4 + 2 + 1 = 15.

I changed the script to also reset the time limit if the window of interest is full screened and active. A normal, active full screened window should be state 47 (15 + 32).


If you run the script just as I have posted it, you should see a tool tip in the upper left corner of your desktop/screen. The tool tip is just there to let you see what the script "sees". Run the script, open Notepad and watch that tool tip. Maximize the Notepad window and watch the tool tip. Minimize the Notepad window and watch the tool tip. You should be able to see the value of the script's "state" variable change as the Notepad window changes states. You should also be able to see the count change and reset. Once you get the script to do what you want, you can remark out or remove the tool tip line of code (if you want).


We can add other programs to the script after we are sure that the script is doing what you want.


AutoIt is a very professional scripting language, but I'm not a professional programmer. I've just been using AutoIt for many years. I'm sure that a professional programmer would write the AutoIt code better :-)
My System SpecsSystem Spec
Reply

 How to close a software automatically when it's idle?




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
I need a .VBS script for a tool to save and automatically close
i want to make netpass.exe window automatically close after make his job and save the password list in a .TXT file Can anyone help me with that please? here the pack to work and test U can downlaod the tool from here: https://www.sendspace.com/file/rcj5oz ...
General Discussion
How to schedule programs to start/close automatically
I want to have my Computer automatically start at 9pm, have utorrent start and run til midnight, then, at midnight, have steam start and run til 6am and have my computer shutdown then, so just to recap: 9PM Computer Start, Utorrent Starts 12AM Utorrent Closes, Steam Starts 6AM Computer Shutdown
General Discussion
How To Close (But Not Force Close) Program Automatically On A Schedule
not sure if it's possible in Task Scheduler but i really want it to work. Basically I know how to automatically start program in Task Scheduler and how to also force close it. If we want to force close it we just create a .bat file with text like TASKKILL /F /IM "nameofprogram.exe" and schedule to...
General Discussion
Is normal start menu close automatically after 1 min?
when I click on start menu and dont move the mouse after 1 minute is disappear the start menu. this normal.?
General Discussion
Program to automatically close and open other programs?
When I'm running on battery power, I'd like to be able to close certain programs to save power (e.g. that send a smile/frown thing for MS Office 2010 beta, Skype, MSN, my dictionary program etc), but instead of closing them one by one, which is a bit of a kerfuffle, I'd like to able to do it with a...
Software
Team Fortress 2 it will close after about 4 seconds idle?
DON'T WORRY I FIXED IT Thanks for your help anyway Regards Jack.
Gaming


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 05:50.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App