Page 1 of 1

Before preOpenStack [Solved]

Posted: Thu May 14, 2015 11:58 am
by atout66
Is there a way to use an handler before the preOpenStack, even when the code is in the main stack ?
I need to test if a file is on the operating system only once when the appli loads.
If not, I set the Preferences by default, otherwise I read them from the file "preferences.txt".

I don't want, each time I change from one stack to an other this test to occur.

Let me know if it's not clear enought...

Cheers, Jean-Paul.

Re: Before preOpenStack

Posted: Thu May 14, 2015 12:06 pm
by Dixie
Have a look at 'startUp' in the dictionary

Re: Before preOpenStack

Posted: Thu May 14, 2015 1:20 pm
by atout66
Exactly what I was looking for...
Thanks a lot!

Re: Before preOpenStack

Posted: Thu May 14, 2015 1:45 pm
by atout66
Argh ! I talk to fast...
This startup handler does not create folder nor files to store user's data.

I suppose I must put this handler into the mainstack, right ?
Cheers, Jean-Paul.

Re: Before preOpenStack

Posted: Thu May 14, 2015 1:57 pm
by atout66
I try an answer message just after the <on startup> to see when appear this startup handler but nothing happens...

Code: Select all

answer "This is the startup handler running !"
I also tried with breakpoints in the debugger without success.

Any idea will be much appreciated ;-)

Regards, Jean-Paul.

Re: Before preOpenStack

Posted: Thu May 14, 2015 2:42 pm
by Dixie
Mmm... something like this perhaps ?

Code: Select all

on openStack
   if environment() = "mobile" then
      put specialfolderpath("documents") & "/mystartpreferences" into thefile
      
      if there is not a file thefile then
        -- set all preferences to default
      else
         put URL ("file:" & thefile) into thelastPlace
         -- set the preferences to the altered state
      end if
end openStack

Re: Before preOpenStack

Posted: Thu May 14, 2015 3:35 pm
by atout66
It sounds nice Dixie but the handler is in an <openStack> of the mainStack.
So each time I navigate from one stack to an other, the handlers <preOpenStack> and <openStack> from the mainStack run and they would read the preferences.

For example, let say I have a tool palette, which is an independant stack (subStack) opened inside the mainStack.
If I close the tool palette, the message <preOpenStack> and <openStack> goes to the mainStack, even if it is already open (for sure it is, it's the main one!), because it gets the focus I suppose...

That's why I can't put the load preferences into a <preOpenStack> or <openStack> handler.
I'm going to think about a bypass or something like that; still searching the forum.

Thanks for your help, Jean-Paul.

Re: Before preOpenStack

Posted: Thu May 14, 2015 3:47 pm
by atout66
Could someone try this to test ?

Code: Select all

on startup
       answer "This is the startup handler running !"
end startup
Placing in the code of the mainStack.

Please, let me know, for me, no message at all...

Re: Before preOpenStack

Posted: Thu May 14, 2015 4:00 pm
by Dixie
Using a global ( a custom property would probably be better) you can set a flag to check what is the state of the preferences set up... in this case if the global 'prefsFlag' is empty then you would know that the stack is being opened for the first time in the session... that being so the 'prefsSetUp' handler would run..

When 'prefsSetUp' is run then say 1 is placed into the global prefsFlag... the next time the stack is opened it knows whether to load prefs or not... when you close the stack, because you are quitting your application then you can save your preferences whether they have been altered or not

Code: Select all

global prefsFlag

on openStack
   if environment() = "mobile" then
      if prefsFlag is empty then
         prefsSetUp
      end if
   end if
end openStack

on openCard
   
end openCard

on prefsSetUp
   put 1 into prefsFlag
   put specialfolderpath("documents") & "/mystartpreferences" into thefile
   if there is not a file thefile then
      -- set your preferences to default
   else
      -- extract your prefs from the file and set things up
      put URL ("file:" & thefile) into thelastPlace
   end if
end prefsSetUp

on closeStack
   put specialfolderpath("documents") & "/mystartpreferences" into thefile
      -- so for example
   put the short id of this card into URL ("file:" & thefile)
end closeStack

Re: Before preOpenStack

Posted: Thu May 14, 2015 4:26 pm
by jacque
Your stack will not get a startup message in the IDE, LiveCode gets it first before it loads your stack. You will only get the message in a standalone.

The most common approach is to put the commands into a preOpenStack handler and put that handler into the script of the first card of the mainstack. LiveCode always opens that card first on startup so it will get the message. But the card script is not in the message path for other stacks, so they won't respond to it.

This way you will be able to read preferences in both the IDE and in the standalone.

Re: Before preOpenStack

Posted: Thu May 14, 2015 5:16 pm
by atout66
Thanks to all of you for your help ;-)
@ Dixie I agree with you. I was thinking about something like that when I talk about bypass. Could be the simpliest way probably.

@ Jacqueline I like your approach and think I'm going to test it too.

I will compare both of them and let you know.

Regards, Jean-Paul.

Re: Before preOpenStack

Posted: Thu May 14, 2015 6:43 pm
by atout66
OK, both methods work fine :-)
The Jacqueline's one avoid the use of a global var.
So now, I've got two ways to get the same thing.;-)
Thanks to all of you for your help, Jean-Paul.