Hum... there are
many different ways to do what you want to do.
Here are a few ideas :
1 - If the file doesn't exist, does it get created when I open it in my app?
No... unless you want to. You will get an error message into the "the result".
And for writing, you should not worry about it, because when the app will write the preference file, it will create it even if the file doesn't exist previously. No need a special command.
Code: Select all
put URL "file:C:/MYAPP/MYPREF.TXT" into myFile
if the result is not empty then answer "Pref file is missing"
So I would say : don't bother. Just load the file when you open the stack. If it doesn't exist, set you preferences variable with defaut value.
Code: Select all
put URL "file:C:/MYAPP/MYPREF.TXT" into myFile
if myFile is empty then
put "1" into myPreference[1]
put "2" into myPreference[2]
put "3" into myPreference[3]
put "4" into myPreference[4]
put "5" into myPreference[5]
put "6" into myPreference[6]
else
--- read the file content
end if
2. How do I read each value from the file? I was going to use a CSV file. I was going to store each value in a different variable while the app was running, then update the file when the app is exited.
It depends on the item delimiter you want to use. You can use ";" but also tab, etc.
You have 6 values, therefore 6 items
Code: Select all
put URL "file:C:/MYAPP/MYPREF.TXT" into myFile
if myFile is not empty then
set itemdelimiter to tab
repeat with i = 1 to 6
put item i of myFile into myPreference[i]
end repeat
end if
It will put the 6 values into an array
After, you can access let's say value number 2 by doing
If you don't like array, you can use 6 different variables (dont forget to make them "global"):
Code: Select all
put URL "file:C:/MYAPP/MYPREF.TXT" into myFile
set itemdelimiter to tab
put item 1 of myFile into myPref1
put item 2 of myFile into myPref2
put item 3 of myFile into myPref3
put item 4 of myFile into myPref4
put item 5 of myFile into myPref5
put item 6 of myFile into myPref6
Before your app quits, you save the preferences into the file :
Code: Select all
put empty into myFile
repeat with i = 1 to 6
put myPreference[i]&tab after myFile
end repeat
put myFile into URL "file:C:/MYAPP/MYPREF.TXT"