Page 1 of 1

Complaining on "try" statement

Posted: 07 Apr 2024, 21:21
by slishnevsky
Hi.

Why is AHK complaining on try statement?

Code: Select all

CreateTimer() {
  timeout := InputBox('Set timer interval (in minutes)', 'Timer').Value
  if (!IsInteger(timeout))
    Exit
  SetTimer(() => (
    SoundPlay('WakeUp.mp3')
    MsgBox('Сработал таймер!', 'Таймер', 'Icon! OK')
    try SoundPlay('Nonexistent.mp3') ; Complains on the usage of try statement here
  ), timeout * 60000)
}

Re: Complaining on Try statement

Posted: 07 Apr 2024, 21:38
by mikeyww
  1. What does that mean?
  2. Post the script instead of an image of it.
  3. Post a screenshot of the error message.

Re: Complaining on Try statement

Posted: 07 Apr 2024, 21:54
by boiler
Try is not a valid part of an expression which you are trying to define as part of a fat arrow function. The syntax for that is () => expr, where expr is an expression.

Re: Complaining on Try statement

Posted: 07 Apr 2024, 22:48
by slishnevsky
boiler wrote:
07 Apr 2024, 21:54
Try is not a valid part of an expression which you are trying to define as part of a fat arrow function. The syntax for that is () => expr, where expr is an expression.
Makes sense. Then, how should I stop playing audio there?
If I move try SoundPlay('Nonexistent.mp3') outside the arrow function, the interpreter doesn't complain, but the audio keeps playing...
According to the documentation, To stop a file that is currently playing, use SoundPlay on a nonexistent filename as in this example: try SoundPlay "Nonexistent.avi".

Code: Select all

CreateTimer() {
  timeout := InputBox('Set timer interval (in minutes)', 'Timer').Value
  if (!IsInteger(timeout))
    Exit
  SetTimer(() => (
    SoundPlay('WakeUp.mp3')
    MsgBox('Сработал таймер!', 'Таймер', 'Icon! OK')
  ), timeout * 60000)
  try SoundPlay('Nonexistent.mp3') ; Doesn't complain, but keeps playing
}

Re: Complaining on "try" statement  Topic is solved

Posted: 07 Apr 2024, 23:46
by boiler
Just use a regular function instead of a fat arrow function.

Re: Complaining on "try" statement

Posted: 08 Apr 2024, 00:11
by slishnevsky
@boiler

Yes! Worked. Thank you.