Last active
September 23, 2020 20:37
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.