Mute a tab in Safari

Is there any way to script the “mute tab” capability in recent Safari versions? Not to mute system volume, but to replicate the behavior from clicking the volume icon in the address bar when a sound is playing, or alternatively, by right-clicking a tab and choosing “mute this tab”.

Hey djl,

It’s possible using GUI-Scripting.

-Chris


-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/22 21:30
# dMod: 2017/02/22 21:39
# Appl: Safari & System Events
# Task: Mute Sounds Playing in the Active Tab of Safari
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Mute, @Sounds, @Playing, @Active, @Tab, @Safari
-------------------------------------------------------------------------------------------

tell application "System Events"
	tell application process "Safari"
		tell (first window whose subrole is "AXStandardWindow")
			tell toolbar 1
				tell group 2
					tell UI element 1
						set muteButton to first button whose accessibility description contains "Mute"
						tell muteButton
							perform action "AXPress"
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

-------------------------------------------------------------------------------------------

I should have looked before writing a GUI-Script…

“Mute This Tab” and “Mute All Tabs” are available in Safari’s Window menu. :stuck_out_tongue:

-Chris

Curiously the “Mute All Tabs” button is NOT available to ui-scripting.

Just for giggles let’s try scripting the menu items:


-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/22 21:30
# dMod: 2017/02/22 22:17
# Appl: Safari & System Events
# Task: Mute Sounds Playing in the Active Tab of Safari by activating "Mute" Menu Item.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @System_Events, @Mute, @Sounds, @Playing, @Active, @Tab, @Safari
-------------------------------------------------------------------------------------------

tell application "System Events"
	tell application process "Safari"
		tell menu bar 1
			tell menu bar item "Window"
				tell menu 1
					tell (first menu item whose name contains "Mute This Tab")
						# tell (first menu item whose name contains "Mute All Tabs")
						if its enabled is true then
							perform action "AXPress"
						else
							beep -- error
						end if
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

-------------------------------------------------------------------------------------------

This works fine with one odd caveat.

When using the “Mute All Tabs” option you cannot toggle it immediately “ you have to wait about 2 seconds.

The “Mute This Tab” option will toggle virtually instantly.

-Chris