StelFoog
StelFoog

Reputation: 21

Changing Mac volume to a variable in terminal

I'm trying to create a shell script in which I change the volume to max, use the Say command and then change the volume back. So far I'm able to save the old volume as a variable, change the volume to maximum and use the say command. The only trouble I'm having is changing the volume to the variable value.

What I have:

#!/bin/bash

VOL=$(osascript -e 'output volume of (get volume settings)')
osascript -e 'set Volume 100'
say Hello 
osascript -e "set Volume $VOL"

Upvotes: 1

Views: 1215

Answers (1)

Gordon Davisson
Gordon Davisson

Reputation: 125788

For some reason, set Volume x uses a volume scale of 0 to 7, while output volume of (get volume settings) uses a scale of 0 to 100. You could multiply by 0.07, but there's an easier solution, pointed out by Antal Spector-Zabusky: use set volume output volume n, which uses the 0-100 scale.

vol=$(osascript -e 'output volume of (get volume settings)')
osascript -e "set volume output volume 100"
#say "Hello"
osascript -e "set volume output volume $vol"

Upvotes: 2

Related Questions