Automate Audio with SoundVolumeCommandLine: A Practical Guide

Mastering SoundVolumeCommandLine: Scripts & Shortcuts

Controlling system audio from the command line can boost productivity, automate workflows, and enable headless setups. This guide covers practical commands, reusable scripts, and handy shortcuts for managing volume and mute state across Windows, macOS, and Linux. Examples assume reasonable defaults: common utilities available on each platform.

Quick overview by platform

  • Windows: Use built-in PowerShell with COM or third-party tools like nircmd.
  • macOS: Use the built-in osascript (AppleScript) and osascript -e from shell, or third-party tools like SwitchAudioSource.
  • Linux: Use amixer for ALSA, pactl/pacmd for PulseAudio, or wpctl for PipeWire.

Windows — PowerShell & nircmd examples

  • Increase volume by 5% (PowerShell using COM):

powershell

Add-Type -TypeDefinition @using System.Runtime.InteropServices; public class Vol { [DllImport(”user32.dll”)] public static extern int SendMessageA(IntPtr hWnd, int Msg, int wParam, int lParam); } @ # VK_VOLUMEUP = 0xAF [Vol]::SendMessageA([IntPtr]::Zero, 0x319, 0, 0x0) # alternative approaches may vary by system
  • Simpler with nircmd (download from NirSoft):

powershell

nircmd.exe changesysvolume 3277 # ~5% up (65535 = 100%) nircmd.exe mutesysvolume 2 # toggle mute
  • Toggle mute (PowerShell using CoreAudio API wrappers): prefer using a small wrapper module from PSGallery for robustness.

macOS — osascript and shell

  • Increase volume by 10%:

bash

osascript -e “set volume output volume ((output volume of (get volume settings)) + 10)”
  • Set absolute volume to 50%:

bash

osascript -e “set volume output volume 50”
  • Toggle mute:

bash

osascript -e “set volume output muted not (output muted of (get volume settings))”
  • For multi-device setups, use SwitchAudioSource to select output, then set volume.

Linux — ALSA, PulseAudio, PipeWire

  • ALSA (amixer):

bash

amixer set Master 5%+ # increase 5% amixer set Master 5%- # decrease 5% amixer set Master toggle # toggle mute
  • PulseAudio (pactl):

bash

pactl set-sink-volume @DEFAULT_SINK@ +5% pactl set-sink-volume @DEFAULT_SINK@ 50% pactl set-sink-mute @DEFAULTSINK@ toggle
  • PipeWire (wpctl):

bash

wpctl set-volume @DEFAULT_SINK@ 1.05 # 1.0 = 100% wpctl set-mute @DEFAULTSINK@ toggle

Cross-platform scripting patterns

  • Create small utilities that abstract platform differences. Example pseudocode (bash/PowerShell hybrid idea):
    • Detect OS.
    • Map operations: up/down/set/toggle.
    • Call platform-specific command with consistent arguments.
  • Example shell script (Linux/macOS):

bash

case \(1</span><span class="token" style="color: rgb(163, 21, 21);">"</span><span> </span><span class="token" style="color: rgb(0, 0, 255);">in</span><span> </span><span> up</span><span class="token" style="color: rgb(57, 58, 52);">)</span><span> pactl set-sink-volume @DEFAULT_SINK@ +</span><span class="token" style="color: rgb(54, 172, 170);">\){2:-5}% ;; down) pactl set-sink-volume @DEFAULT_SINK@ -\({2</span><span class="token" style="color: rgb(57, 58, 52);">:-</span><span class="token" style="color: rgb(54, 172, 170);">5}</span><span>% </span><span class="token" style="color: rgb(57, 58, 52);">;</span><span class="token" style="color: rgb(57, 58, 52);">;</span><span> </span><span> </span><span class="token builtin" style="color: rgb(43, 145, 175);">set</span><span class="token" style="color: rgb(57, 58, 52);">)</span><span> pactl set-sink-volume @DEFAULT_SINK@ </span><span class="token" style="color: rgb(54, 172, 170);">\){2} ;; mute) pactl set-sink-mute @DEFAULT_SINK@ toggle ;; esac

Keyboard shortcuts and automation

  • Linux: bind script to media keys via desktop environment (GNOME/KDE) or use xbindkeys.
  • macOS: use Automator or a third-party hotkey tool (BetterTouchTool, Hammerspoon) to run shell/AppleScript.
  • Windows: create a PowerShell script and assign it to hotkeys using tools like AutoHotkey, or use global keyboard shortcuts that call nircmd.

Useful tips and caveats

  • Default sink/source may change when devices connect. Query sinks first (pactl list short sinks) and resolve the active one.
  • Percentage vs absolute values: some tools accept 0–100, others use fractional or 0–65535 ranges—convert consistently.
  • Running from system services may require proper environment (DISPLAY, DB

Comments

Leave a Reply