[comp.sys.mac.hypercard] Question about the startUp message

mcgee@nuacc.acns.nwu.edu (Randy McGee) (12/08/88)

I've been playing around with a startUp handler lately and am having some 
problems.  One of the things I'm trying to do in this stack is ensure that 
the userLevel is set to 3.  Since the normal hierarchy would execute my stack 
startUp handler first (assuming you open the stack from the finder of course) 
and then the Home stack handler, the userlevel is "set" according to the 
preference card.

I thought of two ways around this.  One was simply to copy the Home stack 
startUp script into my startUp script.  This works, but I'd rather not do 
this since it may change in the future and would be a maintenance problem.

The second was simply to send the startUp message to the Home stack.  The 
problem here is that when I do this, HyperCard goes into a loop until it stop 
with a recursion limit error.  After playing around for a while and putting 
some debugging puts into my handler, I've determined that sending the startUp 
message to the Home stack somehow ends up calling my stack handler again!  
This is contrary to the normal progression of messages through the 
hierarchy.

Is this a bug or just a screwy side effect of how startUp messages work.  If 
it's not a bug, does anyone have a GOOD work around suggestion?  I've found 
two work arounds, but don't like either.  One is to copy the Home stack 
script as described above.  The other is to use a global variable to hold a 
boolean as to weather my startUp handler has executed yet to conditionaly 
send the startup message to the Home stack.

Thanks for any and all suggestions.


P.S.   I've tried this on both version 1.1 and 1.2 of HyperCard.

Randy McGee (MAGOO) <mcgee@nuacc.acns.nwu.edu>     Phone:    (312) 491 4079
Academic Computing and Network Services            USPost:   2129 Sheridan Road
Northwestern University                                      Evanston, IL  60208

englandr@phoenix.Princeton.EDU (Scott Louis Englander) (12/12/88)

In article <406@accuvax.nwu.edu.NWU.EDU> mcgee@nuacc.acns.nwu.edu (Randy McGee) writes:
>I've been playing around with a startUp handler lately and am having some 
>problems.  One of the things I'm trying to do in this stack is ensure that 
>the userLevel is set to 3.  Since the normal hierarchy would execute my stack 
>startUp handler first (assuming you open the stack from the finder of course) 
>and then the Home stack handler, the userlevel is "set" according to the 
>preference card.
>
After many hours of trying to do this exact thing, i've come up
with a way, but it's a still a pain in the butt.  If Apple is at
all considerate of folks like us, they'll incorporate a command in
future versions, like "set local userlevel to" which only affects
the current stack.  The following works, but it seems to me i had 
some trouble in older versions of HC, because openstack was not sent
when launched from the finder.  I may be wrong about this, though.

Here it is:

-- stack script:
on initlevel
  global savelevel, initflag
  if the version < 1.2 then
    answer "Please upgrade to HyperCard version 1.2 or greater" B
    with "Cancel"
    go home
  else
    put true into initflag
    hide msg
    set the cursor to 4 -- watch
    lock screen
    set lockmessages to true
    if the userlevel is not 2 then
      push recent card
      push this card
      go to card "User Preferences" of stack "Home"
      put card field "User Level" into savelevel
      -- closestack restores userlevel
      setUserLevel 2 -- typing only
      go to first card
      pop card
    else
      set userlevel to 2
      put 2 into savelevel
    end if
    go to first card
    --
    -- other initialization stuff goes here
    --
    set lockmessages to false
  end if
end initlevel

on openstack
  global initflag
  if initflag is not true then initlevel
end openstack

on startup
  --sent after openstack when launched from the finder
  global finderlaunch
  put true into finderlaunch
  -- don't pass startup, as home stack will intercept and
  -- reset userlevel
end startup

on closestack
  global savelevel, initflag, finderlaunch
  put empty into initflag
  lock screen
  set lockmessages to true
  set cursor to 4 -- watch
  set userlevel to 5
  domenu "compact stack"
  set userlevel to 2
  if finderlaunch is not true then  -- restore userlevel
    -- we don't really know what home userlevel was if we launched from
    -- finder, as we didn't need to go there to save it
    push this card
    go to card "User Preferences" of stack "Home"
    setUserLevel savelevel
    pop card
  end if
  put empty into initflag
  put empty into finderlaunch
  put empty into savelevel
  set lockmessages to false
  unlock screen
end closestack
--------------------------------------------------------------------------

Hope this helps.


-- 

                                               - Scott

aisl@uhura.cc.rochester.edu (Lawrence Landry) (12/15/88)

In article <406@accuvax.nwu.edu.NWU.EDU> mcgee@nuacc.acns.nwu.edu (Randy McGee) writes:
>The second was simply to send the startUp message to the Home stack.  The 
>problem here is that when I do this, HyperCard goes into a loop until it stop 
>with a recursion limit error.  After playing around for a while and putting 
>some debugging puts into my handler, I've determined that sending the startUp 
>message to the Home stack somehow ends up calling my stack handler again!  
>This is contrary to the normal progression of messages through the 
>hierarchy.
>

This is a hack to fix what I view as a bug,  but it works.

The key is to keep track of whether you have been executed yourself.  Try
the following script.  It should work.

on startUp
   global startup_already

   if startup_already is empty then
      put "busy" into startup_already
      startup
      put empty into startup_already

      set the userLevel to 3
      -- other commands you need to execute
   else
      pass startUp
   end if
end startUp

Larry Landry
University of Rochester