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

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 4
    Windows 10 Pro
       #11

    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 Computer


  2. Posts : 10,485
    W7 Pro SP1 64bit
       #12

    stan0ne said:
    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 Computer


  3. Posts : 4
    Windows 10 Pro
       #13

    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 Computer


  4. Posts : 10,485
    W7 Pro SP1 64bit
       #14

    stan0ne said:
    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
    Last edited by UsernameIssues; 22 Sep 2016 at 09:49. Reason: changed code to - If $state = 15 Or $state = 47 Or $state = 0 Then $count = 0
      My Computer


  5. Posts : 4
    Windows 10 Pro
       #15

    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
    Last edited by stan0ne; 22 Sep 2016 at 01:27. Reason: add comment
      My Computer


  6. Posts : 4
    Windows 10 Pro
       #16

    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
    Last edited by stan0ne; 22 Sep 2016 at 03:52. Reason: word correction, add comment
      My Computer


  7. Posts : 10,485
    W7 Pro SP1 64bit
       #17

    stan0ne said:
    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 Computer


  8. Posts : 10,485
    W7 Pro SP1 64bit
       #18

    stan0ne said:
    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 Computer


  9. Posts : 1
    Windows 7 Pro
       #19

    Hi all,

    Sorry to resurrect an old thread, but I've got a problem using the above script and was hoping for a bit of help.

    I'm using the script above to detect an idle program and close it if idle for more than 1 hour, and it works wonderfully, except when a user opens two instances of the application - I'd like the script to detect the second instance and close it if it's idle in the background, but at the moment if one instance is active, then the second instance is not detected as idle and closed.

    Any advice would be appreciated.

    Thanks,
    jaycee73
      My Computer


  10. Posts : 2
    Windows 10 Pro 64bit
       #20

    This script works for exact and only one window title. I have three variations of titles, which contain words X, Y and Z. Can I use this and how:
    Local $var = WinList("[REGEXPTITLE?i)(.*X.*|.*Y.*|.*Z.*)]")
      My Computer


 
Page 2 of 3 FirstFirst 123 LastLast

  Related Discussions
Our Sites
Site Links
About 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 22:18.
Find Us