defaultStack and front scripts

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: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

defaultStack and front scripts

Post by phaworth » Fri Apr 16, 2010 12:38 am

My .rev file contains a script that is inserted as a front script at startup. I'm now wanting to break out the stack/card that contains that script into a separate .rev file so I can us it from any other Rev applications.

I split them out to a new .rev file and built a test app that does the insert command to put the scripts into it as front scripts - all seemed to work fine.

The first handler I want to call from the front script checks to see if a certain stack exists in the test .rev file and if not, creates it and adds one card to it. This is done with an "if exists(stack "xyz")" test. What I'm finding is that the check is looking for the stack in the .rev file that holds my code library and creating the stack in that .rev file instead of in the .rev file for my test app. Also when a create he card (with the create card command), it goes into the stack that holds the card with the code library handlers in it.

I've tried setting the defaultStack property but according to the dictionary, that holds what looks like the abbrev name of a stack so it still seems to look in the code library.rev file.

How can I make the exists test look in the test .rev file, not the code library .rev file, and create the stack/card in my app file rather than the code library .rev file?

Also, when I start to distribute this thing as a standalone app, will I have to distribute the .rev file for the code library as well as the .rev file for the app, or is there some way to handle that during the build of the standalone app.



Thanks,
Pete

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

Re: defaultStack and front scripts

Post by Klaus » Fri Apr 16, 2010 1:09 pm

Hi Pete,

I am not sure if I understand exactly what you are doing here :), but does this eventually work for you?

Code: Select all

...
if there is NOT a stack "Name of new stack here" of stack "Name of stack that is NOT your library stack" THEN
  create stack "Name of new stack here"
  set the defaultstack to "Name of new stack here"
  create card

  ## NOW make this a new substack of any stack you want:
  set the mainstack of stack "Name of new stack here" to "Name of stack that is NOT your library stack"
end if
...
Best

Klaus

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: defaultStack and front scripts

Post by phaworth » Fri Apr 16, 2010 6:55 pm

Thanks Klaus and sorry if I wasn't clear on what I am trying to do. I think you understood but I'll try to state it again more simply.

Let's say my code library file is named library.rev and all the code is in the card script of a stack named "Code Library". My application file is name myApp.rev and has 1 main stack named myMainStack and several substacks. The preOpenCard handler for the only card in myMainStack uses the insert command to set the code in the library.rev file as a front script. Right after that, it calls one of the handlers in the code library which contains the code in question.

The purpose of this code is to gather information about the structure of the SQLite database being used in any app I write. I should end up with a stack named Structures in myApp.rev with one card in it for each table in the SQLite database. There should be 2 fields on each card, one holding a list of the column names and types in the table and the other holding the name of the primary key field field for the table.

As an aside, figuring out the problem is being made doubly difficult because debug does not seem to observe the break points I set in the code library handler so I have had to use answer information as you'll see below.

I've used basically the code you supplied with a few changes as follows.

Code: Select all

answer information "Checking for stack Structures in" && the long name of this stack
   if there is NOT a stack "Structures" of the long name of this stack then
      answer information "Creating stack Structures in stack" && the long name of this stack
      create stack "Structures"
      save stack "Structures" as the long name of this stack
      set the defaultstack to "Structures"
      ## NOW make this a new substack of any stack you want:
      set the mainstack of stack "Structures" of the long name of this stack to the mainstack of the long name of this stack
When this code executes, I get the answer information dialog about checking for stack Structures and "the long name of this stack" correctly references myApp.rev but the code to create the Structures stack does not get executed (I never see the answer information dialog) even though there is no stack named Structures in myApp.rev.

Later in the code, I create the card as follows I create several cards in a repeat loop with the variable myTable holding the name of the card.

Code: Select all

if there is not a card myTable of stack "Structures" of the long name of this stack then
         answer information "Creating card"  && myTable 
         create card myTable
         create field "FieldsInTable"
         create field "PrimaryKey"
      end if
answer information "Putting" && myData && "into FieldsInTable of stack Structures of" && the long name of this stack
      put myData into field "FieldsInTable" of card myTable of stack "Structures" of the long name of this stack
      answer information "Putting" && myPrimaryKey && "into PrimaryKey of stack Structures of" && the long name of this stack
      put myPrimaryKey into field "PrimaryKey" of card myTable of stack "Structures" of the long name of this stack
When this code gets executed, I see the answer information dialogs about Checking for the card, creating the card, and putting myData into FieldsInTable. Then I get an execution error "Can't find card" on the line "put myData into field "FieldsInTable"....

After all that, myMainStack of myApp.Rev shows up as a main stack in library.rev and the card named in myTable is part of myMainStack.

Any ideas?

Thanks,
Pete

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

Re: defaultStack and front scripts

Post by Klaus » Sat Apr 17, 2010 11:36 am

Hi Pete,

I confess that I have problems with the second of these lines:
...
create stack "Structures"
save stack "Structures" as the long name of this stack
...

Sounds like this will overwrite the old file and save the new stack as a MAINSTACK in that file!
I would first make it a substack and then save its mainstack.

And I have no idea when the script in your lib get called, could you give me a hint.
And are all these (strange) references "the long name of this stack" really necessary?
Makes the script hard to read and understand. 8)

Hint, after creating a new stack it already has a first card that you could simply rename instead
of creating a new one with the name you want. I am a fan of recycling, you know :D


Best

Klaus

phaworth
Posts: 592
Joined: Thu Jun 11, 2009 9:51 pm

Re: defaultStack and front scripts

Post by phaworth » Sat Apr 17, 2010 6:29 pm

Hi Klaus,
Thanks for continuing to help.

The references to the long name of this stack were my attempt to get the stack/cards saved in the correct .rev file. Without that, the stack/cards were being saved in the code library .rev file. I've since modified the code to only use that construct when I set the mainstack of the created stack like this:

set the mainstack of stack "Structures" to the mainstack of the long name of this stack

That avoids me having to pass in the name of the app stack to the code library handler.

I think a lot of my problems were caused by there being two stacks with the same name, one in the app and another in the code library. Once I fixed that, things began working much more smoothly.

Thanks for the suggestions.

Pete

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

Re: defaultStack and front scripts

Post by Klaus » Sat Apr 17, 2010 7:13 pm

phaworth wrote:I think a lot of my problems were caused by there being two stacks with the same name, one in the app and another in the code library...
Ah, yes, that means tempting fate! :D

Post Reply