Skip to content

Instantly share code, notes, and snippets.

@na0x2c6
Created May 12, 2021 08:10
Show Gist options
  • Save na0x2c6/11a73948a7a83359267c527d62c35750 to your computer and use it in GitHub Desktop.
Save na0x2c6/11a73948a7a83359267c527d62c35750 to your computer and use it in GitHub Desktop.
Mic mute toggle script on Mac
-- refs: https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html
-- refs: https://medium.com/macoclock/how-in-the-bleep-do-i-mute-my-mic-anywhere-on-macos-d2fa1185b13
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
set theFile to theFile as string
set theOpenedFile to open for access file theFile with write permission
if overwriteExistingContent is true then set eof of theOpenedFile to 0
write theText to theOpenedFile starting at eof
close access theOpenedFile
return true
on error
try
close access file theFile
end try
return false
end try
end writeTextToFile
on readFile(theFile)
set theFile to theFile as string
return read file theFile
end readFile
on getMicrophoneVolume()
input volume of (get volume settings)
end getMicrophoneVolume
on setMicrophoneVolume(vol)
set volume input volume vol
display notification ("mic input is set:" & (vol as text))
end setMicrophoneVolume
set volumeStore to ((path to home folder as text) & ".__micvolume")
set vol to getMicrophoneVolume()
if vol is greater than 0 then
writeTextToFile(vol as text, volumeStore, true)
-- mute
setMicrophoneVolume(0)
else
set _vol to readFile(volumeStore)
-- unmute
setMicrophoneVolume(_vol)
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment