(Awesome) TTS help in getting the displayed field read

I have made a card template that randomly chooses to display one of two different fields, and now I am trying to get the displayed field to be read out loud. I use Awesome TTS for all of my other card templates, but this one is different since I cant have A-TTS set to a certain field since it may not be the one being displayed. I’m hoping someone smarter than me knows a way around this. Pasted below is the card template I am working with, but currently Anki chooses either Cloze Phrase 1 or Cloze Phrase 2 to display. Whichever field is chosen is then represented by the id = ‘text’. If I insert ‘text’ into the A-TTS code though, it says field not found.

How do I get anki to read out what is being displayed currently, rather than assigning it to read any particular field?

-Note: I only want the FrontSide read, nothing on the backside.

1 Like

It sounds like this could be done with the inbuilt Anki TTS system. That’s not ideal if you would prefer ATTS but at least it would only change how this note type works. Presumably, Awesome TTS has this issue as its TTS is based on the set field without looking at your card’s template.

Here is the inbuilt TTS guide:
https://docs.ankiweb.net/#/templates/fields?id=text-to-speech

So I think my issue is that I need TTS to look at the template to know which field to speak. Do you know how I could make this happen?

To elaborate, I have the card template set up right now to display either Field1 or Field2 randomly (named Close Phrase 1 & 2). In the script, it assigns the variable ‘text’ to be equal to the whichever field it shows. If I have TTS set up to read Field1, it will read Field1 even if the card is showing Field2. But Anki doesn’t recognize ‘text’ as a field when I try to set up TTS to read what is being displayed.

What would be ideal is if Anki recognized that {{tts en_US:‘text’}} meant to read the field it randomly chose to display, but I don’t know how to do that.

@dylanmcooper Have you tried putting the “tts en_US:‘” in the initial “texts” variables instead of the result?

Also does your “text” div display an Anki field at all? I would have thought Anki fields would be turned into their outputs before your JavaScript was run making their existence in your second script tag meaningless in JavaScript’s eyes.

If I put the “tts en_US:” inside of the <script /script> it gives me a Syntax Error. I think that route is the way to go, I just don’t know how to get around that error.

And it does display it. Instead of showing the word Phrase, it will take either whats in Cloze Field 1 or Cloze Field 2 and display that instead. It took me probably 15 hours to figure out that template since I’m very new to this, but it works perfectly.

Looking around on old Anki pages, it looks like this script (the bottom of the screenshot) has worked in the past. However, I cant get it to speak anything. Do you think I’m on the right track?

TTS tags are processed before the text is delivered to the webview, so you can’t use them for dynamic content.

Thanks for confirming, I expected as much.

A solution may be to non-dynamically generate audio for both and dynamically get rid of one. This code does that for me when clicking the audio button but doesn’t affect autoplay:

<div id=0>{{tts en_US:Capital}}</div><div id="1"> {{tts en_US:Country}}</div>

<script> 
rand = Math.floor(Math.random() * Math.floor(2)); 

document.getElementById( rand ).innerHTML = '';
</script>