How do I set the notification tone on error in VSCode terminals?

Asked 2 years ago, Updated 2 years ago, 68 views

Is it possible to set it to "sound a notification tone when an error occurs while running a program in the VSCode terminal"?

(Example: Sound notification when running Selenium in Python and getting selenium.common.exception.WebDriverException error)

I am using Selenium to automatically work on it, but I am asking this question in order to solve this problem because sometimes I am late to notice it when an error occurs.Please put the terminal where you can see it and don't notice it.

Environment: Windows 10

vscode

2022-09-29 22:25

1 Answers

In this case, isn't "VSCode's Terminal" just launching the "running Selenium" Python program specifically linked?

In other words, instead of "VSCode's Terminal" monitoring Python program behavior and taking action, or "Python Program" notifying VSCode's Terminal to take action, all you need to do is "Python Program running Selenium makes an error sound alone when an exception occurs."

In that case, wouldn't this article be applicable?
How to notify errors when calling python from the Windows GUI, or winsound.Beep

I tried to make the sound only when the exception is raised in Python in Windows.

import winsound

def hoge():
    # make some exceptions

if__name__=='__main__':
    try:
        hoge()
    except:
        winsound.Beep (400,200)

Background
In Windows, I sometimes launch a .bat python script file from a GUI such as Explorer.At that time, a shell window is opened only at the time of execution, and a standard output is displayed, and when the processing is finished, it is closed.
As a problem, even if the script terminates abnormally, the shell window is closed and the standard error output is not visible.(Error is displayed but shell window closes immediately)
Then, it is not known whether it is normal or abnormal.
I encountered this problem because I used to call python scripts directly from Tablacus Explorer as follows:

What do you want to do?

If you hear a sound like this, you'll find out if there's a problem, so you can open IDE and debug it as you like.

addition:

The comment didn't ring winsound.Beep(), but based on the article below, it seems that it was done using winsound.PlaySound(), such as preparing a wav file and making any sound.
Active engineer explains how to play music on Python's winsound [For beginners]

Assume you have an audio file sample.wav in your current directory.

import winsound

with open('sample.wav', 'rb') as f:
    data=f.read()

winsound.PlaySound(data, winsound.SND_MEMORY)

If you can do the above, you may be able to use the wav file provided by your system.MessageBeep().
winsound.MessageBeep (type=MB_OK)
Find the original API and parameters here MessageBeep function

There seems to be another alarm sound from the escape sequence \a.
However, it doesn't seem to work in IDE.
Play simple beep with python without external library

So sys.stdout.write("\a")might be better.

How to make a sound in OSX using python3

import sys
sys.stdout.write('\a')
sys.stdout.flush()

Actually, sys.stdout.write('\a')works for me, but not in IDE, try to run this code in Terminal.You will hear the system sound.
It actually works with sys.stdout.write('\a'), but it doesn't work with IDE.Try executing this code in terminal.System sound is heard.

If it's not Windows, you can replace it with the contents of the article around here.
Sound the Python Beep Code (Windows/Mac Ready)

Beep codes are implemented by environment in platform.system() because Windows and Mac have different implementations.

def beep(freq, dur=100):
    """
        make a beep
        @param freq frequency
        @param dur Duration (ms)
    """
    if platform.system()=="Windows":
        # For Windows, use the Python standard library called Winsound.
        import winsound
        winsound.Beep (freq, dur)
    else:
        # For Macs, use the play command installed on the Mac as a standard.
        importos
        os.system('play-nsynth%ssin%s'%(dur/1000, freq))

// ring 500 ms/sec at 2000 Hz
beep (2000,500)

Actually, play seems to be in the MacOS SoX package.
The following is a comment in the Linux article:

On MacOS you can install sox via ports (sudo port install sox).Sox contain the play command.
On MacOS, you can install sox through the port ( sudo port install sox), which contains the play command.

It may also be used on Linux.
play(1)-Linux man page

Python: what are the nearest Linux and OSX equivalents of winsound.Beep?

However, you can try the os.system command to do the same with the system command beep.Here is a snippet, which defines the function play ground ind indication> However, you can try the os.system command and do the same with the system command beep.This is a snippet that defines the function playsound in a platform-independent manner

try:
    import winsound
except ImportError:
    importos
    def playsound (frequency, duration):
        # apt-get install beep
        os.system('beep-f%s-l%s'%(frequency, duration))
else:
    def playsound (frequency, duration):
        winsound.Beep (frequency, duration)

This article is related to Linux.You may need to install beep as well as MacOS.

You will need to install the beep package on linux to run the beep command. You can install by giving the command
To run the beep command, you must install the beep package on Linux.You can install it by giving the command

 sudo apt-get install beep


2022-09-29 22:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.