[bug] Anki can't play tts on Mac due to PATH environment variable problem

I habe installed Anki both on my Macbook and on my Windows PC.
First in Windows I built a card template cloned from the Basic template, and in the Back template I added the text as below:

{{tts de_DE:audio_text_d}}

In Windows the Anki app can play the card using tts normally. But after I synchronized the cards templates and cards using AnkiWeb and and played them in MacOS, an error message poped:

# Error
An error occurred. Please use Tools > Check Database to see if that fixes the problem.
If problems persist, please report the problem on our [support site](https://help.ankiweb.net). Please copy and paste the information below into your report.
Anki 2.1.54 (b6a7760c) Python 3.9.7 Qt 5.14.1 PyQt 5.14.1
Platform: Mac 12.6
Flags: frz=True ao=False sv=2
Add-ons, last update check: 2022-09-18 17:35:33
Caught exception:
Traceback (most recent call last):
File "aqt.webview", line 568, in handler
File "aqt.reviewer", line 730, in _onTypedAnswer
File "aqt.reviewer", line 401, in _showAnswer
File "aqt.sound", line 150, in play_tags
File "aqt.sound", line 209, in _play_next_if_idle
File "aqt.sound", line 212, in _play
File "aqt.sound", line 223, in _best_player_for_tag
File "aqt.tts", line 111, in rank_for_tag
File "aqt.tts", line 76, in voice_for_tag
File "aqt.tts", line 72, in voices
File "aqt.tts", line 190, in get_available_voices
File "subprocess", line 505, in run
File "subprocess", line 951, in __init__
File "subprocess", line 1821, in _execute_child
FileNotFoundError: [Errno 2] No such file or directory: 'say'

I find that “say” is the command in MacOS for tts, and its full path is “/usr/bin/path”, but the execution path list(the PATH environment variable) of the Anki app in MacOS doesn’t contain “/usr/bin”, but it contains “/usr/local/bin”. After I ran the following command in terminal, everything went well:

sudo ln -s /usr/bin/say /usr/local/bin/say

I think the problem is that in some cases in MacOS the PATH environment variable for GUI applications is different from that of the terminal, the former doesn’t contain “/usr/bin” and the latter contains “/usr/bin”. So there is a method to solve this problem: on the start of the Anki desktop app, check if the running OS is Macos, and if yes, check if the PATH environment variable contains “/usr/bin”, if no, add “/usr/bin” to the PATH environment variable.
P.S.
The used Anki installation image is anki-2.1.54-mac-intel-qt5.dmg. The OS version is macOS Monterey 12.6.

@dae I’m not sure if the advice here can be seen by the anki desktop team. Do I need to post it again at “Issues · ankitects/anki(github)”?

As far as I’m aware, /usr/bin is on the path in a default macOS installation - maybe you customized it or your login shell in the past?

No, the PATH environment for any GUI app that is started by clicking its icon or through spotlight is different from that is launched from terminal. The latter inherits PATH set through ~/.profile or any other startup script, but the former doesn’t get such things. You can have a look at [Set system-wide PATH environment variable for Mac OS GUI apps - Bounga’s Home](I can’t include link in my reply, so you can search the title in google.) about this.
On MacOS 12, one needs to set PATH explicitly in Anki.app/Contents/Info.plist as below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<!-- other settings -->
	<key>LSEnvironment</key>
	<dict>
		<key>PATH</key>
		<string>/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
	</dict>
</dict>
</plist>

P.S. I managed to play tts by modifying Info.plist of the Anki packaged created by myself through pyinstaller, but it doesn’t work for the Anki.app package installed through the official dmg.

Yes, I’m aware of the how GUI apps will not inherit paths set in login shells. But my point is that I think macOS includes /usr/bin in the standard path that GUI apps have - if it did not, tts would be failing for other macOS users as well. That makes me wonder whether you may have customized something in the past which has caused the path to be missing on your system.

Maybe you are right that my macOS has been customized. It’s a pity that I can’t remember about it. Do you know how to fix this thing? I have tried several methods found via google but I haven’t managed it.
BTW, I begin to use HyperTTS now. This is a super Add-On, even if its full power is not free of charge.

Sorry, I don’t know off the top of my head.