27

I would like to check the current volume level from the CLI on my Mac. I know I can set it like this:

osascript -e 'set volume <N>'

But that doesn't seem to work when trying to get the current volume level.

$ osascript -e 'get volume'
4:10: execution error: The variable volume is not defined. (-2753)

3 Answers 3

27

You should find that get volume settings will return an object containing among other things the output volume and the alert volume. So for example you could do this to retrieve the entire object:

osascript -e 'get volume settings'

or rather maybe this to grab just the output volume (e.g. rather than the alert volume):

osascript -e 'set ovol to output volume of (get volume settings)'

... but note that not all audio devices will have direct software control over volume settings. For example your display audio should have control; however, a firewire or USB i/o board probably would not have those settings under software control (since they might be physical knobs). If the particular setting is not under the control of software then it will show up in the object returned from get volume settings as "missing value" or something like that.

3
  • get volume settings doesn't really differentiate between 0, 0.1, and 0.01. It doesn't show decimal values, making it quite useless.
    – Asclepius
    Commented Apr 18, 2014 at 22:36
  • @A-B-B, great suggestion. Thanks for contributing.
    – ghoti
    Commented Oct 28, 2015 at 0:22
  • @Asclepius the volume values are integers from [0, 100] (inclusive on both ends). Commented May 3, 2022 at 16:30
8

Getting and setting the volume using the same scale 1..100:

# Get current volume as a number from 0 to 100
current_vol=$(osascript -e 'output volume of (get volume settings)')

# Prank co-worker by playing loud noise/music
osascript -e "set volume output volume 100"
afplay sabotage.m4a

# (Re-)set to saved volume as a number from 0 to 100
osascript -e "set volume output volume $current_vol"
2
  • +1 for directly useable commands I could paste right into a script to stop it scaring the hell out of me every time it ran and, if I could, +100 for scripted pranking 😆
    – RedYeti
    Commented Jan 15, 2020 at 10:51
  • +1 for exactly what I was trying to do! Now just to add a cron job...
    – menney
    Commented Dec 15, 2021 at 7:08
5

I committed a very humble bash script named "chut". As I was fed up with the sys volume requiring a float point as input (0 to 10 step 0.1) but outputing an integer with a step 14 ranging from 0 to 100.

Go figure... If anyone interested: http://github.com/docgyneco69/chut

In its full glory:

#!/bin/bash
## CHUT script
## Note: regex [[:digit:]] requires a relatively recent shell
## easy to change with a sed cmd if needed
## applescript arg is not fully bullet proofed for sneaky cmds
## but as no outside arg is passed by the script I kept the usual
## arg format for code readibility (and pure laziness)

# init _x and curr_vol with defaults values (muting)
_x='- 100' ; curr_vol='0' ;

function _usage {echo -e "CHUT is a simple cmd exe to change the system audio volume.
USAGE chut [][-][--][+][++]
      no arg will mute (default)
      [-][+] [--][++] to decrease or increase the volume
      [+++] to set to the maximum
      [-h][--help] display this message
NOTE sys sets volume as float (0-10/0.1) but outputs int (0-100/14)" ; exit 1 ; } ;

# set _x by looping $1 then break as we only use 1st arg, -h or --help to print usage
while [[ "$1" ]]; do case "$1" in
    "-h"|"--help")  _usage      ;;
    "-")        _x='- 0.5'  ;;
    "--")       _x='- 1.0'  ;;
    "+")        _x='+ 0.5'  ;;
    "++")       _x='+ 1.0'  ;;
    "+++")      _x='+ 100'  ;;
    *)      _x='- 100'  ;; # unrecognized values will mute
esac ; break ; done ;

# get current volume value from system (sys volume is 0 to 100 step 14)
curr_vol=$(/usr/bin/osascript -e "get volume settings" | cut -d ',' -f1 | tr -dc [[:digit:]]) ;

# set new volume via _x - use bc for floating point, escape potential errors, 
# print value with one decimal - test & echo the new volume value via applescript
curr_vol=$( printf "%.1f" "$( echo "$curr_vol / 14 $_x" | bc -l 2>&-)" ) ;
(/usr/bin/osascript -e "set Volume "\"$curr_vol"\" ") && \
echo $(/usr/bin/osascript -e "get volume settings" | cut -d ',' -f1 | tr -dc [[:digit:]]) ;

exit 0 ;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .