Page 1 of 1
XML Process
Posted: Thu Apr 04, 2013 5:58 am
by Nakia
Hi,
What is the normal process in dealing with loading preference files on Application startup.
I am building my first Desktop Applicatin and have come to the stage of needing to setup Preferences which will be stored in a file.
I am right now deciding if I should create this file as an XML File for portability into other systems or do I create my own format... opinions?
If I do decide to go with XML is it normal on application startup to create the XML Tree from the file and keep that in memory so all parts of the Application can use it if needed?
Is this standard practice...
Re: XML Process
Posted: Thu Apr 04, 2013 11:50 am
by Mark
Hi,
This might get you started:
http://qery.us/3ea
Feel free to ask questions on the blog.
Kind regards,
Mark
Re: XML Process
Posted: Thu Apr 04, 2013 7:02 pm
by jacque
There isn't really a "normal" process, you can create your prefs file in whatever format you like. For simple apps, I've just used a text file with one preference per line, like this:
green
10
DaisyMae
My app knows which line means which preference. But lately I've been using stacks to store preferences because of the many storage options they provide. Usually I set a custom property to the preference, which gives me an automatic array and makes it easy to extract any preference I want:
set the cUserName of stack "prefs" to "DaisyMae"
set the cUserAge of stack "prefs" to "10"
put the cUserName of stack "prefs" into tUserName
--etc
I can store anything in custom properties and it is far easier than messing around with XML, which to me is a waste of time for something like this.
Different OSs require different places to store prefs files. You can use the specialFolderPath("preferences") or the application support folder on Macs -- specialFolderPath("asup"). There's a fantastic folder resource on Ken Ray's site, which I consult all the time:
http://www.sonsothunder.com/devres/live ... ile010.htm Go back one page for a complete list of invaluable tricks and tips like this one.
Re: XML Process
Posted: Thu Apr 04, 2013 7:17 pm
by Simon
Hi jacque,
But lately I've been using stacks to store preferences
I'm getting the odd feeling that this says standalones can save themselves? Have I missed something maybe like standalones can't save themselves but it can create a substack that it can save? Maybe not even create but change an additional stack file that gets saved?
Simon
Re: XML Process
Posted: Thu Apr 04, 2013 8:32 pm
by jacque
No, standalones can't save themselves or save data to their substacks. The prefs stack I was talking about is a separate stack that is saved as an independent file in the appropriate preferences folder. Regular stacks are just documents and can be saved like any other app's documents.
My scripts typically create a new, blank stack that the user never sees, and save it to disk. This can happen on first launch, the first time you need to save a preference, or any other time:
Code: Select all
on savePref pPref,pValue
put specialFolderPath("asup") & "/MyPrefs.lc" into tPrefsFile
if there is no stack tPrefsFile then
create invisible stack "myPrefs"
set the filename of it to tPrefsFile
save stack "myPrefs"
end if
set the pPref of stack "myPrefs" to pValue
end savePref
Re: XML Process
Posted: Thu Apr 04, 2013 8:55 pm
by Simon
Hi jacque,
So the difference between saving a stack file and saving a text file is that you can use:
Code: Select all
put the pPref of stack "myPrefs" into myLocalVar
as opposed to:
Code: Select all
put url "file:myPrefs.txt" into myPrefs
put line 3 of myPrefs into myLocalVar
--or
put item 5 of myPrefs into myLocalVar
Allowing you to use a custom property rather than line/word/item to select?
I like that... "feels" more accurate.
Simon
Re: XML Process
Posted: Thu Apr 04, 2013 10:24 pm
by Nakia
Thanks for the advice.
I kind of agree with Jacque that the XML path seems unnecessarily complicated and I do like the method of saving the Prefs to a Stack File and this method
will overcome the scenario of looking for existing entries etc.
Re: XML Process
Posted: Thu Apr 04, 2013 10:52 pm
by Simon
Hi Nakia,
Mine is still a question up there which I'd like to add to.
Can the custom properties by used like objects?
eg
repeat for the number of customKeys of me --nope
or
repeat for each key tKey of the customKeys of me --nah that didn't work
put the number of customProperties of me into tKey --nope
hmmm... Without addressing the customProperty name specifically, (maybe too big a question) what other ways can one work with the whole data set?
Simon
Re: XML Process
Posted: Fri Apr 05, 2013 1:27 pm
by sturgis
You're close simon
the customkeys -- returns a line delimited list
so your first attempt could be written like so.
Code: Select all
put the customkeys into tKeys
repeat with i = 1 to the number of lines in tKeys
put line i of tKeys
end repeat
2nd method
Code: Select all
repeat for each line tLine in the customkeys of stack "timerclock"
put tLine
end repeat
also from your question change the next option to "put the number of lines in the customkeys of me.."
Might also look at custom property sets, and also be aware you can store an array in a custom property also. So you can have your entire prefs settings in an array and put it into a property, then retrieve the whole array intact from the property on the next run.
Re: XML Process
Posted: Fri Apr 05, 2013 9:39 pm
by Simon
Ha, funny me.
There I was extolling the virtues of using a custom property because it wasn't a chunk expression and the first thing I do is ask how to make it into chunks!
Ha
Simon