Timer code doesn't work if midnight falls in between! Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
playerinfinity707
Posts: 23
Joined: 28 Dec 2021, 01:37

Timer code doesn't work if midnight falls in between!

Post by playerinfinity707 » 16 Jan 2022, 11:28

So I have this code that runs AIMP if the time is between 1700 to 0400 for some reason:

Code: Select all

SetTitleMatchMode 2

SetTimer tCheck,3000

GroupAdd Volume,NCS ahk_exe Firefox.exe
GroupAdd Volume,Pixabay ahk_exe Firefox.exe
GroupAdd RunAIMP,Anilist ahk_exe Firefox.exe
GroupAdd RunAIMP,Adobe Premiere Pro.exe


tCheck:
  If WinExist("ahk_group Volume") && !fVol{
    SoundGet vVol
    SoundSet 14
    fVol:=1
  }Else If !WinExist("ahk_group Volume") && fVol{
    SoundSet % vVol
    fVol:=0
  }
  DetectHiddenWindows 1                       ;Turn this on for this only
  If (A_Hour>=17 && A_Hour<4) && WinExist("ahk_group RunAIMP") && !WinExist("ahk_exe AIMP.exe")
  Run % "C:\Program Files (x86)\AIMP\AIMP.exe"
  DetectHiddenWindows 0                       ;Turn this off again
Return

The code works however if I set the time to something like 1700 to 2300. Any reason for this disparity?

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Timer code doesn't work if midnight falls in between!

Post by boiler » 16 Jan 2022, 11:35

The hour can't be both greater than or equal to 17 and less than 4. You need to check to see if it's greater than or equal to 17 or less than 4:

Code: Select all

  If (A_Hour>=17 || A_Hour<4) && WinExist("ahk_group RunAIMP") && !WinExist("ahk_exe AIMP.exe")

playerinfinity707
Posts: 23
Joined: 28 Dec 2021, 01:37

Re: Timer code doesn't work if midnight falls in between!

Post by playerinfinity707 » 16 Jan 2022, 11:41

@boiler Yup you were right. Replacing the Logical AND with OR worked! Thank u very much!

Code: Select all

If (A_Hour>=17 && A_Hour<4) || WinExist("ahk_group RunAIMP") && !WinExist("ahk_exe AIMP.exe")

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Timer code doesn't work if midnight falls in between!  Topic is solved

Post by boiler » 16 Jan 2022, 11:44

playerinfinity707 wrote: @boiler Yup you were right. Replacing the Logical AND with OR worked! Thank u very much!

Code: Select all

If (A_Hour>=17 && A_Hour<4) || WinExist("ahk_group RunAIMP") && !WinExist("ahk_exe AIMP.exe")
You have put the OR in the wrong place. See where I put it. It would "work" the way you changed it only because the RunAIMP window exists because you made that an OR condition, so it doesn't matter what the hour is. The If statement will now be true at any time of day as long as that window exists.

The condition (A_Hour>=17 && A_Hour<4) will never be true at any time of day. It can't be true logically. A number can't be simultaneously greater than or equal to 17 and less that 4. It needs to be (A_Hour>=17 || A_Hour<4). That's how it will exclude hours 4 through 16.

playerinfinity707
Posts: 23
Joined: 28 Dec 2021, 01:37

Re: Timer code doesn't work if midnight falls in between!

Post by playerinfinity707 » 30 Jan 2022, 14:26

@boiler you are right. actually, I had made the right code but commented something else.

anyways, thanks a lot!

Post Reply

Return to “Ask for Help (v1)”