在OS X Terminal CLI中获取当前的音量级别?


17

我想从Mac上的CLI检查当前音量。我知道我可以这样设置:

osascript -e 'set volume <N>'

但这在尝试获取当前音量时似乎不起作用。

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

Answers:


18

您应该发现get volume settings它将返回一个对象,其中包含输出量和警报量。因此,例如,您可以执行以下操作来检索整个对象:

osascript -e 'get volume settings'

或者更确切地说,这可能只是获取输出量(例如,而不是警报量):

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

...但是请注意,并非所有音频设备都可以通过软件直接控制音量设置。例如,您的显示音频应具有控制权;但是,火线或USB I / O板可能不会在软件控制下进行这些设置(因为它们可能是物理旋钮)。如果特定设置不在软件的控制之下,则它将显示在get volume settings作为“缺失值”或类似内容返回的对象中。


get volume settings不能真正区分0、0.1和0.01。它不显示十进制值,因此非常无用。
Acumenus 2014年

@ABB,很好的建议。感谢您的贡献。
ghoti 2015年

5

我犯了一个非常卑鄙的bash脚本,名为“ chut”。由于我已经厌倦了需要浮点数作为输入的sys量(0到10步长0.1),但是输出的整数是14步,范围是0到100。

去图...如果有人感兴趣:http : //github.com/docgyneco69/chut

在其全部荣耀中:

#!/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 ;

0

使用相同的比例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"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.