Custom Property to store data in a standalone ap

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

urbaud
Posts: 112
Joined: Tue Feb 24, 2009 12:10 am
Location: Victoria, BC, Canada

Custom Property to store data in a standalone ap

Post by urbaud » Thu Mar 12, 2015 10:55 pm

I’m not sure of this, so hence the reason for this post. I think I remember reading somewhere that using a Custom Property (CP) to store data (text) in a standalone app won’t work because LiveCode won’t let the data be changed, so adding or deleting data from the CP in the standalone won’t happen. Am I correct about this? If so, the only way then to save data that can be changed would be to use an external file, like a text file-right?

urbaud
urbaud

Klaus
Posts: 11009
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Custom Property to store data in a standalone ap

Post by Klaus » Thu Mar 12, 2015 11:14 pm

Exactly! :D

Remember that you can also create a (invisible) stack on the fly and store your stuff in that stack
and later save that stack in the users docs folder or wherever!

urbaud
Posts: 112
Joined: Tue Feb 24, 2009 12:10 am
Location: Victoria, BC, Canada

Re: Custom Property to store data in a standalone ap

Post by urbaud » Sat Mar 14, 2015 7:58 pm

Hi Klaus,

Thanks for replying to my post. Unfortunately, I don’t know how to create an invisible stack on the fly. I wouldn’t know how to code it or even how to get started coding it. Do you mean that the user could create an invisible stack while the standalone app is running? If that’s what you mean, how would you do that?

Also, I don’t know what you mean by: “…later save that stack in the users docs folder or wherever!” Where is the users docs folder? How would I do that?

Maybe you shouldn’t have replied to my post, ‘cause all I’m doing is asking you more questions-probably not something you expected. Anyway, I do want to thank you again for responding.

Dan
urbaud

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6026
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Custom Property to store data in a standalone ap

Post by dunbarx » Sat Mar 14, 2015 8:27 pm

Hi.

What everyone means is that any standalone that you create becomes the executable on whatever platform you save it to. Nothing can be saved in that file. The OS will lock it.

There are many threads dealing with this. I think the easiest way is to use the "splash" stack method, whereby your "working" stack(s) files are added to the standalone, and opened as needed. Most often they are the only ones visible, with the actual standalone being just a vehicle to open them. You get used to this quickly; it is not so bad.

Try this:

http://forums.livecode.com/viewtopic.ph ... sh#p120667

or search for the word "splash" in this forum. Write back if you get stuck.

Craig Newman

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Custom Property to store data in a standalone ap

Post by magice » Sat Mar 14, 2015 10:24 pm

Here is a little trick to show how stacks that are not saved as stand alone apps can still act like it is one. Create a stack with 1 button. In that button script put these 2 lines of code :

Code: Select all

on mouseUp
   answer file "navigate to any uncompiled stack file"
   open it
end mouseUp

Now save it as a stand alone. With it you can open any stack without Livecode. I actually keep this small app on my desktop for testing stacks when doing so in LiveCode will corrupt the stack. (fill in text fields, image files etc. that you want blank in the standalone)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6026
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Custom Property to store data in a standalone ap

Post by dunbarx » Sat Mar 14, 2015 10:32 pm

Magice,

But then the stack you open has to run in the IDE, that is with LC resident on the machine, no? It is not really a standalone.

Craig

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Custom Property to store data in a standalone ap

Post by magice » Sat Mar 14, 2015 10:36 pm

dunbarx wrote:Magice,

But then the stack you open has to run in the IDE, that is with LC resident on the machine, no? It is not really a standalone.

Craig
It is essentially the same as a splash screen. It will run on any machine even one without LC. (provided you have all necessary external files)

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 279
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Custom Property to store data in a standalone ap

Post by Newbie4 » Sun Mar 15, 2015 3:31 am

Urbaud,
In answer to your original question, the simplest way to save data is by writing it out to an external text file but it is not the only way. You can save data as custom properties inside LiveCode, but it has to be saved as an "external" stack in another folder/directory.

I will try to explain it in simple terms as I understand it.

Programs (like LiveCode) are normally installed into a directory that has restricted permissions (Apple==>"Applications", PC==>"Program Files"). You need "Admin" rights to install it and to change it. Users can only execute programs that are in it. They can not write or save files in that directory. Even LiveCode, itself, can not make changes and save itself back to that directory. It is a very controlled, restrictive environment for security reasons. It prevents unauthorized users, programs and even malware from maliciously modifying files, renaming or replacing programs with bad copies or introducing viruses into the system.

This leaves you with 2 choices:
1. Write out your data to a file in your "Documents" or similar directory where you are allowed full access. Then each time you start up, your program reads that file in and resets all the variables.
2. Save your data in custom properties in a different stack and write that stack out into your "Documents" directory. (You are allowed to write and modify files there). This choice takes more programming but is cleaner and has more advantages

Here is how you do it. You structure your program to start up with a "dummy" (or "splash" stack) which then automatically transfers control to your external stack that is in your "Documents" directory. That stack in your "Documents" directory is really the main part of your program. It contains all your code. Since it is in your "Documents" directory, it can do anything that it wants. It can change properties, create new (custom) properties and more. Before you exit, you must remember to close and save that stack with all its changes.

Your program then consists of 2 files: the "Splash" stack and the "Main" stack. The Splash stack is installed into your programs directory, the second or Main stack is placed in your "Documents" directory (or just written out there the first time you run it). You can not modify or rewrite out the Splash stack but you can do anything that you want with the Main stack. In this way you get around the OS's restrictions for not being allowed to change programs.

Thus, each time you start up your program, it has all the latest changes and new data. The data is always in your program and part of the object as it should be.

This allows you even more flexibility. In fact, you can later on use this to update your app automatically without the user having to reinstall the new version. (The Splash stack can check for updates, download a new version of the Main stack and save it before transferring control to it)

Does that make sense?
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Custom Property to store data in a standalone ap

Post by magice » Sun Mar 15, 2015 8:16 am

Newbie4 wrote:
Programs (like LiveCode) are normally installed into a directory that has restricted permissions (Apple==>"Applications", PC==>"Program Files"). You need "Admin" rights to install it and to change it. Users can only execute programs that are in it. They can not write or save files in that directory. Even LiveCode, itself, can not make changes and save itself back to that directory. It is a very controlled, restrictive environment for security reasons.
Actually, with Windows, location is irrelevant. The security is the same in "Program Files" as it is anywhere on the C drive. It is the file types that determine security levels. In the case of executable files, they are just very simply read only, and for security and stability reasons can't be changed. LiveCode executables actually have a little more freedom that others because they are portable executables. This means they can be moved. (notice if you drag any other .exe to a new location it only creates a shortcut, but LCs actually move) The part that can be (and was difficult for me) to get your head around, is that a stack file will run in windows as if it is .exe without the IDE. It has everything it needs except the instruction to run in memory. That is where the splash screen comes in. It gets the .rev's into memory to run. But that is not the only way to save files. There is the text option and another I find even more convenient. Any .rev/.livecode file can be accessed by any stand alone, so you can save your data to the custom properties of any .rev/.livecode file. Just put in the lines

Code: Select all

 set the cMyCustomProperty of stack "mySaveStack" to myData
save stack "mySaveStack"
Some of the advantages to this method over text files are:
1. An entire array can be saved without combining/splitting so maintains its integrity.
2. It is Not as easily opened and viewed and corrupted
3. It is more versatile on the types of data that can be saved
4. It is less work to structure the data for retrieval and the structure is more flexable

Basically if all you need to save is data use this method. If you need to save the status of a stack then use the splash screen method, and save a copy of the stack. (Hint: when you save a copy of the stack with the splash screen method, you don't have to give it the .Livecode extension. It can be anything. Just avoid extensions already in use.)

urbaud
Posts: 112
Joined: Tue Feb 24, 2009 12:10 am
Location: Victoria, BC, Canada

Re: Custom Property to store data in a standalone ap

Post by urbaud » Mon Mar 16, 2015 6:39 pm

Magice and Newbie4,

Where to start? I’m sure I’m going to sound really dense. Most of the info you guys gave me is new, so it might take me some time to completely understand it.

Problem is guys, I don’t know how to save data in a custom property which is then saved as an “external” stack in another folder or directory. I don’t know how to create the “splash” and “main” stacks. And Magice, I don’t know where to use the two lines of code you gave me:
set the cMyCustomProperty of stack "mySaveStack" to myData
save stack “mySaveStack"

Magice, if the two lines of code you gave me saves data into the variable “myData” and then to the custom property “cMyCustomProperty” what is the code to read the data back into myData? How would I use the “Documents” directory with these lines of code.

Dan
urbaud

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6026
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Custom Property to store data in a standalone ap

Post by dunbarx » Mon Mar 16, 2015 7:28 pm

Hi.

You must practice. Make a new stack, put a button and a field on it.

In the button script:

Code: Select all

on mouseUp
put random(99) into fld 1
set the currentContentsProperty of fld 1 to fld 1
answer fld 1 + the  currentContentsProperty of fld 1
end mouseUp
Each time you click the button, the field gets a random number, and the custom property "currentContentsProperty" which I just made up, is set to that number. Then the value of the property is added to the field value, which is always double the field contents, and then displayed.

Any time you like, including after you have manually emptied the field, AND having shut down your computer for a week, you can:

Code: Select all

get the currentContentsProperty of fld 1
or

Code: Select all

put the currentContentsProperty of fld 1 into fld 1
The property survives sessions, and is one of the best tools in LC. Any sort of data can be stored there.

Homework for you. What are the pros and cons of this tool compared to a global variable? Consider the use of either across several handlers in a project.

Craig
Last edited by dunbarx on Mon Mar 16, 2015 10:09 pm, edited 1 time in total.

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Custom Property to store data in a standalone ap

Post by magice » Mon Mar 16, 2015 9:10 pm

urbaud wrote:Magice and Newbie4,

Where to start? I’m sure I’m going to sound really dense. Most of the info you guys gave me is new, so it might take me some time to completely understand it.

Problem is guys, I don’t know how to save data in a custom property which is then saved as an “external” stack in another folder or directory. I don’t know how to create the “splash” and “main” stacks. And Magice, I don’t know where to use the two lines of code you gave me:
set the cMyCustomProperty of stack "mySaveStack" to myData
save stack “mySaveStack"

Magice, if the two lines of code you gave me saves data into the variable “myData” and then to the custom property “cMyCustomProperty” what is the code to read the data back into myData? How would I use the “Documents” directory with these lines of code.

Dan
Craig's example is about as clear as it can be described. Experiment with it. When you figure it out it will be simple. as far as a splash screen, I am uploading an old example stack I made.

1. Unzip the file to your desktop.
2. Save the file "splash.rev" to a stand alone.
3. Put that stand alone app on your desktop.
4. Double click the stand alone.
5. it will launch the stack "MyStack" outside of the IDE as if it were a stand alone.
6. Change the text and hit the save button.
7. Close the stack.
8. Reopen the stack and you will see that the changes were saved
9. Now look at the code in both stacks and see how simple it is.
Attachments
splash example.rar
(2.27 KiB) Downloaded 75 times

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 279
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Custom Property to store data in a standalone ap

Post by Newbie4 » Mon Mar 16, 2015 10:35 pm

Let’s explain this in a different way:

As was covered before, a standalone is an executable file (Windows=.exe, Apple=.app) which is execute only. You can’t change it or write over it. That means that you will not be able to save data in it.

Data can only be saved in an external file other than your program. It can be any kind of file that you want – .TXT, .DAT, ,CSV, etc. Whatever format that you feel comfortable working with. What Craig and Magice are talking about is saving your data in a Stack instead. That stack file also can be saved anywhere and can be rewritten anytime.

Using a stack to save your data has a number of advantages:
1. LiveCode has a number of commands that deal with stacks which makes working with them easier.
2. Unlike other file formats, LiveCode files can also store code. ...it actually stores objects which have properties (data) and behavior (code). That keeps everything together (data and code) which is a basic principle of Object Oriented Languages. Your data is safer that way.
3. Storing data as properties is better than using variables. Variables can be changed by anyone, anytime and problems are hard to debug. Properties belong to the object and can be better controlled and monitored.
4. Since this second stack can also contain code and can be modified, it makes sense to put all your code there. You can update it, fix bugs or change it any way that you want. It is an external file and is writable. If you move all your code there, your original stack becomes a dummy or just a “splash” screen with no code in it. All it does is transfer control to your second stack. Since the original stack is the “exe” (read-only executable) file, it doesn’t matter.

You save your data by making custom properties for them. Like other properties, you do that with the “set” command
- set loc of btn “box” to 50,85
- set the visible of image “ghost” to false
e.g.
- set the myProperty of stack “xyz” to 1,087
- set the myProperty2 of stack “xyz” to the field “newName”
----------------------------------------------------
What you then have is:

1. One stack called “Launcher” and in its stack script, you have:

Code: Select all

On openStack
	Open stack “MainStack”
	Close stack “Launcher”
End openStack
(You create a standalone (executable) file with this stack.)

2. All the rest of your program goes in the second stack called “MainStack”, which you can put anywhere that you want, change whenever you want, name whatever you want and save whatever data there that you want.

Does this put the process in perspective better? Now you can look at their code and make better sense of it
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6026
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Custom Property to store data in a standalone ap

Post by dunbarx » Mon Mar 16, 2015 10:40 pm

@Newbie4:

Newbie5?

Craig

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 279
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Custom Property to store data in a standalone ap

Post by Newbie4 » Mon Mar 16, 2015 10:52 pm

:lol:
(Aren't you counting in the wrong direction?)
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”