Saving Information

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Saving Information

Post by bsouthuk » Mon Apr 12, 2010 6:25 pm

Hi All

I have developed some quote generation software which i'm sure lots of you have gathered seeing all the posts i have added on here lately!

The problem I am facing at the moment is that when a user has made selections in my application and wants to save what he/she has done so far, is there a way in doing this without having to start the selection process all over again? Would be great if a user could press a button and Rev will take a 'screen shot' then if they close down my applciation then re open, the user could press another button take the back to the stage they were at. I know I may have to implement so kind of database but is it possible without a database on a basic level?

As always, any help would be most appreciated.

Cheers

Daniel

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving Information

Post by Klaus » Thu Apr 15, 2010 2:20 pm

Hi Daniel,

looks like you need to write the last "state" of your app into some prefs file and "restore" this when the app starts again.
Maybe something like this "on closestack":

Code: Select all

---
## For OS X
put the hilite of btn "xyz" into url("file:" & specialfolderpath("preferences") & "/Name of your app Prefs.txt")
...
And restore the value "on openstack":

Code: Select all

...
put specialfolderpath("preferences") & "/Name of your app Prefs.txt" into tPrefsFile
if there is a file tPrefsFile then
  put url("file:" & tPrefsFile) into tPrefs
  set the hilite of btn "xyz" to tPrefs
end if
...
You get the picture :)


Best

Klaus

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: Saving Information

Post by bsouthuk » Thu Apr 15, 2010 3:15 pm

Thanks Kluas - that's exactly what I need!

Though I cannot get this working. Let me show you what I have done:

Firstly I have enterered the following script into the stack script

Code: Select all


on closestack
   put the hilite of btn "test" into url("file:" & specialfolderpath("preferences") & "/Name of your app Prefs.txt")
end closestack

on openstack
   put specialfolderpath("preferences") & "/Name of your app Prefs.txt" into tPrefsFile
if there is a file tPrefsFile then
put url("file:" & tPrefsFile) into tPrefs
set the hilite of btn "test" to tPrefs
end if
end openstack

The only code I have changed of yours is the name of the button. Do I need to change anything else?

I have tested this script and added 3 fields to the stack. If i put some content into these fields, then close the stack and reopen, the content disppears so its not saving the state for some reason. Can you see where I am going wrong?

Thanks

Dan

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving Information

Post by Klaus » Thu Apr 15, 2010 4:12 pm

Hi Dan,

hm, works here on my OS X. What platform are you on? Mac or Win?
On Windows you must use "specialfolderpath(26)" whcih is the "Application Data" folder for the current user, if I remember right.

Please check:
1. if the file has really been written!
You could check "the result" after the writing whcih should be empty on success like:

Code: Select all

...
put the hilite of btn "test" into url("file:" & specialfolderpath("preferences") & "/Name of your app Prefs.txt")
if the result <> empty then
   ## Get a hint why the file could not be written!
   answer the result
end if
...
2. The content of that file:

Code: Select all

...
if there is a file tPrefsFile then
  put url("file:" & tPrefsFile) into tPrefs
  ANSWER tPrefs ##!!
  set the hilite of btn "xyz" to tPrefs
end if
...

Remember that "the hilite of btn xyz" was just a quick example which would only make sense for checkboxes!


Best

Klaus

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: Saving Information

Post by bsouthuk » Thu Apr 15, 2010 4:40 pm

Right OK this is where I am going wrong klaus

An error pop up box states 'cant open file' when I close the stack.

When you say that I havent written the file - what do you mean? Sorry to be blonde but am using windows...

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving Information

Post by Klaus » Thu Apr 15, 2010 4:43 pm

Hi Dan,

hey, I AM blonde, so mind your words! :D
Did you try "specialfolderpath(26)" (necessary for Windows!) as I mentioned?


Best

Klaus

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: Saving Information

Post by bsouthuk » Thu Apr 15, 2010 4:53 pm

lol Klaus you are the man! - thank you. Works with the button example now!

However, if the state of the application is far more complicated - and needs to save every field on the stack (there are around 100) and also the buttons that are displayed in the state then is this pretty straight forward? How would I amend the script for this?

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving Information

Post by Klaus » Thu Apr 15, 2010 5:13 pm

Hi Dan,

this is also possible in ONE file, but this also requires a lengthy reply and I do not have the time right now.
I hope to be able to post a solution tomorrow, maybe someone else chimes in in the meantime...


Best

Klaus

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: Saving Information

Post by bsouthuk » Thu Apr 15, 2010 5:15 pm

Ok Klaus it is so much appreciated.

Thanks for your help

Dan

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Saving Information

Post by Klaus » Fri Apr 16, 2010 12:59 pm

Hi Dan,

OK, here we go, I will show you an example that I would use to save a lot of user input.
The trick is to define a "file-format" that can be easily written and read in again.

Since you need to save contents of fields as well as states/hilites of button from different cards
we also need to store this info in the file.

I would use this "format" (TAB delimited) in the file, one complete info per line:
F or B (button or field) TAB Name of button or field TAB card number or name the object is on TAB the state of button or the URLENCODED content of a field.
When you urlencode the (multiline) content of a field, the result is ONE line of text! Ideal to save in one line a shown here.

So a routine to read in the user info might look like this:

Code: Select all

## the_data conatins the content of the prefs file previously written
command fill_in_user_prefs the_data
 set itemdel to TAB
 lock screen
 repeat for each line k in the_data
   put item 1 of k into tKind
   put item 2 of k into tName
   put item 3 of k into tCard
   put item 4 of k into tContent
  
   ## I use a switch statement here, since this is much better readable than a lot of if..then.. statements
   ## and thus easier to expand later if necessary!
   switch tKind
   case "b" ## Button
     set the hilite of btn tName of cd tCard to tContent
     ## Also possible for LABELS of option buttons etc...
   break
   case "f" ## Field
     put urldecode(tContent) into fld tName of cd tCard
   break
  end switch
  unlock screen
end fill_in_user_prefs
This will work for ONE or THOUSANDS of lines and is fast enough! :)

To gather this info before writing it to file might look like this:

Code: Select all

command gather_user_info
  put "b" & TAB & "name of button here" & TAB & number/name of card here & TAB & the hilite of btn "xyz" of cd z into tData
  put CR & "f" & TAB & "name of field here" & TAB & number/name of card here & TAB & urlencode(fld "xyz" of cd z) after tData
  ### etc...
  put tData into url("file:" & ("path to prefs file here")
end gather_user_info
I think you get the idea :-)


Best

Klaus

Post Reply