Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mayo
Last active September 23, 2020 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayo/bc967e167491bf6642899d2d24d6fd7b to your computer and use it in GitHub Desktop.
Save mayo/bc967e167491bf6642899d2d24d6fd7b to your computer and use it in GitHub Desktop.
AppleScript to toggle the input device (microphone) volume between 0 (muted) and set volume
on run
set vol_level to (input volume of (get volume settings))
if (vol_level < 1) then
set volume input volume ReadVolumeLevel()
else
set volume input volume 0
WriteVolumeLevel(vol_level)
return vol_level
end if
return vol_level
end run
on WriteVolumeLevel(volume_level)
try
set open_volume_file to ((open for access (POSIX path of (path to home folder from local domain) & ".mic_level") with write permission))
set eof of open_volume_file to 0
write volume_level to the open_volume_file starting at eof as «class utf8»
close access the open_volume_file
return true
on error
try
close access file open_volume_file
end try
dialog("failed")
return false
end try
end WriteVolumeLevel
on ReadVolumeLevel()
try
set volume_file to (open for access (POSIX path of (path to home folder from local domain) & ".mic_level"))
set vol to first paragraph of (read volume_file for (get eof volume_file) as «class utf8»)
close access volume_file
return vol
on error
return 100
end try
end ReadVolumeLevel
@mayo
Copy link
Author

mayo commented Sep 23, 2020

The script can be used as an Automator Quick Action and activated with global shortcut (set in System Preferences) or from the Services menu. Check the "Ignore this action's input" in the Run options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment