Hello Bruce,
You were very close. Here is a slightly modified version of your script that will do what you want:
Repeat with n=1 to the number of lines in file "~/Desktop/test"
put line n of file "~/Desktop/test" into data
if chars 1 to 2 of data <> "//" then // Ignore comments
put value(data) into IconView_Icon
put IconView_Icon
Repeat with each item of keys(IconView_Icon)
put it into PropName
put property (it) of IconView_Icon into PropValue
(*? Will be replacing PUT with TypeText? below:? *)
put "The Property Name is: " & PropName
put "The Property Value is: " & PropValue
put " "
end repeat
end if
end repeat
Let me explain the 3 changes I made, and then suggest another idea or two.
First, the key thing you were missing was the use of the value() function (in the 4th line of the script above). When the value is read from the file, it is stored in the “data” variable as a text string. The value() function evaluates this text as a SenseTalk expression, which in this case results in a property list value in place of its text representation.
Second, I put parentheses around “it” at line 8 (“put property (it) of IconView_Icon into PropValue”). Strictly speaking, in the current version of Eggplant this is not necessary. The way you wrote it (without parentheses) is perfectly correct right now. However, we expect this to change for the next release, as we move to correct the inconsistency that exists now between the syntax for accessing a property using the word “property” and the other ways of accessing a property (see the topic “Planned Property Syntax Change” in the “Looking Ahead” forum for a detailed explanation).
Using parentheses ensures that SenseTalk will treat (it) as an expression and therefore use the value of the it variable as the property name and not try to access a property named “it”, both now and in the future.
The third change I made was to delete the first line of your script, opening the test data file. Because the script accesses the data file by simply treating it as a container, it’s not necessary to open the file first. The open file command is only needed when your script will be doing sequential (or random) reading and updating of the file using the read, seek, or write commands. For most purposes it is simpler to ignore those commands and just access the file directly as you have done.
You could make your script even shorter (and avoid having to name the file twice), by changing the repeat loop from this:
Repeat with n=1 to the number of lines in file "~/Desktop/test"
put line n of file "~/Desktop/test" into data
-- do stuff with data here
end repeat
to this:
Repeat with data = each line of file "~/Desktop/test"
-- do stuff with data here
end repeat
Finally, here is an idea for a different way of structuring the data file. Instead of using SenseTalk’s property list format on each line of the data file, you could structure the file as a simple tab-delimited text file. The first line might hold the labels for each of the columns, with the data values on subsequent lines like this:
Value1 CalcOp Value2
1 1 2
3 1 7
2 1 9
A script to read this data file might look like this:
put file "~/Desktop/test" into sourceData
put line 1 of sourceData into header -- get the first line
delete line 1 of sourceData
set the itemDelimiter to tab
Repeat with data = each line of sourceData
set record to (:)
repeat with n=1 to the number of items in header
set property (item n of header) of record to item n of row
end repeat
insert record after recordList
end repeat
At this point, recordList would contain a list of property lists, one for each line of data from the file, with the properties set using the names from the first line of the data file.
This isn’t necessarily a better approach, just different. It has the advantage of being a simpler and smaller data file. The original format you proposed, on the other hand, offers more flexibility, since the record defined on each line could have different sets of keys.
I hope this answer was helpful.
Doug