Applescript to mute Discord

applescript

Edit: My initial question does not appear to have a solution. Instead I used an alternate method. Scroll down to see the alternate method.

I'm looking for a solution to mute my mic in Discord that I can then add to my touchbar via bettertouchtool.

BTT cannot send key combos to Discord for whatever reason. As far as I can tell I have two options.

  1. Use applescript to activate the Discord window, send the key press, then hide the window. This works, but flashing the application up for a moment is annoying. Worse when it's on another desktop as it will flip desktops over and then back.

  2. Use applescript to click the top menu (this menu) to click the Discord menu, and work down through the UI elements to click the Mute button.

I'm not experienced enough with applescript to make the second option work, but I think it could work. Using UI browser I was able to get the path to the UI element:
enter image description here

How can I make this work? Any help is greatly appreciated. Thanks

Edit HR

ALTERNATE SOLUTION

What I am wanting to do here does not appear to be possible, so I'm just muting my mic at the OS level instead of Discord, and then adding a toggle button to my touchbar with BetterTouchTool. This has the added benefit of working system wide. Here's video of it in action:

GIF

Streamable

In case anyone else was wanting to do this these are the steps I took:

1. Create a new Applescript with the following script:

property storedInputLevel : missing value
if input volume of (get volume settings) is 0 then
    set volume input volume storedInputLevel
    return 1
else
    tell application "System Events"
        set storedInputLevel to input volume of (get volume settings)
    end tell
    set volume input volume 0
    return 2
end if

Save the script somewhere.

2. Open up Better Touch Tool and create a new Touch Bar widget and set the widget to Run Apple Script and Show Return Value
enter image description here

3. In the window that pops up after selecting Run Apple Script and Show Return Value you'll want to erase the test line in the box and give your widget a name:
enter image description here

3.a Click the appearance and settings button

enter image description here

4. In the Appearance and Settings window you'll want to configure the following settings:
enter image description here

You don't need to set an alternate background color, but it helps to see you're muted at a glance.

For the icons you can use these:

enter image description hereenter image description here

Make sure that you set the Alternate color and icon if result matches regex setting to 2.

Click Save.

5. On the new widget you created click on the drop down for Predefined action and choose Run Apple Script (async in background).

enter image description here

6. In the window that popped up you'll want to click on Select Apple Script File and then Choose Apple Script file from Disk and browse to the script file you created earlier. Click save and you're done.

image13

That's it. Your widget should toggle your mic on and off and the change should be reflected in the icon on your touch bar.

Best Answer

Firstly, well done for exploring and doing your own research with which you've come armed to ask how it can be applied to form a solution. It makes it so much easier to provide useful help.

With that in mind, the screenshot from UI Browser is immensely helpful, as it literally lays out the hierarchy of UI elements that we need to traverse in order to reach the target menu item and issue a click via AppleScript:

tell application "System Events"
    tell application process "Discord"
        tell menu bar 2
            tell menu bar item 1
                click

                tell (a reference to menu 1)
                    repeat until it exists
                        delay 0.2
                    end repeat
                    tell menu item "Mute"
                        click
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

However, there is a known and well-queried/documented/complained-about phenomenon of an irritating 5-second inexplicable delay between the first click that shows the menu, and the second click that selects the menu item. This isn't specific to Discord, but seems to apply to menu bar icon menus.

Various solutions have been put forward to workaround this, but I won't go into them in detail because none of them are a) reliable, b) without side-effects, or c) clean to implement. You'll undoubtedly do a quick search yourself, and come upon the most common solution, which is to kill the System Events process between the first two click events. But you'll find upon further reading that later on, users report that this method stops working; and in the interrim, causes System Events to throw other, unrelated errors where it didn't before.

Therefore, this solution for you is bittersweet, I'm afraid. I've shown you how to AppleScript your way to the menu item you want; but the end result may not be any more appealing than your first option.

EDIT: Possible Non-AppleScript Solution

Having downloaded a copy of Discord myself to see what's what, I first note that my version of the application has no such menu bar icon to speak of. It must be a setting I haven't gleaned upon.

What I did glean upon, however, is the option to set a user-defined keybinding that triggers one of a selection of functions, one of which being a toggle for the mic mute:

Discord Keybindings in macOS

Here, I set up one to bind to Toggle Mute. As you'd expect, it works within the application. Surprisingly, it works outwith the application too, when it doesn't have focus, and even when it's minimised. Even more surprisingly, it worked when Discord was resident on a separate desktop (space), and at no point did the Discord app need to attain focus or the active desktop be switched away from.

I believe this is exactly the solution you're after - a hotkey that operates globally on your system to mute your Discord microphone.