PDA

View Full Version : Reading prefs from a txt file (applescript)


bnivc2002
05-03-2003, 05:47 AM
Hi. I have two questions. What book should i buy to learn more about applescript?
How do can i get applescript to read values from a txt file and store it in a variable?

Thank you.

at_sym
05-07-2003, 07:24 PM
The AppleScript book that's most often recommended is Danny Goodman's AppleScript Handbook (http://www.dannyg.com/pubs/as/applescriptTOC.html). The first part is a good intro to how AppleScript works and how to write basic scripts, but examples involving TouchBase Pro, HyperCard, and MacWrite Pro make it pretty obvious that Danny hasn't updated this in a while.

If you're looking for more updated info, I'd suggest Ethan Wilde's Visual Quickstart books or the (supposedly forthcoming) AppleScript 1,2,3 (http://www.amazon.com/exec/obidos/ASIN/0321149319/) by Apple's in-house scripting guru Sal Soghoian.

bnivc2002
05-08-2003, 08:50 AM
Thanks you for the answer.
But for now could anyone tell me how to read values from a textfile and store these values into variables?

I have 5 values to be stored. This is because i would like to create a prefernce file.

Thank you

at_sym
05-08-2003, 08:37 PM
To pull a file into AppleScript, you can use:

set myPrefsFile to (choose file with prompt "Select a file to read:" of type {"TEXT"})
open for access myPrefsFile
set prefsContents to (read myPrefsFile)
close access myPrefsFile


That will get the entire file in as a variable. Is this what you are looking for?

bnivc2002
05-08-2003, 09:02 PM
Not realy. I got that far myself.
What I would like to do is get 5 values into 5 diferent variables.

The text file would look like:

1. Some text
2. Some more text etc.

The variables
txtvar1
txtvar2
txtvar3
etc.

Thank you

at_sym
05-09-2003, 02:10 AM
So based on your example, it looks like you've got a return-separated list of text. Correct? If so, you could grab a line at a time with:

set myPrefsFile to (choose file with prompt "Select a file to read:" of type {"TEXT"})
open for access myPrefsFile
set prefsContents to read myPrefsFile using delimiter {return}
close access myPrefsFile
set txtvar1 to item 1 of prefsContents
set txtvar2 to item 2 of prefsContents
...

set txtvar10 to item 10 of prefsContents

Then you can get the value of the first line with:

return txtvar1

That will work if each line is a value you want to capture.

If there is additional text that you don't want (say, the numbers prefixing each line), you'll need to read in the line, chop out what you don't want, then assign them to txtvar[x]. You could do that with the trim_line subroutine (http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.07.htm) described here.

bnivc2002
05-09-2003, 04:38 AM
Thank you at_sym. This is excactly what i was looking for.
I apreciate your help very much :-)