Making sound play only when GUI checkbox is checked

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Making sound play only when GUI checkbox is checked

Post by SmithyZoomZoom » 08 Dec 2022, 20:19

I've been working on a script that displays a tooltip and plays a sound once every hour. (Except that when the program Streaming Audio Recorder is running, I don't want the sound to play. Also, when the GUI checkbox is unchecked, I don't want the sound to play.)

The good news is that the script works as intended when the Streaming Audio Recorder program is active, as the sound will not play; however, regardless of whether the checkbox is checked or not, the sound still plays. Does anyone have any advice for getting the sound not to play when the checkbox is unchecked? The condition of the checkbox seems to have no impact on the execution of the sound file. Any help is appreciated. Thank you.

Code: Select all

#Persistent

I_Icon = %A_MyDocuments%\Autohotkey Icons\exclamation_icon.ico
IfExist, %I_Icon%
Menu, Tray, Icon, %I_Icon%

SetTimer, ShowTooltip, 120000 ; I lowered the timer so that the sound plays every couple of minutes just for testing purposes.

Gui, Add, Button, xm ym+20, Play Sound
Gui, Add, Checkbox, vSoundEnabled xm ym+40, Play sound


; Set SoundEnabled to 1 to enable sound by default
SoundEnabled := 1

Gui, Show, w200 h70, Price Check Reminder

ButtonPlaySound:
    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        ; Play the wave file
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
        }
    }
    return

ShowTooltip:
    ToolTip, Remember to check chat!
    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
        }
    }
    Sleep, 5000
    ToolTip
    return

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Making sound play only when GUI checkbox is checked

Post by mikeyww » 08 Dec 2022, 20:43

Gui, Submit:
Saves the contents of each control to its associated variable (if any) and hides the window unless the NoHide option is present.

SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: Making sound play only when GUI checkbox is checked

Post by SmithyZoomZoom » 09 Dec 2022, 15:28

I added the GUI submit code. Now when I start up the AutoHotkey script, instead of seeing a checkbox that I can check or uncheck, it just disappears rather quickly.

Code: Select all

ButtonPlaySound:
    Gui, Submit
	Gui, Show, w200 h70, Price Check Reminder
    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        ; Play the wave file
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
			Gui, Show, w200 h70, Price Check Reminder

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Making sound play only when GUI checkbox is checked

Post by mikeyww » 09 Dec 2022, 15:31

You missed what I already posted:
hides the window unless the NoHide option is present.

SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: Making sound play only when GUI checkbox is checked

Post by SmithyZoomZoom » 09 Dec 2022, 17:52

I did, indeed. I added the No Hide option and now I can interact with the checkbox again. Sadly, when I interact with the checkbox, it still doesn't seem to affect whether the sound plays. To make a difference, I have to manually go in and change sound enabld from a zero to a one, or vice versa, by editing the script. Very curious indeed.

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Making sound play only when GUI checkbox is checked

Post by mikeyww » 09 Dec 2022, 17:53

Debugging is straightforward, because you can display the values of both SoundEnabled & ErrorLevel. You currently do not know what those values are.

Explained: Process, Exist:
Sets ErrorLevel to the Process ID (PID) if a matching process exists, or 0 otherwise.
ErrorLevel is zero if the process does not exist.

SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: Making sound play only when GUI checkbox is checked

Post by SmithyZoomZoom » 09 Dec 2022, 20:28

Thank you for your suggestion to debug. I may not have done it in quite the way you showed, but I was able to discover some things with this:


Code: Select all

#Persistent

I_Icon = %A_MyDocuments%\Autohotkey Icons\exclamation_icon.ico
IfExist, %I_Icon%
Menu, Tray, Icon, %I_Icon%

SetTimer, ShowTooltip, 120000 ; I lowered the timer so that the sound plays every couple of minutes just for testing purposes.

Gui, Add, Button, xm ym+20, Play Sound
Gui, Add, Checkbox, vSoundEnabled xm ym+40, Play sound


; Set SoundEnabled to 1 to enable sound by default
SoundEnabled := 1

Gui, Show, w200 h70, Price Check Reminder

ButtonPlaySound:
Gui, Submit , NoHide
MsgBox, The value of SoundEnabled is: %SoundEnabled%
    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        ; Play the wave file
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
        }
    }
    return

ShowTooltip:
    ToolTip, Remember to check chat!
    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
        }
    }
    Sleep, 5000
    ToolTip
    return
I can get the message box I used for error-checking to switch between displaying zero and one for SoundEnabled. And I can get the sound file to play or not play according to my desires but strangely I have to finalize my choice by clicking the play sound button. Just clicking the checkbox by itself doesn't work. So I am not sure what to do next but thank you for providing any help.

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Making sound play only when GUI checkbox is checked

Post by mikeyww » 09 Dec 2022, 20:35

Use a g-label. https://www.autohotkey.com/docs/lib/GuiControls.htm#Checkbox
A g-label such as gMySubroutine may be listed in the control's options. This would cause the MySubroutine label to be launched automatically whenever the user clicks or changes the checkbox.

SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: Making sound play only when GUI checkbox is checked

Post by SmithyZoomZoom » 09 Dec 2022, 23:01

I made an attempt to use the glabel. I'm getting a parameter #1 error on line 20:

Code: Select all

#Persistent

I_Icon = %A_MyDocuments%\Autohotkey Icons\exclamation_icon.ico
IfExist, %I_Icon%
Menu, Tray, Icon, %I_Icon%

SetTimer, ShowTooltip, 120000 ; I lowered the timer so that the sound plays every couple of minutes just for testing purposes.

Gui, Add, Button, xm ym+20, Play Sound
Gui, Add, Checkbox, vSoundEnabled gMyCheckbox xm ym+40, Play sound


; Set SoundEnabled to 1 to enable sound by default
SoundEnabled := 1

Gui, Show, w200 h70, Price Check Reminder

ButtonPlaySound:
    ; Update the value of SoundEnabled based on the current state of the checkbox
    GuiControl, Get, SoundEnabled, MyCheckbox

    MsgBox, The value of SoundEnabled is: %SoundEnabled%

    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        ; Play the wave file
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
        }
    }
    return

ShowTooltip:
    ToolTip, Remember to check chat!

    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
        }
    }
    Sleep, 5000
    ToolTip
    return

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Making sound play only when GUI checkbox is checked

Post by mikeyww » 10 Dec 2022, 05:56

Code: Select all

soundFile = %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
; soundFile = %A_WinDir%\Media\Ring01.wav
If !FileExist(soundFile)
 MsgBox, 48, Error, File not found.`n`n%soundFile%
Gui, Font, s10
Gui, Add, Button  , w230                                , Play sound
Gui, Add, Checkbox, wp   Checked vsound gButtonPlaySound, Play sound
Gui, Show,, Sound
Return

ButtonPlaySound:
Gui, Submit, NoHide
If (sound || A_GuiControl = "Play sound") {
 Process, Exist, Streaming Audio Recorder.exe
 If !ErrorLevel { ; Process does not exist
  SoundPlay, %soundFile%
 }
} Else SoundPlay, ""
Return

SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: Making sound play only when GUI checkbox is checked

Post by SmithyZoomZoom » 10 Dec 2022, 17:45

Thank you so much, Mr. Mike! Strangely, the same issue is happening to me when I'm running your code, in that the sound file still plays whenever I click the play sound button, regardless of whether the checkbox is checked. Weird…

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Making sound play only when GUI checkbox is checked

Post by mikeyww » 10 Dec 2022, 20:03

It's not strange; that's how the script is written. The one below disables the button if the checkbox is not marked. To understand what the script does, follow it line by line.

Code: Select all

soundFile = %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
; soundFile = %A_WinDir%\Media\Ring01.wav
proc      = Streaming Audio Recorder.exe
; proc      = notepad.exe
If !FileExist(soundFile)
 MsgBox, 48, Error, File not found.`n`n%soundFile%
Gui, Font, s10
Gui, Add, Button  , w230 vplay                , Play sound
Gui, Add, Checkbox, wp   vsound Checked gCheck, Play sound
Gui, Show,, Sound
Loop {
 Process, Exist, %proc%
 If !ErrorLevel {
  SoundBeep, 1500
  Process, Wait, %proc%
 }
 SoundBeep, 1000
 Process, WaitClose, %proc%
}
Check:
Gui, Submit, NoHide
If !sound {
       SoundPlay, ""
       GuiControl, Disable, play
} Else GuiControl, Enable , play
Return

ButtonPlaySound:
Gui, Submit, NoHide
Process, Exist, %proc%
If sound && !ErrorLevel
 SoundPlay, %soundFile%
Return
Or:

Code: Select all

soundFile  = %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
; soundFile  = %A_WinDir%\Media\Ring01.wav
proc       = Streaming Audio Recorder.exe
; proc       = notepad.exe
last      := sound := True
If !FileExist(soundFile)
 MsgBox, 48, Error, File not found.`n`n%soundFile%
Gui, Font, s10
Gui, Add, Button  , w230 vplay                , Play sound
Gui, Add, Checkbox, wp   vsound Checked gCheck, Play sound
Gui, Show,, Sound
Loop {
 Process, Exist, %proc%
 If !ErrorLevel {
  SoundBeep, 1500
  GuiControl,, sound, %last%
  Gosub, Check
  GuiControl, Enable, sound
  Process, Wait, %proc%
 }
 Gui, Submit, NoHide
 last := sound
 GuiControl,, sound, 0
 Gosub, Check
 GuiControl, Disable, sound
 SoundBeep, 1000
 Process, WaitClose, %proc%
}
Check:
Gui, Submit, NoHide
If !sound {
       SoundPlay, ""
       GuiControl, Disable, play
} Else GuiControl, Enable , play
Return

ButtonPlaySound:
Gui, Submit, NoHide
Process, Exist, %proc%
If sound && !ErrorLevel
 SoundPlay, %soundFile%
Return

SmithyZoomZoom
Posts: 111
Joined: 23 Aug 2019, 23:32

Re: Making sound play only when GUI checkbox is checked

Post by SmithyZoomZoom » 13 Dec 2022, 21:35

Thank you for the code. The other day, I don't think I explained the weird thing very well because I was expecting the play sound button to still work even when the checkbox is not checked. Like you said, that's what it does.

What I was trying to say is that this code:

Code: Select all

#Persistent

I_Icon = %A_MyDocuments%\Autohotkey Icons\exclamation_icon.ico
IfExist, %I_Icon%
Menu, Tray, Icon, %I_Icon%

SetTimer, ShowTooltip, 120000 ; I lowered the timer so that the sound plays every couple of minutes just for testing purposes.

Gui, Add, Button, xm ym+20, Play Sound
Gui, Add, Checkbox, vSoundEnabled gMyCheckbox xm ym+40, Play sound


; Set SoundEnabled to 1 to enable sound by default
SoundEnabled := 1

Gui, Show, w200 h70, Price Check Reminder

ButtonPlaySound:
    ; Update the value of SoundEnabled based on the current state of the checkbox
    GuiControl, Get, SoundEnabled, MyCheckbox

    MsgBox, The value of SoundEnabled is: %SoundEnabled%

    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        ; Play the wave file
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
        }
    }
    return

ShowTooltip:
    ToolTip, Remember to check chat!

    ; Check if the SoundEnabled variable is equal to 1
    if (SoundEnabled = 1) {
        Process, Exist, Streaming Audio Recorder.exe
        if !ErrorLevel {
            SoundGet, currentVolume
            SoundSet, 50
            SoundPlay, %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav
            SoundSet, %currentVolume%
        }
    }
    Sleep, 5000
    ToolTip
    return
    
    
    
Is supposed to do as follows:

It displays a tooltip message that reminds the user to check chat every 2 minutes. If the checkbox in the GUI is checked, the script will also play a sound whenever the tooltip is displayed. The sound file is located at %A_MyDocuments%\Autohotkey Sounds\tooltip_message.wav. The script also checks if the specified process, "Streaming Audio Recorder.exe", is running. If it is, the script will play the sound. If the exclamation_icon.ico file exists, the script sets it as the tray icon for the script.

It works fine, except that when I checked or unchecked the checkbox, it's like the program doesn't register the change in checkbox status until after I hit the play sound button. That's the part I thought was strange. I would think that just checking or unchecking the box would be enough to impact whether the sound automatically plays every two minutes. Instead of me having to check/uncheck the box depending on my preference and then also having just hit the play sound button whenever I check or uncheck the box to save my choice within the program. Does that make sense or am I talking crazy talk? I could be crazy, you know. Definitely, there is a chance I don't know what I'm talking about.

Your most recent script seems to work. Your AutoHotkey script will make a sound when a certain program is open. You can pick the sound by changing the "soundFile" variable and the program you want to monitor by changing the "proc" variable. There's also a button to play the sound and a checkbox to turn it on or off. If the sound file isn't there, an error message will show up.

I think that's right?

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Making sound play only when GUI checkbox is checked

Post by mikeyww » 13 Dec 2022, 22:00

The answers are already provided in my previous posts in this thread.

1. g-label executes a routine whenever the box status changes. Without a g-label, nothing happens immediately.
2. Submitting the GUI enables you to retrieve the value or contents of the control. An alternative is GuiControlGet. Without one of these, no values are obtained.

Using these two things together is what enables you to determine whether to play the sound.

GuiControlGet is the name of one command. Check your syntax! Don't make up your own; use what AHK provides.

Post Reply

Return to “Ask for Help (v1)”