Author Topic: Read a random line from a text file  (Read 1268 times)

douglgm

  • Newbie
  • *
  • Posts: 3
Read a random line from a text file
« on: May 07, 2023, 04:30:25 AM »
Hello. How do I read a random line from an existing text file in Voice Attack. I've installed the VAExtensions plugin, but can't see how to just read one line.
Thanks.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4761
  • RTFM
Re: Read a random line from a text file
« Reply #1 on: May 07, 2023, 11:06:01 AM »
That plugin doesn't appear to support splitting the contents of a text file into "lines".

You don't need a plugin to retrieve the contents of a text file; that can be done using the "Value from file/URI" option of the "Set a Text Value" action.


Retrieving a random line, rather than a specific line, can be done using a combination of the "{TXTRANDOM:}" token, and the "Replace with" option of the "Set a Text Value" action.

E.G.
Code: [Select]
Set text [~textFile] to [C:\text.txt] (Replace '{NEWLINE}' with ';')
Write [Blue] '"{TXTRANDOM:{TXT:~textFile}}"' to log

This replaces the newline characters (on Windows this would be a carriage return followed by a newline, which is what will be returned by the "{NEWLINE}" token on that operating system) with semicolons, which are used throughout VoiceAttack to delimit lists of literal text options.
The "{TXTRANDOM:}" token then takes that semicolon-delimited list and returns one of the entries from that list.

If you intend to use this in a "Say Something with Text-To-Speech" action, you don't need the "{TXTRANDOM:}" token, as that action takes semicolon-delimited list directly, E.G.
Code: [Select]
Set text [~textFile] to [C:\text.txt] (Replace '{NEWLINE}' with ';')
Say, '{TXT:~textFile}'

douglgm

  • Newbie
  • *
  • Posts: 3
Re: Read a random line from a text file
« Reply #2 on: May 09, 2023, 12:19:27 PM »
Many thanks for this - I'll have a play when I get a few minutes. There's a lot to learn!

douglgm

  • Newbie
  • *
  • Posts: 3
Re: Read a random line from a text file
« Reply #3 on: May 21, 2023, 02:52:05 AM »
Thanks for this. It was exactly what I needed! Everything is working as I want now!