I need to retreive values from a txt file and send them to the command “TypeText”. For this I started, with basically getting the values from the txt file to a variable, then send the value to TypeText.
Now the issue is, with a simple example, Iam trying to read in values to var from another data file. However, the value is not being loaded to the variable “var”. Always the message “Not equal to 2002” is being logged.
The data file has only 3 values - 2002, test, 2003
Note that the file search.txt is in the scripts folder itself.
open file "search.txt"
read from file "search.txt" until end
put the first word of it into var
put var --doesnot display any value in log
put 2002 into var1
Log"value of var1" & var1
if ( var is equal to var1) then
Log "equal to 2002"
LogError "Not equal to 2002"
close file "search.txt"
Is there a simpler way?
Thanks in advance.
There is a SenseTalk keyword called “file” that you can use to retrieve and write to a file. This is a much simpler method of working with files in that it allows you to work with files similar to how you would work with an ordinary container.
// might have to include path (e.g. "/path/to/data.txt")
put file "data.txt" into var
repeat for each item in var
// display every word
If you need a better example, consult the ‘Using Eggplant’ manual, page 31 (“Data Driven Testing”).
It’s true that accessing a file directly as a container is usually a little simpler than using the open, read, and close commands. Your original script was basically correct, though, so I think some additional explanation of what’s going on may be helpful.
I think the main problem with your script is that it’s not finding your file. When you don’t specify the full path to a file, SenseTalk assumes it’s in the current folder (which can be accessed or changed using “the folder”). You can test for the presence of a file using “if there is a file …”. If you add these lines to the beginning of your script, I think you’ll get a better understanding of what’s happening:
put the folder -- see what the current folder is
put the long name of file "search.txt" -- see the full path that will be used
if there is a file "search.txt" then put "file exists" else put "no file"
You may find that the last line above reports that the file exists even though the path is not the correct one to your file. If so, it’s probably because an earlier run of your script actually created an empty file at that path. This is because the “open file” command defaults to opening a file for updating, which will create the file if it doesn’t already exist.
Since you say your file is in the Scripts folder, one simple solution would be to add this command at the beginning of your script:
set the folder to my folder
Here, “my folder” refers to the folder containing the script. If you don’t want to change “the folder” (since it’s global in scope, and hence changing it might impact other scripts), you could also read the file like this:
put file (my folder & "/search.txt") into var
I hope that helps clarify things. Let us know if you have other questions.
Thanks a lot!
Actually my script was not able to find the correct data file. An empty file was always being created at /users/documents/ folder. using the “set the folder to my folder” corrected the referencing.
Also the other “put file into var” also works.