Reading text file values into Table, help please.

I’m making an application that reads a text file into a table, and I nead some help, please.

I looked at apples “Archive Maker” example, and it helped with some of the table stuff, but not all.

I know how to read in files, using the cat shell script with applescript, but I need help organizing the text file, and then dumping the values into a table.

Question 1, is it possible to have items in a table with “hidden properties”? Like the item “bob” of a table might also be associated with the path “bob2.txt” that is not visible in the table? If so, how?

If I do that, would I need a variable for every single item on the table? I could always just use a loop, right?

Question 2, when reading a multi line variable in applescript like this one:

bob
bob.txt
bob2
bob2.txt

You know the “word i of myVariable” system right? Well, is it possible to take lines instead of words?

like: set whatever to line 2 of whatever2

Thanks!

Hi,

For seperating the text you can use ‘paragraph’ instead of ‘word’.

Entering the table data depends on how you have it set up. For instance you might use a data source and save your data in records or lists of lists.

gl,

Yeah, I was thiking an array… In applescript, it’s called a “list” right?

Is there any limit to the number of items in a list?

Yeah… anyway. I got it to work great now, but is there any way I can check which slot in a table was clicked?

I’m thinking a loop inside a “on clicked theObject” handler, but I don’t know how to inditify items in tables.

Thanks.

Hi Dude,

If you’re using a data source, then say the identifier for your table is “table”. In your ‘clicked’ handler, you might have a series of if … thens like:

if name of theObject is “button” then
– do something
else if name of theObject is “table” then
set data_row to selected data row of theObject
set c to content of every data cell of data_row
end if

the value of c will be the sublist like:

{“bob”}

Then if you want to get “bob.txt” (your hidden field), you would need to look it up in your stored data. Say you stored it in a list (array) like:

tableData:
{{“bob”, “bob.txt”}, {“bob2”, “bob2.txt”}}

where tableData is the name of the variable that holds the data, then you would need to look it up in this list. for instance:

repeat with this_record in tableData
if item 1 of this_record is “bob” then
set my_hidden_value to item 2 of this_record
exit repeat
end repeat

If you have a lot of data, then this would take too long for most people. There are better ways to store the data and that would be a good question in another post. I don’t know what the limits are for items in a table and items in a list, so you might ask that if you have a lot of items.

Back to the table. One method might be to add an index like:

tableData:
{{1, “bob”, “bob.txt”}, {2, “bob2”, “bob2.txt”}}

Then if your table has 2 columns, appending items would only append the first two fields of each record. Your table would look like this:

1 bob
2 bob2

Then when you get the contents of a selected data row, you would get say:

{2, “bob2”}

Then to get “bob2.txt”, you just need to use the index (2) to get item 2 from your stored list.

item 3 of item 2 of tableData.

But like I said, there are many ways to do this and I don’t know which is the best.

gl,

Wow, thanks you inspired me. :smiley:

I’m planning it to do something like this:


property myUrlList : {}
property myNameList : {}

property nextIsUrl : false

set readIn to do shell script "cat ~/test.txt"

repeat while currentItemNum ≤ (number of paragraphs in readIn)
	set currentItem to paragraph currentItemNum of readIn
	if nextIsUrl is true then
		set myUrlList to myUrlList & currentItem
		set nextIsUrl to false
	else if nextIsUrl is false then
		set myNameList to myNameList & currentItem
		set nextIsUrl to true
	end if	
set currentItemNum to currentItemNum + 1
end repeat

That way, the name (which is read first) will be alligned to value x in array 1, and the path (which is ALWAYS after a name) is aligned into the mirror array.

It may not make sense to you, but it’ll hopefully work for me. :slight_smile:

If I have any further questions, I know where to come.

Hi Dude,

Instead of toggling whether you are don with a record, you can use the range reference form:

paragraphs i thru j of some_text

Then after you have this list, it’s better to add this list to the end of your main list instead of concatenating (slower). Something like this:

property myURLList : {}
property num_fields : 2

set readln to “bob
bob.txt
bob2
bob2.txt”
if length of readln is 0 then return
set num_recs to (count paragraphs of readln) div num_fields
repeat with i from 1 to num_recs
set first_index to ((i - 1) * num_fields) + 1
set rec_list to (paragraphs first_index thru (first_index + 1) of readln)
set end of myURLList to rec_list
end repeat
return myURLList

It doesn’t matter much for small number of fields, but just for future reference.

Another thing I wanted to say on the question of getting values from your table. If you don’t use the data source method, then you keep track of your table data in your list. To get an item from the list, you can use the ‘row clicked’ property of table view. This gives an index of the row and you can get the value by getting the item from the list.

See the Table example for using data source and not using data source.

gl,

Thanks, I’ll try to use anything I can get to efficienate (word? :wink: ) my code as much as possible. This is my LAST applescript program. I must never use this cursed language again, for I must dedicate myself to the harder, but much cooler C. :cool:

One last question:

Can applescript “#include” like other languages, such as C, or C++?

Because it would be great to put all my non-important functions that I use for other programs in one script that I “#include”.

Thanks!

You use ‘load script’ to load libraries of subroutines or you can use use functions from another already opened script app. If you post questions like this in the AppleScirpt bb you will probably get better answers. Also read AppleScriptLanguageGuide.pdf for more info on loading libraries of subroutines. Here’s an article:

http://www.applescriptsourcebook.com/tips/scriptserver.html

gl,

Thanks for the tip.

One last thing:

Why doesn’t this code work:


property myText : "Hello this is character 1: ' and this is character 2: \""

if myText contains "'" and "\"" then
display dialog "Both characters are in the text!"
end if

I don’t get the dialog. :frowning:

Thanks for your time!

When you say…

… AS is actually trying to evaluate “"” as a boolean. Since it doesn’t make any sense as a boolean, and AS can’t coerce it logically into one, it assumes that “"” = false and it fails the comparison. Essentially, the way you’ve written it is is the same as saying…

if (myText contains "'") and false then

Run that and you’ll find that you get the same results.

If you want to have multiple conditions for an if statement, you need to explicitly state them, such as…

if (myText contains "'") and (myText contains "\"") then

j

Wow, thanks for all the great help you guys!

Without this forum, I’d never finish any of my apps. :rolleyes:

'DtC :cool: