Applescript to mute Discord
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.
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.
Use applescript to click the top 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:
How can I make this work? Any help is greatly appreciated. Thanks
applescript
New contributor
|
show 2 more comments
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.
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.
Use applescript to click the top 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:
How can I make this work? Any help is greatly appreciated. Thanks
applescript
New contributor
By the way, with your first option, you mention "send[ing] the key press". To what does this refer ? What key press are you sending, what is it doing, and why is the application flashing up ?
– CJK
4 hours ago
@CJK I tried this:activate application "Discord" tell application "System Events" key code 46 using {shift down, command down} -- shift-command-left end tell
. It switches to the application and sends the key press to mute my microphone, but doing activate pops up the application.
– Spaisekraft
2 hours ago
And what does ⌘⇧← do (I know it mutes the microphone) ? I suppose I mean, how does it do it: is it a menu shortcut in the app, or a builtin keybinding ?
– CJK
1 hour ago
Ah yes, it's a key combo set in Discord. And you're right in your edit that it works without focus on the app. I've been using the key combo to toggle mute, but I wanted to make it a button on my touchbar with BTT.
– Spaisekraft
1 hour ago
As your own tests have revealed, the simulated keypresses are not being picked up by the Discord keybindings. In theory, there'd be no need to target the Discord app itself, as its a systemwide keybinding. Therefore, telling System Events just to issue the appropriate keyevents should have, in my opinion, worked. But it didn't. I double-checked to make sure it wasn't an AppleScript bug, and wrote some Objective C code to simulate the keyevents and target Discord (which you can do with ObjC, but not AppleScript). It failed. Cont'd...
– CJK
1 hour ago
|
show 2 more comments
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.
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.
Use applescript to click the top 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:
How can I make this work? Any help is greatly appreciated. Thanks
applescript
New contributor
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.
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.
Use applescript to click the top 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:
How can I make this work? Any help is greatly appreciated. Thanks
applescript
applescript
New contributor
New contributor
New contributor
asked 6 hours ago
Spaisekraft
1162
1162
New contributor
New contributor
By the way, with your first option, you mention "send[ing] the key press". To what does this refer ? What key press are you sending, what is it doing, and why is the application flashing up ?
– CJK
4 hours ago
@CJK I tried this:activate application "Discord" tell application "System Events" key code 46 using {shift down, command down} -- shift-command-left end tell
. It switches to the application and sends the key press to mute my microphone, but doing activate pops up the application.
– Spaisekraft
2 hours ago
And what does ⌘⇧← do (I know it mutes the microphone) ? I suppose I mean, how does it do it: is it a menu shortcut in the app, or a builtin keybinding ?
– CJK
1 hour ago
Ah yes, it's a key combo set in Discord. And you're right in your edit that it works without focus on the app. I've been using the key combo to toggle mute, but I wanted to make it a button on my touchbar with BTT.
– Spaisekraft
1 hour ago
As your own tests have revealed, the simulated keypresses are not being picked up by the Discord keybindings. In theory, there'd be no need to target the Discord app itself, as its a systemwide keybinding. Therefore, telling System Events just to issue the appropriate keyevents should have, in my opinion, worked. But it didn't. I double-checked to make sure it wasn't an AppleScript bug, and wrote some Objective C code to simulate the keyevents and target Discord (which you can do with ObjC, but not AppleScript). It failed. Cont'd...
– CJK
1 hour ago
|
show 2 more comments
By the way, with your first option, you mention "send[ing] the key press". To what does this refer ? What key press are you sending, what is it doing, and why is the application flashing up ?
– CJK
4 hours ago
@CJK I tried this:activate application "Discord" tell application "System Events" key code 46 using {shift down, command down} -- shift-command-left end tell
. It switches to the application and sends the key press to mute my microphone, but doing activate pops up the application.
– Spaisekraft
2 hours ago
And what does ⌘⇧← do (I know it mutes the microphone) ? I suppose I mean, how does it do it: is it a menu shortcut in the app, or a builtin keybinding ?
– CJK
1 hour ago
Ah yes, it's a key combo set in Discord. And you're right in your edit that it works without focus on the app. I've been using the key combo to toggle mute, but I wanted to make it a button on my touchbar with BTT.
– Spaisekraft
1 hour ago
As your own tests have revealed, the simulated keypresses are not being picked up by the Discord keybindings. In theory, there'd be no need to target the Discord app itself, as its a systemwide keybinding. Therefore, telling System Events just to issue the appropriate keyevents should have, in my opinion, worked. But it didn't. I double-checked to make sure it wasn't an AppleScript bug, and wrote some Objective C code to simulate the keyevents and target Discord (which you can do with ObjC, but not AppleScript). It failed. Cont'd...
– CJK
1 hour ago
By the way, with your first option, you mention "send[ing] the key press". To what does this refer ? What key press are you sending, what is it doing, and why is the application flashing up ?
– CJK
4 hours ago
By the way, with your first option, you mention "send[ing] the key press". To what does this refer ? What key press are you sending, what is it doing, and why is the application flashing up ?
– CJK
4 hours ago
@CJK I tried this:
activate application "Discord" tell application "System Events" key code 46 using {shift down, command down} -- shift-command-left end tell
. It switches to the application and sends the key press to mute my microphone, but doing activate pops up the application.– Spaisekraft
2 hours ago
@CJK I tried this:
activate application "Discord" tell application "System Events" key code 46 using {shift down, command down} -- shift-command-left end tell
. It switches to the application and sends the key press to mute my microphone, but doing activate pops up the application.– Spaisekraft
2 hours ago
And what does ⌘⇧← do (I know it mutes the microphone) ? I suppose I mean, how does it do it: is it a menu shortcut in the app, or a builtin keybinding ?
– CJK
1 hour ago
And what does ⌘⇧← do (I know it mutes the microphone) ? I suppose I mean, how does it do it: is it a menu shortcut in the app, or a builtin keybinding ?
– CJK
1 hour ago
Ah yes, it's a key combo set in Discord. And you're right in your edit that it works without focus on the app. I've been using the key combo to toggle mute, but I wanted to make it a button on my touchbar with BTT.
– Spaisekraft
1 hour ago
Ah yes, it's a key combo set in Discord. And you're right in your edit that it works without focus on the app. I've been using the key combo to toggle mute, but I wanted to make it a button on my touchbar with BTT.
– Spaisekraft
1 hour ago
As your own tests have revealed, the simulated keypresses are not being picked up by the Discord keybindings. In theory, there'd be no need to target the Discord app itself, as its a systemwide keybinding. Therefore, telling System Events just to issue the appropriate keyevents should have, in my opinion, worked. But it didn't. I double-checked to make sure it wasn't an AppleScript bug, and wrote some Objective C code to simulate the keyevents and target Discord (which you can do with ObjC, but not AppleScript). It failed. Cont'd...
– CJK
1 hour ago
As your own tests have revealed, the simulated keypresses are not being picked up by the Discord keybindings. In theory, there'd be no need to target the Discord app itself, as its a systemwide keybinding. Therefore, telling System Events just to issue the appropriate keyevents should have, in my opinion, worked. But it didn't. I double-checked to make sure it wasn't an AppleScript bug, and wrote some Objective C code to simulate the keyevents and target Discord (which you can do with ObjC, but not AppleScript). It failed. Cont'd...
– CJK
1 hour ago
|
show 2 more comments
1 Answer
1
active
oldest
votes
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:
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.
Thank you for your reply CJK. I'm running in to a problem with the script you wrote. When it reaches the following section:repeat until it exists delay 0.2 end repeat
It waits until I click the menu myself. I'm guessing that's because the menu doesn't exist until I open it. Once I open the menu the script continues and clicks "Mute". Do you have any suggestions on how to solve this? Thanks again Edit: I see the comment reply ignores formatting. Sorry for the mess.
– Spaisekraft
2 hours ago
Are you saying that the menu won't materialise unless you physically click the menu bar icon yourself ? If so, this means the firstclick
command had no effect. Try the following adjustments separately: 1. change the firstclick
toclick it
; then 2. changeclick it
toperform its action "AXPress"
; then, in a new script, trytell app "System Events" to click menu bar item 1 of menu bar 2 of process "Discord"
. My hope is that (1) or (2) will solve the problem, but my feeling is it won't change anything. (3), I hope, will open the menu (but not click "Mute").
– CJK
1 hour ago
PS. Should none of those suggested changes work, it implies the problem lies with the Discord app and not the script. Somewhere in development, they haven't properly set the accessibility properties of the menu bar icon to allow simulated mouse events from outside sources, or something of parallel nature. As the script works as expected with other menu bar icons from other applications, I fear there's no immediate workaround. However, I have posted an edit to my answer that provides a possible second solution to your dilemma. Let me know if it works for you like it does for me.
– CJK
1 hour ago
Yes the script does work for other applications, but none of the other changes would work for Discord. I did just end up creating a script to toggle mute the mic in the OS, and then run the script on the touchbar using BTT. I'll add that to my original post in case anyone else ever ends up here trying to accomplish this. Thank you for all the help. I've learned a few more things about applescript today :)
– Spaisekraft
1 hour ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "118"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Spaisekraft is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fapple.stackexchange.com%2fquestions%2f347057%2fapplescript-to-mute-discord%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
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.
Thank you for your reply CJK. I'm running in to a problem with the script you wrote. When it reaches the following section:repeat until it exists delay 0.2 end repeat
It waits until I click the menu myself. I'm guessing that's because the menu doesn't exist until I open it. Once I open the menu the script continues and clicks "Mute". Do you have any suggestions on how to solve this? Thanks again Edit: I see the comment reply ignores formatting. Sorry for the mess.
– Spaisekraft
2 hours ago
Are you saying that the menu won't materialise unless you physically click the menu bar icon yourself ? If so, this means the firstclick
command had no effect. Try the following adjustments separately: 1. change the firstclick
toclick it
; then 2. changeclick it
toperform its action "AXPress"
; then, in a new script, trytell app "System Events" to click menu bar item 1 of menu bar 2 of process "Discord"
. My hope is that (1) or (2) will solve the problem, but my feeling is it won't change anything. (3), I hope, will open the menu (but not click "Mute").
– CJK
1 hour ago
PS. Should none of those suggested changes work, it implies the problem lies with the Discord app and not the script. Somewhere in development, they haven't properly set the accessibility properties of the menu bar icon to allow simulated mouse events from outside sources, or something of parallel nature. As the script works as expected with other menu bar icons from other applications, I fear there's no immediate workaround. However, I have posted an edit to my answer that provides a possible second solution to your dilemma. Let me know if it works for you like it does for me.
– CJK
1 hour ago
Yes the script does work for other applications, but none of the other changes would work for Discord. I did just end up creating a script to toggle mute the mic in the OS, and then run the script on the touchbar using BTT. I'll add that to my original post in case anyone else ever ends up here trying to accomplish this. Thank you for all the help. I've learned a few more things about applescript today :)
– Spaisekraft
1 hour ago
add a comment |
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:
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.
Thank you for your reply CJK. I'm running in to a problem with the script you wrote. When it reaches the following section:repeat until it exists delay 0.2 end repeat
It waits until I click the menu myself. I'm guessing that's because the menu doesn't exist until I open it. Once I open the menu the script continues and clicks "Mute". Do you have any suggestions on how to solve this? Thanks again Edit: I see the comment reply ignores formatting. Sorry for the mess.
– Spaisekraft
2 hours ago
Are you saying that the menu won't materialise unless you physically click the menu bar icon yourself ? If so, this means the firstclick
command had no effect. Try the following adjustments separately: 1. change the firstclick
toclick it
; then 2. changeclick it
toperform its action "AXPress"
; then, in a new script, trytell app "System Events" to click menu bar item 1 of menu bar 2 of process "Discord"
. My hope is that (1) or (2) will solve the problem, but my feeling is it won't change anything. (3), I hope, will open the menu (but not click "Mute").
– CJK
1 hour ago
PS. Should none of those suggested changes work, it implies the problem lies with the Discord app and not the script. Somewhere in development, they haven't properly set the accessibility properties of the menu bar icon to allow simulated mouse events from outside sources, or something of parallel nature. As the script works as expected with other menu bar icons from other applications, I fear there's no immediate workaround. However, I have posted an edit to my answer that provides a possible second solution to your dilemma. Let me know if it works for you like it does for me.
– CJK
1 hour ago
Yes the script does work for other applications, but none of the other changes would work for Discord. I did just end up creating a script to toggle mute the mic in the OS, and then run the script on the touchbar using BTT. I'll add that to my original post in case anyone else ever ends up here trying to accomplish this. Thank you for all the help. I've learned a few more things about applescript today :)
– Spaisekraft
1 hour ago
add a comment |
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:
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.
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:
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.
edited 1 hour ago
answered 4 hours ago
CJK
2,573113
2,573113
Thank you for your reply CJK. I'm running in to a problem with the script you wrote. When it reaches the following section:repeat until it exists delay 0.2 end repeat
It waits until I click the menu myself. I'm guessing that's because the menu doesn't exist until I open it. Once I open the menu the script continues and clicks "Mute". Do you have any suggestions on how to solve this? Thanks again Edit: I see the comment reply ignores formatting. Sorry for the mess.
– Spaisekraft
2 hours ago
Are you saying that the menu won't materialise unless you physically click the menu bar icon yourself ? If so, this means the firstclick
command had no effect. Try the following adjustments separately: 1. change the firstclick
toclick it
; then 2. changeclick it
toperform its action "AXPress"
; then, in a new script, trytell app "System Events" to click menu bar item 1 of menu bar 2 of process "Discord"
. My hope is that (1) or (2) will solve the problem, but my feeling is it won't change anything. (3), I hope, will open the menu (but not click "Mute").
– CJK
1 hour ago
PS. Should none of those suggested changes work, it implies the problem lies with the Discord app and not the script. Somewhere in development, they haven't properly set the accessibility properties of the menu bar icon to allow simulated mouse events from outside sources, or something of parallel nature. As the script works as expected with other menu bar icons from other applications, I fear there's no immediate workaround. However, I have posted an edit to my answer that provides a possible second solution to your dilemma. Let me know if it works for you like it does for me.
– CJK
1 hour ago
Yes the script does work for other applications, but none of the other changes would work for Discord. I did just end up creating a script to toggle mute the mic in the OS, and then run the script on the touchbar using BTT. I'll add that to my original post in case anyone else ever ends up here trying to accomplish this. Thank you for all the help. I've learned a few more things about applescript today :)
– Spaisekraft
1 hour ago
add a comment |
Thank you for your reply CJK. I'm running in to a problem with the script you wrote. When it reaches the following section:repeat until it exists delay 0.2 end repeat
It waits until I click the menu myself. I'm guessing that's because the menu doesn't exist until I open it. Once I open the menu the script continues and clicks "Mute". Do you have any suggestions on how to solve this? Thanks again Edit: I see the comment reply ignores formatting. Sorry for the mess.
– Spaisekraft
2 hours ago
Are you saying that the menu won't materialise unless you physically click the menu bar icon yourself ? If so, this means the firstclick
command had no effect. Try the following adjustments separately: 1. change the firstclick
toclick it
; then 2. changeclick it
toperform its action "AXPress"
; then, in a new script, trytell app "System Events" to click menu bar item 1 of menu bar 2 of process "Discord"
. My hope is that (1) or (2) will solve the problem, but my feeling is it won't change anything. (3), I hope, will open the menu (but not click "Mute").
– CJK
1 hour ago
PS. Should none of those suggested changes work, it implies the problem lies with the Discord app and not the script. Somewhere in development, they haven't properly set the accessibility properties of the menu bar icon to allow simulated mouse events from outside sources, or something of parallel nature. As the script works as expected with other menu bar icons from other applications, I fear there's no immediate workaround. However, I have posted an edit to my answer that provides a possible second solution to your dilemma. Let me know if it works for you like it does for me.
– CJK
1 hour ago
Yes the script does work for other applications, but none of the other changes would work for Discord. I did just end up creating a script to toggle mute the mic in the OS, and then run the script on the touchbar using BTT. I'll add that to my original post in case anyone else ever ends up here trying to accomplish this. Thank you for all the help. I've learned a few more things about applescript today :)
– Spaisekraft
1 hour ago
Thank you for your reply CJK. I'm running in to a problem with the script you wrote. When it reaches the following section:
repeat until it exists delay 0.2 end repeat
It waits until I click the menu myself. I'm guessing that's because the menu doesn't exist until I open it. Once I open the menu the script continues and clicks "Mute". Do you have any suggestions on how to solve this? Thanks again Edit: I see the comment reply ignores formatting. Sorry for the mess.– Spaisekraft
2 hours ago
Thank you for your reply CJK. I'm running in to a problem with the script you wrote. When it reaches the following section:
repeat until it exists delay 0.2 end repeat
It waits until I click the menu myself. I'm guessing that's because the menu doesn't exist until I open it. Once I open the menu the script continues and clicks "Mute". Do you have any suggestions on how to solve this? Thanks again Edit: I see the comment reply ignores formatting. Sorry for the mess.– Spaisekraft
2 hours ago
Are you saying that the menu won't materialise unless you physically click the menu bar icon yourself ? If so, this means the first
click
command had no effect. Try the following adjustments separately: 1. change the first click
to click it
; then 2. change click it
to perform its action "AXPress"
; then, in a new script, try tell app "System Events" to click menu bar item 1 of menu bar 2 of process "Discord"
. My hope is that (1) or (2) will solve the problem, but my feeling is it won't change anything. (3), I hope, will open the menu (but not click "Mute").– CJK
1 hour ago
Are you saying that the menu won't materialise unless you physically click the menu bar icon yourself ? If so, this means the first
click
command had no effect. Try the following adjustments separately: 1. change the first click
to click it
; then 2. change click it
to perform its action "AXPress"
; then, in a new script, try tell app "System Events" to click menu bar item 1 of menu bar 2 of process "Discord"
. My hope is that (1) or (2) will solve the problem, but my feeling is it won't change anything. (3), I hope, will open the menu (but not click "Mute").– CJK
1 hour ago
PS. Should none of those suggested changes work, it implies the problem lies with the Discord app and not the script. Somewhere in development, they haven't properly set the accessibility properties of the menu bar icon to allow simulated mouse events from outside sources, or something of parallel nature. As the script works as expected with other menu bar icons from other applications, I fear there's no immediate workaround. However, I have posted an edit to my answer that provides a possible second solution to your dilemma. Let me know if it works for you like it does for me.
– CJK
1 hour ago
PS. Should none of those suggested changes work, it implies the problem lies with the Discord app and not the script. Somewhere in development, they haven't properly set the accessibility properties of the menu bar icon to allow simulated mouse events from outside sources, or something of parallel nature. As the script works as expected with other menu bar icons from other applications, I fear there's no immediate workaround. However, I have posted an edit to my answer that provides a possible second solution to your dilemma. Let me know if it works for you like it does for me.
– CJK
1 hour ago
Yes the script does work for other applications, but none of the other changes would work for Discord. I did just end up creating a script to toggle mute the mic in the OS, and then run the script on the touchbar using BTT. I'll add that to my original post in case anyone else ever ends up here trying to accomplish this. Thank you for all the help. I've learned a few more things about applescript today :)
– Spaisekraft
1 hour ago
Yes the script does work for other applications, but none of the other changes would work for Discord. I did just end up creating a script to toggle mute the mic in the OS, and then run the script on the touchbar using BTT. I'll add that to my original post in case anyone else ever ends up here trying to accomplish this. Thank you for all the help. I've learned a few more things about applescript today :)
– Spaisekraft
1 hour ago
add a comment |
Spaisekraft is a new contributor. Be nice, and check out our Code of Conduct.
Spaisekraft is a new contributor. Be nice, and check out our Code of Conduct.
Spaisekraft is a new contributor. Be nice, and check out our Code of Conduct.
Spaisekraft is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Ask Different!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fapple.stackexchange.com%2fquestions%2f347057%2fapplescript-to-mute-discord%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
By the way, with your first option, you mention "send[ing] the key press". To what does this refer ? What key press are you sending, what is it doing, and why is the application flashing up ?
– CJK
4 hours ago
@CJK I tried this:
activate application "Discord" tell application "System Events" key code 46 using {shift down, command down} -- shift-command-left end tell
. It switches to the application and sends the key press to mute my microphone, but doing activate pops up the application.– Spaisekraft
2 hours ago
And what does ⌘⇧← do (I know it mutes the microphone) ? I suppose I mean, how does it do it: is it a menu shortcut in the app, or a builtin keybinding ?
– CJK
1 hour ago
Ah yes, it's a key combo set in Discord. And you're right in your edit that it works without focus on the app. I've been using the key combo to toggle mute, but I wanted to make it a button on my touchbar with BTT.
– Spaisekraft
1 hour ago
As your own tests have revealed, the simulated keypresses are not being picked up by the Discord keybindings. In theory, there'd be no need to target the Discord app itself, as its a systemwide keybinding. Therefore, telling System Events just to issue the appropriate keyevents should have, in my opinion, worked. But it didn't. I double-checked to make sure it wasn't an AppleScript bug, and wrote some Objective C code to simulate the keyevents and target Discord (which you can do with ObjC, but not AppleScript). It failed. Cont'd...
– CJK
1 hour ago