retrieve or pull words randomly form a text file

Hi to all the great mascripters, i am trying to realize a script that retrieves a word randomly from a text file and use this in a keyboard maestro macro, the file is made of lines with one word each, is saved in UTF, here is the code that a friend from macscripter.net sent me but it is not working because of a thing that surely I don’t know.

following the code:

set fileLoc to (Users/minipower/Documents/testnomi.txt)
set nameText to read fileLoc
set tNames to paragraphs of nameText
set oneName to some text of tNames

and of course the error:
script error: Expected “,” but found unknown token. (-2741)

thank you for any piece of advice or help.

Hi,

at least the path must be in double quotes


set fileLoc to ((path to documents folder as text) & "testnomi.txt") -- relative path to the plain text file
set nameText to read file fileLoc
set oneName to some paragraph of nameText

or


set oneName to some item of (read file ((path to documents folder as text) & "testnomi.txt") using delimiter linefeed) -- or return

if the line separator is return (ASCII 13) change linefeed to return

Thank you for your fast reply and help but i tried both solutions and they are not working, can you please insert my path into your example?

with this:
set oneName to some item of (read file ((Users/minipower/Desktop/) & “testnomi.txt”) using delimiter return)

i obtain this error:
script error: Expected expression but found “)”. (-2741)

with this:
set fileLoc to ((/Users/minipower/Desktop) & “testnomi.txt”) – relative path to the plain text file
set nameText to read file fileLoc
set oneName to some paragraph of nameText

i obtain this:
script error: Expected expression, “)”, etc. but found “/”. (-2741)

((Users/minipower/Desktop/) & “testnomi.txt”) = ((path to desktop as text) & “testnomi.txt”)

Thank you very much, it is working like a charm now, i always used windows and this kind of things are different there, completely, i am not a programmer but i had to do something with it in simple way and this is disarming because i could never imagine it was so simple as putting only the name of the predetermined folder.
You were a great help

You failed to enclose a component of the pathname between quotes and forgot a slash (just after Desktop)

Would be :

set fileLoc to “/Users/minipower/Desktop/” & “testnomi.txt”

or as stephanK wrote :

set fileLoc to (path to desktop as text) & “testnomi.txt”)

Yvan KOENIG (VALLAURIS, France) samedi 20 octobre 2012 22:34:25

Who? :wink:

thank you Yvan for the additional informations, i learned something here today.
;):slight_smile: