How to change volume of "say" in AppleScript?
Here is a line from my AppleScript that speaks the selected text:
set this_say_Pid to do shell script "LANG=en_US.UTF-8 pbpaste -Prefer txt | say > /dev/null 2>&1 & echo $!"
I would like the speaking volume to be much lower. I would prefer not to accomplish this by decreasing my overall system volume.
I can successfully decrease the volume of say
in Terminal with the following code:
say "[[volm 0.35]] This is a sentence"
But, when I insert [[volm 0.35]]
in my do shell script
string, the volume does not change.
Since you are piping the output of the pbpaste
command directly to say
command, then e.g. [[volm 0.35]]
would need to be a part of what's on the clipboard as e.g. [[volm 0.35]]
must precede the content of what was actually going to be said.
I'd try using the following in place of what you are using:
set howLoudAndWhatToSay to "[[volm 0.35]] \"" & (get the clipboard as string) & "\""
set this_say_Pid to do shell script "say " & howLoudAndWhatToSay & " > /dev/null 2>&1 & echo $!"
Update: If you want to stick with using pbpaste
, then this example command should work:
set this_say_Pid to (do shell script "echo \"[[volm 0.35]] $(LANG=en_US.UTF-8 pbpaste -Prefer txt)\" | say > /dev/null 2>&1 & echo $!")
Note the primary differences between the command within the do shell script "..."
command in your question and my answer.
-
echo \"[[volm 0.35]]
is added in front ofLANG=...
and note the space after]]
. The
LANG=en_US.UTF-8 pbpaste -Prefer txt
is now enclosed in$(...)
which is using Command Substitution to, in essence, concatenate what gets echoed to the pipe ahead of thesay
command.As well as a matching closing literal double-quote
\"
, before the pipe tosay
, to go with the one inecho \"[[volm 0.35]]
. It did work in limited testing without the use of the opening and closing double-quotes however it's probably better to encase it in the double-quotes to account for something the shell might try to unnecessarily expand.
That said, on my system using 0.35
for the value in [[volm 0.35]]
didn't work well in that is was difficult to perceive the difference in volume from my normal setting. However using 0.3
in [[volm 0.3]]
the difference was notable. (This is one of the reasons why I used "e.g. [[volm 0.35]]
" in my opening sentence.)
Simply move the first double quote: ... "say [[volm 0.35]] This is a sentence"
In Terminal.app the quotes aren't required at all and say [[volm 0.35]] This is a sentence
simply works.
In your code line you would have to prepend [[volm 0.35]]
to the copied text.
In Terminal the following line would work:
cat <(echo [[volm 0.35]] ) <(LANG=en_US.UTF-8 pbpaste -Prefer txt) | say
but I don't get this to work in your AppleScript line properly - probably I have to escape one or several items.