First post here.
I need help to close a software automatically after it's not used for some time. Not Windows idle, but that particular software. Let's say I open a software and work it with. Then I switch to other software without closing it. After 10 mins not using it, I want it to automatically close.
The reason I want to do this is because the license is concurrent, and I want it to close so other users can use it.
My initial idea is to use task scheduler. But it seems that the only option is to trigger the task when the computer is idle. Not that specific software.
I think that the Windows native PowerShell could handle a task like this. If I were going to do this, I would use AutoIt - but that is only because I know AutoIt better than PowerShell.
Let's see if some forum member wants to write a PowerShell script for edwinprakoso.
After the installation completes:
Right click on the desktop and select New > AutoIt v3 Script from the context menu.
(You might need to select the desktop and press F5 to get that context menu.)
Then right click on the newly created file and select Edit Script from the context menu.
Copy/paste the info below into the newly created file:
(You can paste over the info that is already in the new file.)
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
Save that file.
Move it to the Startup folder.
Right click on the file and select Run Script from the context menu.
You can end the script by clicking on the AutoIt icon in the notification area down by the clock.
If you would rather not have an AutoIt icon in the notification area, add this line...
#NoTrayIcon
...as the first line of the script.
If you do that, then you can kill the script (if need be) via Task Manager.
Kill the process named AutoIt3.exe.
The script in the code box above is going to close the app after 6 seconds.
Change
If $count > 6 Then WinClose($title)
to
If $count > 600 Then WinClose($title)
to make it wait 10 minutes (600 seconds).
Change the text in this line to match the app that you want to close:
$title = "Untitled - Notepad"
The title needs to be an exact match.
If the title of the app to be closed changes a bit (like notepad's title changes once you save a file), then we can change the code to handle matching a part of the title.
If the app to be closed can get into a state where it will not close (like notepad with unsaved changes), then we can add code to deal with that. If need be, we can kill the app and lose the unsaved changes.
Hide the ToolTip that I used for debugging by adding a semicolon before that line as shown in the video
This part of the script just sets a name for the invisible window that the script will create:
Code:
$S_running = "[COLOR=Red]check-4-app[/COLOR]" ;name the script
If WinExists($S_running) Then Exit
AutoItWinSetTitle($S_running)
You can change "check-4-app" to anything that you want... maybe "~~check-4-app-2-close~~". Just pick a name that won't exist in any other app window title that you run. If you happen to start a second instance of the script, that second copy should just exit because it sees a window with its name already running.
That is awesome UsernameIssues!
Now I will have to confirm this with our IT department head if he allows us to use AutoIt.
But if someone post a PowerShell script, I think I will go that way. But at least now I have an alternative that actually works!
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.
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 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.
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:
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.
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
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.
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.*)]")