Could someone write me a very simple Windows 7 script?

Zoandar

Junior Member
May 7, 2013
18
0
61
I would like to have a script I can run in Windows 7 that will play a sound when a specific program window closes. It is a window like those you would open as a command line, and is running a compiler named "resample.exe" (part of custom scenery creation for MS Flight Simulator X) that would be displayed in the window header. Once started, the compiler runs for several minutes, and then the window closes automatically. I would like to have the PC play a sound when the window closes.

I can set Windows 7 to sound when "any" window closes, and this works, but it keeps sounding for all the other things I am opening and closing while I wait for the compiler. So I would like to have only the compiler window make a sound when it closes. Would this be easy for one of you to do? I don't know anything about writing scripts.
 

Tweak155

Lifer
Sep 23, 2003
11,448
262
126
Interesting request. You could probably do it in VBScript which is native to Windows as well.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
You will probably have better luck if you dig into coding and then post some code you are having trouble with. I will give you a hint: you are going to want to use WMI to enumerate all processes and keep looping every few seconds to see if that process is still in the list of active processes. When it is no longer there, it can exit the loop and then play the sound. Good luck.

http://vbscriptblog.com/vbscript/wm...ipt-to-enumerate-all-processes-on-a-computer/
 

Zoandar

Junior Member
May 7, 2013
18
0
61
This works, such as it is. I am not seeking anything elegant. It does throw PowerScript errors during each of the time intervals between the bell, and I have to start it manually after the compiler starts. it would be nice to have it start automatically. However. my overall view of this was that I did not want to have to pursue an IT degree just to be able to "turn on the lights", so to speak. I don't have much other interest in scripting, so I was hoping someone who liked writing scripts would offer something simple to get the job done.

I am using this in a .ps1 file:

Code:
wait-process resample
`a
start-sleep -s 1
`a
start-sleep -s 1
`a
start-sleep -s 1
`a
start-sleep -s 1
`a

I gathered enough information to throw this together between reading a little about Powershell and making use of its handy get-help x -example command. :)

The problem with all the tutorials I found was that they all assume one wants to learn a great deal about Windows Scripting. I've got several other projects on my plate, and was just looking for some way to get the resample compiler to tell me when it was finished, so I could move on to the next step in the scenery editing process. I am typically occupied elsewhere while it is running. The compiler is started by dragging an information file and dropping it onto the compiler's exe file, so it doesn't actually have a way to tell it to start that might be added to the script. The information file's name is different for each run.

A sophisticated script maybe could run all the time and watch for the compiler to start, then sound the bell when it stops. But that is very far beyond what I could do without a lot of studying first.
 

takeru

Golden Member
Jan 1, 2002
1,206
8
81
You could always just script it in autoit. This script will stay active until you exit it from the notification tray. It will scan for windows with "resample.exe" as part of the window title, and play the sound file indicated when none no longer exist.

http://www.autoitscript.com/site/autoit/

Complete code, just change sound file and window title if need be and compile it:

Code:
Global $soundFile, $windowTitle
$soundFile = "R:\sound.wav"
$windowTitle = "resample.exe"

While 1
sleep(100)
If WinExists($windowTitle) Then
	WinWaitClose($windowTitle)
	SoundPlay($soundFile)
EndIf
WEnd
 
Last edited:

Zoandar

Junior Member
May 7, 2013
18
0
61
Thank you very much! :)

I had never run across AutoIT before. I do have a little experience with AutoHotkey several years ago, but the experience had a lot of negative aspects so I got away from it. I am also at the very beginning of learning to work with Python on the Raspberry Pi, and wayyyyyy back when I wrote in Basic and assembler for my trusty TRS-80. :) But that feels like a lifetime ago.

So although I only know enough to be dangerous, it looks to me like this script would run indefinitely once started and do exactly what I wanted. :)

I will download AutoIT and see what I can accomplish with it.

Thanks!
 
Last edited:

Zoandar

Junior Member
May 7, 2013
18
0
61
If it turns out to be fun and easy to do, I may dive into it somewhat.

I had some serious disliking for the help file when I was trying to learn AutoHotkey, because it never fully answered any of my questions. And their support forum wasn't very helpful either. IMHO, user guides and help files need to be written by someone while learning the subject, overseen by experienced users. When the experienced users write the manuals, they assume far too much and leave out essential steps in learning. Someone who understands this can write a very nice tutorial. But many do not understand it.

Although I was able to accomplish most of what I wanted with AutoHotkey, it was very frustrating along the way and left a bad taste, and desire not to return to it. I hope AutoIT is nothing like that. ;)

Thanks for the help! I'll let you know how things work out when I try it. :)
 

Zoandar

Junior Member
May 7, 2013
18
0
61
Question - I am installing AutoIT ver. 3.3.8.1 on my Windows 7 x 64 Home Premium PC, where most of any scripts I will create will likely run. Would you suggest I set the x86 tools by default, or use the native x64 tools by default? It seems to be suggesting I use the x86 tools by default for better compatibility.
 

Zoandar

Junior Member
May 7, 2013
18
0
61
Well, I installed AutoIT3, copied your script into it, and made the following changes for testing purposes (there is no way to shut down resample.exe that I know of, and it usually takes several minutes to create a BGL file).

Code:
Global $soundFile, $windowTitle
$soundFile = "D:\AUDIO\ding.wav"
$windowTitle = "cmd.exe"

While 1
sleep(100)
If WinExists($windowTitle) Then
    WinWaitClose($windowTitle)
    SoundPlay($soundFile)
EndIf
WEnd
I then compiled it. I have tried running it from the exe and also tried running it as a script. It is not working. What do I do now?

One possible issue - the title of the CMD.exe window I see is more than just "cmd,exe", it acutally says

"Administrator:cmd.exe - shortcut".

Should be be checking to see if "cmd.exe" is CONTAINED WITHIN the window title, instead of if it IS the window title? I know the simple script I had working is doing it that way. I tried changing the code to look for exactly that window title, but still no joy. However, I just noticed that the script icon showing on the task tray, when I right click it, shows " script paused". If I click it to un-pause the script, it just re-does the pause again. So maybe the loop isn't looping? How can I fix that? Will I encounter "permissions" BS making these scripts?
 
Last edited:

Zoandar

Junior Member
May 7, 2013
18
0
61
OK, by commenting out the meat of the If loop, and just letting it play the sound file (adding in a 2 second sleep delay) it works.

And, it seems the act of right-clicking the script's taskbar icon is what 'pauses' it.

So whatever wasn't working has to do with detecting the existence of the window title and seeing when it closes. Still digging....... :)
 
Jan 31, 2013
108
0
0
OK, by commenting out the meat of the If loop, and just letting it play the sound file (adding in a 2 second sleep delay) it works.

And, it seems the act of right-clicking the script's taskbar icon is what 'pauses' it.

So whatever wasn't working has to do with detecting the existence of the window title and seeing when it closes. Still digging....... :)
I use to write tons of stuff in AU3, this code should work just fine.
Code:
Global $SoundFile, $WindowTitle

$SoundFile = "C:\Windows\Media\ding.wav"
$WindowTitle = "Command Prompt"

While 1
   If WinExists($WindowTitle) Then
	  If WinWaitClose($WindowTitle) Then
		 SoundPlay($SoundFile)
	  EndIf
   Else
	  Sleep(1)
   EndIf
WEnd
 

Zoandar

Junior Member
May 7, 2013
18
0
61
I got it to work with Notepad.exe, by adding quotes here:

Code:
If WinExists("$windowTitle") Then
but not with the CMD.exe window, which is similar to the resample.exe window. Must have something to do with the actual window name maybe not being "cmd"? I'll have more time later to work on it, and I'll try it with the resample.exe window itself. If it works with that for now, I will be happy.

@Warmonger - I had not seen your post yet. I'll give that a try. Thanks!
 
Last edited:
Jan 31, 2013
108
0
0
I got it to work with Notepad.exe, by adding quotes here:

Code:
If WinExists("$windowTitle") Then
but not with the CMD.exe window, which is similar to the resample.exe window. Must have something to do with the actual window name maybe not being "cmd"? I'll have more time later to work on it, and I'll try it with the resample.exe window itself. If it works with that for now, I will be happy.

@Warmonger - I had not seen your post yet. I'll give that a try. Thanks!
Catching a command prompt can be tricky, simply because the title can change depending on the directory its executed from. You can use its classname in effect for just detecting just console windows, along with regexptitle for detecting a string within the title. For example the follow code will detect any console windows that have "Command Prompt" in the title bar. Regardless if the title is "Hello this is a Command Prompt title", or "This is a Command Prompt". It will detect any console windows that have that string in the title.

Code:
Global $SoundFile, $WindowTitle

$SoundFile = "C:\Windows\Media\ding.wav"
$WindowTitle = "Command Prompt"

While 1
   If WinExists("[REGEXPTITLE:" & $WindowTitle & ";CLASS:ConsoleWindowClass]") Then
	  If WinWaitClose($WindowTitle) Then
		 SoundPlay($SoundFile)
	  EndIf
   Else
	  Sleep(1)
   EndIf
WEnd

Another example, "resample.exe".

Code:
Global $SoundFile, $WindowTitle

$SoundFile = "C:\Windows\Media\ding.wav"
$WindowTitle = "resample.exe"

While 1
   If WinExists("[REGEXPTITLE:" & $WindowTitle & ";CLASS:ConsoleWindowClass]") Then
	  If WinWaitClose($WindowTitle) Then
		 SoundPlay($SoundFile)
	  EndIf
   Else
	  Sleep(1)
   EndIf
WEnd

This will detect any window that is a console window, and has "resample.exe" in its title.
 

Zoandar

Junior Member
May 7, 2013
18
0
61
Thank you both for the options. :) I'll be back at the PC in a while to try them out. If I want to go with the PID, what's the best way to harvest that for a particular window?
 

Zoandar

Junior Member
May 7, 2013
18
0
61
This sort of works, but it plays the sound repeatedly as long as the CMD window is open. It does not wait for it to close. However, it does also play the sound once after I close the CMD window:

HTML:
Global $SoundFile, $WindowTitle

$SoundFile = "C:\Windows\Media\ding.wav"
$WindowTitle = "cmd"

While 1
   If WinExists("[REGEXPTITLE:" & $WindowTitle & ";CLASS:ConsoleWindowClass]") Then
      If WinWaitClose($WindowTitle) Then
         SoundPlay($SoundFile)
      EndIf
   Else
      Sleep(1)
   EndIf
WEnd

Note that I had to change "command prompt" to "cmd" to get it to detect the cmd.exe window.
 

Zoandar

Junior Member
May 7, 2013
18
0
61
Bear in mind I have little to no idea what I am doing here :) so I am experimenting based on whatever information I find. I tried this, based on an example in the AutoIT Help listing for WinWaitClose, and it works like it is supposed to, sounding only when the opened CMD window is closed.

Code:
Global $SoundFile, $WindowTitle

$SoundFile = "C:\Windows\Media\ding.wav"
$WindowTitle = "cmd"

While 1
   If WinExists("[REGEXPTITLE:" & $WindowTitle & ";CLASS:ConsoleWindowClass]") Then
      If WinWaitClose("[CLASS:ConsoleWindowClass]") Then
         SoundPlay($SoundFile)
      EndIf
   Else
      Sleep(1)
   EndIf
WEnd
And THIS works for the actual "resample.exe" compiler window, BUT....Now I have another problem. The script apparently is putting a serious hit on the performance of the compiler. (The files being compiled, and the compiler, are running on a SSD). It appears to be causing the compiler to pause about every 3 seconds for a second. Is there anything I could do to help that? I don't need to know the very instant the compiler window closes. I just want to know 'eventually' that it closed. So maybe make the script only check every so many seconds somehow?

Hmm. This is weird. Toward the end, the pausing completely disappeared, and the compiler was running at its normal speed. Maybe it was not the script causing the pausing? I'll have to run it several times to see.

Code:
Global $SoundFile, $WindowTitle

$SoundFile = "C:\Windows\Media\ding.wav"
$WindowTitle = "resample"

While 1
   If WinExists("[REGEXPTITLE:" & $WindowTitle & ";CLASS:ConsoleWindowClass]") Then
      If WinWaitClose("[CLASS:ConsoleWindowClass]") Then
         SoundPlay($SoundFile)
      EndIf
   Else
      Sleep(1)
   EndIf
WEnd
 
Last edited:

takeru

Golden Member
Jan 1, 2002
1,206
8
81
This sort of works, but it plays the sound repeatedly as long as the CMD window is open. It does not wait for it to close. However, it does also play the sound once after I close the CMD window:

Code:
Global $SoundFile, $WindowTitle

$SoundFile = "C:\Windows\Media\ding.wav"
$WindowTitle = "cmd"

While 1
   If WinExists("[REGEXPTITLE:" & $WindowTitle & ";CLASS:ConsoleWindowClass]") Then
      If WinWaitClose($WindowTitle) Then
         SoundPlay($SoundFile)
EndIf
   Else
      Sleep(1)
   EndIf
WEnd

Note that I had to change "command prompt" to "cmd" to get it to detect the cmd.exe window.

remove the highlighted code and try again
 

Zoandar

Junior Member
May 7, 2013
18
0
61
Well, now the pausing seems to have gone away, and even with the script running the compiler runs quickly and smoothly.

One of the joys of Windows. I never know what it is 'really' doing in the background. :biggrin:

Thanks for all the help guys!! :)
 

takeru

Golden Member
Jan 1, 2002
1,206
8
81
That still sounds the tone continuously as long as the CMD window is open.

not sure how yours is repeating the sound? just tried warmongers code and it works fine too. maybe your windowtitle needs fine tuning? use the autoit window info tool on your resample window, and paste the summary tab info. that should help.
 

Zoandar

Junior Member
May 7, 2013
18
0
61
I would be happy to try, but when I looked in the AutoIT Editor Help file for

window info tool

I did not find anything useful.

Ah! When in doubt, Google! "AutoIt v3 comes with a standalone tool called the AutoIt Window Info Tool............"

I found it in the list of associated programs. Seems to work much like that used in some other macro apps I have seen in the past. Here is the Summary when I dragged it onto a running instance of the compiler:

>>>> Window <<<<
Title: H\Lake Erie Shore\Westlake S3\resample.exe
Class: ConsoleWindowClass
Position: 0, 0
Size: 843, 549
Style: 0x14EF0000
ExStyle: 0x00040310
Handle: 0x013C074E

>>>> Control <<<<
Class:
Instance:
ClassnameNN:
Name:
Advanced (Class):
ID:
Text:
Position:
Size:
ControlClick Coords:
Style:
ExStyle:
Handle:

>>>> Mouse <<<<
Position: 538, 17
Cursor ID: 0
Color: 0xFCFFFF

>>>> StatusBar <<<<

>>>> ToolsBar <<<<

>>>> Visible Text <<<<


>>>> Hidden Text <<<<
 
Last edited:

Zoandar

Junior Member
May 7, 2013
18
0
61
I found the problem :)

Your code has

Code:
WinWaitClose($WindowTitle)

but the example in the Help file for WinWaitClose shows:

Code:
 WinWaitClose("[Class:ConsoleWindowClass]")

If I change that line to the latter of those two, it works correctly, for both cmd and for resample.
 
Jan 31, 2013
108
0
0
remove the highlighted code and try again
I put the Sleep(1) to deliberately prevent the compiler from consuming an entire core worth of resources (tight loops will eat 100% CPU). The way it's written, is if no window exists it will just sleep for 16ms.

@OP You can try.

Code:
Global $SoundFile, $WindowTitle

$SoundFile = "C:\Windows\Media\ding.wav"
$WindowTitle = "Command Prompt"

While 1
   If WinExists("[REGEXPTITLE:" & $WindowTitle & ";CLASS:ConsoleWindowClass]") Then
	  If WinWaitClose("[REGEXPTITLE:" & $WindowTitle & ";CLASS:ConsoleWindowClass]") Then
		 SoundPlay($SoundFile)
	  EndIf
   Else
	  Sleep(1)
   EndIf
WEnd