Loop not working properly; AHK or Windows-related?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
skribb
Posts: 28
Joined: 22 Jun 2016, 20:52

Loop not working properly; AHK or Windows-related?

07 Jul 2017, 14:38

This loop is supposed to run in the background and set the volume of eventghost according to the current time (if my headphones are disabled; the flag of which is governed by eventghost). If I run the script it works the first time , because I notice that the volume level does indeed change for eventghost. However, after subsequent loops the volume doesn't change, even tho when opening the script window I can see that the Run commands are actually executed in the script.

I have no clue why it isn't working. Is something wrong with the logic in my if statements or is there something finicky about the way Windows' app volumes are handled? Assistance needed. :)

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
;#NoTrayIcon


Loopstart:

FileRead, headphones_flag, M:\Portfolio\Smart Home Projects\headphones_flag.txt

IfInString, headphones_flag, 0
{
	;;;;;;;;;;;;; MONDAY - THURSDAY 

	if (A_WDay == 2 or A_WDay == 3 or A_WDay == 4 or A_WDay == 5) 
	{
		if (A_Hour <= 8 or A_Hour >= 21)
		Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe 30
		if (A_Hour > 8 and A_Hour < 21)
		Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe 50
	}	                    
	
		;;;;;;;;;;;;;;;;;;;FRIDAY
	if (A_WDay == 6) 
	{
		if (A_Hour <= 8 or A_Hour >= 21)
		Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe 30
		if (A_Hour > 8 and A_Hour < 21)
		Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe 50
	}
            
	;;;;;;;;;;;;;;;;;;;7 is SATUUUR
	if (A_WDay == 7) 
	{
		if (A_Hour <= 8 or A_Hour >= 21)
		Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe 30
		if (A_Hour > 8 and A_Hour < 21)
		Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe 50
	}
            
	;;;;;;;;;;;;;;;;;;;1 is SUNDAY
	if (A_WDay == 1) 
	{
		if (A_Hour <= 8 or A_Hour >= 21)
		Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe 30
		if (A_Hour > 8 and A_Hour < 21)
		Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe 50
	}
}

Sleep, 1800000 ; 30 min
Goto, Loopstart
User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: Loop not working properly; AHK or Windows-related?

07 Jul 2017, 18:31

I suppose you're saying that you've changed the volume manually within the half hour since the script changed it, correct? Otherwise, why would you expect it to change the next time through the loop unless you've crossed one of the hour boundaries?
skribb
Posts: 28
Joined: 22 Jun 2016, 20:52

Re: Loop not working properly; AHK or Windows-related?

07 Jul 2017, 20:11

boiler wrote:I suppose you're saying that you've changed the volume manually within the half hour since the script changed it, correct? Otherwise, why would you expect it to change the next time through the loop unless you've crossed one of the hour boundaries?
For test purposes I reset the volume manually yes
User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: Loop not working properly; AHK or Windows-related?

07 Jul 2017, 21:31

Well, then it seems like it should work, but I can't speak to your Run commands. The logic seems right. You should use Loop instead of Goto, but it should still work.
User avatar
YoucefHam
Posts: 372
Joined: 24 Aug 2015, 12:56
Location: Algeria
Contact:

Re: Loop not working properly; AHK or Windows-related?

07 Jul 2017, 21:31

Try this

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
;#NoTrayIcon

Loopstart:
FileRead, headphones_flag, M:\Portfolio\Smart Home Projects\headphones_flag.txt

IfInString, headphones_flag, 0
{
	
	if A_WDay in 2,3,4,5 ;;;;;;;;;;;;; MONDAY - THURSDAY 
	{
		if (A_Hour <= 8) or (A_Hour >= 21)
			SetVolumeFunc(30)
		else
			SetVolumeFunc(50)
	}
	else if (A_WDay == 6) ;;;;;;;;;;;;;;;;;;; FRIDAY
	{
		if (A_Hour <= 8) or (A_Hour >= 21)
			SetVolumeFunc(30)
		else
			SetVolumeFunc(50)
	}
	else if (A_WDay == 7) ;;;;;;;;;;;;;;;;;;; 7 is SATUUUR
	{
		if (A_Hour <= 8) or (A_Hour >= 21)
			SetVolumeFunc(30)
		else
			SetVolumeFunc(50)
	}
    else if (A_WDay == 1) ;;;;;;;;;;;;;;;;;;;1 is SUNDAY
	{
		if (A_Hour <= 8) or (A_Hour >= 21)
			SetVolumeFunc(30)
		else
			SetVolumeFunc(50)
	}
}
Sleep, 1800000 ; 30 min
Goto, Loopstart


SetVolumeFunc(Volume)
{
	global
	;~ SoundSet, %Volume%   ;for the test
	Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe %Volume%
}
:wave: There is always more than one way to solve a problem. ;)
skribb
Posts: 28
Joined: 22 Jun 2016, 20:52

Re: Loop not working properly; AHK or Windows-related?

08 Jul 2017, 08:25

boiler wrote:stuff
YoucefHam wrote:stuff
Seems to be working, I only did minor testing tho. Will report back if something happens :dance:

Youcef what is your reason for making the script modular other than it being much prettier to read? Do you think it could have any bearing on the script's success rate?

Code: Select all

Loop
{
FileRead, headphones_flag, M:\Portfolio\Smart Home Projects\headphones_flag.txt

IfInString, headphones_flag, 0
{
	
	if A_WDay in 2,3,4,5 ;;;;;;;;;;;;; MONDAY - THURSDAY 
	{
		if (A_Hour <= 8) or (A_Hour >= 21)
			SetVolumeFunc(30)
		else
			SetVolumeFunc(50)
	}
	else if (A_WDay == 6) ;;;;;;;;;;;;;;;;;;; FRIDAY
	{
		if (A_Hour <= 8) or (A_Hour >= 21)
			SetVolumeFunc(30)
		else
			SetVolumeFunc(50)
	}
	else if (A_WDay == 7) ;;;;;;;;;;;;;;;;;;; 7 is SATUUUR
	{
		if (A_Hour <= 8) or (A_Hour >= 21)
			SetVolumeFunc(30)
		else
			SetVolumeFunc(50)
	}
    else if (A_WDay == 1) ;;;;;;;;;;;;;;;;;;;1 is SUNDAY
	{
		if (A_Hour <= 8) or (A_Hour >= 21)
			SetVolumeFunc(30)
		else
			SetVolumeFunc(50)
	}
}
Sleep, 1800000 ; 30 min
}


SetVolumeFunc(Volume)
{
	global
	;~ SoundSet, %Volume%   ;for the test
	Run, X:\Apps\Audio\SoundVolumeView\SoundVolumeView.exe /SetVolume eventghost.exe %Volume%
}

EDIT: there was a period of a few hours recently where the script wouldn't affect the volume level, and then suddenly it started working again. There must be something weird with the way that Windows interprets/receives the volume commands
EDIT2: it appears that eventghost's volume can only be changed while it is playing audio. The reason is that when EG isn't playing audio, the application isn't a visible app in the volume mixer .

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 361 guests