5

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.

5

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 of LANG=... 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 the say command.

  • As well as a matching closing literal double-quote \", before the pipe to say, to go with the one in echo \"[[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.)

1

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.

  • Sorry, I wasn't clear. I am trying to accomplish the volume change with AppleScript, not in Terminal directly. I don't know where [[volm 0.35]] belongs in the line of code that I provided. – rubik's sphere Mar 18 '17 at 8:12
  • Some thoughts & observations... While cat <(echo [[volm 0.35]] ) <(LANG=en_US.UTF-8 pbpaste -Prefer txt) | say, as is, works okay in a bash shell, it doesn't work in a sh shell, which is what AppleScript uses. So I do not believe it has to do with escaping any part of that particular command. If you open Terminal, assuming it is the default shell, bash, and typed sh and press enter, then execute that same command, it errors out with sh: syntax error near unexpected token `(', the same as in AppleScript. It looks like the sh shell doesn't like Process Substitution. – user3439894 Mar 18 '17 at 12:17
  • While one could preface that command with bash -c , e.g. set this_say_Pid to (do shell script "bash -c \"cat <(echo [[volm 0.35]] ) <(LANG=en_US.UTF-8 pbpaste -Prefer txt) | say > /dev/null 2>&1 & echo $!\""), to actually run it in a bash shell, the say command will then work properly but you will not get the pid of the say command, which of course then defeats the purpose. Hopefully rubik's sphere can use what I've suggested in my answer if there isn't another way to do it along the lines of your suggestion. – user3439894 Mar 18 '17 at 12:18
  • Okay, I figured out a way around the issue the sh shell was having with Process Substitution <(...) and to offer a solution using pbpaste by using Command Substitution $(...) instead, as shown in my updated answer, which still includes my original answer as that shows another way to accomplish the same thing, albeit two lines of code instead of one. :) – user3439894 Mar 18 '17 at 16:18
0

simple answer:

say "[[volm 0.04]] This is a sentence"

Seems to me that from 0.01 to 0.04 is the smaller volume output.

When I tried 0.25 or 0.35 the volume did not change.

You must log in to answer this question.

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