In place of the list in

tell app "SpeechRecognitionServer to set theQueryList to listen for {"list item 1", "list item 2"...}

I'd like to read in a text file that contains the list. Something like

set theQueryList to read POSIX file "/Users/username/folder/text.txt"

The file text.txt contains a list, written out as

"list item 1", "list item 2", ... "list item n"

without brackets and in utf8 (I hope) format. (I created the file by cat'ing it in Terminal.)

This works, somewhat. I mean: I can display the list from text.txt in a dialog, and I can use it in a repeat loop. But it doesn't trigger SpeechRecognitionServer when spoken. (I don't know a better way to say this.) I should mention that when I paste the list from this file into the script, it works fine. So the list items don't seem to be at fault -- nor how I say them.

Apologies if I'm a bit fuzzy in my wording here. Grateful for your ideas. Thanks.

  • Your file will need to have the data encoded as an actual list, or if it is just plain text, you will need to do something to convert it to a list, such as getting lines/paragraphs or splitting the text using AppleScript's text item delimiters. – red_menace Sep 16 '19 at 17:05
  • Thanks. It's comma separated, no brackets, no quotation marks. When I include brackets and/or quotation marks, the script timesout -- or returns a -1708 error, which I thought at first had to do with having the file read into the SpeechRecognitionServer tell block, so I moved the read statement up (Script editor owns it) and passed the contents of the file down the line in a variable. Interesting to note that when I make the file one item long, it works like a charm. It's been quite a few years since I've had my head in this stuff; I'm grateful for your help. – user346653 Sep 17 '19 at 2:19

The items to listen for need to be in a list, so you will need to extract the items from the text.

If the items in the text are separated by end-of-ine characters:

list item 1
list item 2
list item 3
list item n

you can just get the paragraphs:

set theQueryList to paragraphs of (read POSIX file "/Users/username/folder/text.txt")

If the items in the text are separated by commas (or tabs, or whatever):

list item 1,list item 2,list item 3,list item n

you can use AppleScript's text item delimiters:

set separator to "," -- the separating text
set fileText to (read POSIX file "/Users/username/folder/text.txt")
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, separator}
set theQueryList to text items of fileText
set AppleScript's text item delimiters to tempTID -- restore original
  • Brilliant. I'm grateful. Truly. – user346653 Sep 17 '19 at 9:58

You must log in to answer this question.