sharing records between handlers in a script

Discussion in 'Mac Programming' started by ShuddaRunThat, Apr 2, 2014.

  1. ShuddaRunThat macrumors newbie

    Joined:
    Apr 1, 2014
    #1
    Hi - I have a large script that within which I have a number of handlers. I also have a large set of records. I cannot get the handlers to use the contents of the records so my current workaround is to remove the handlers and drop their code into the relevant points in the script to make this script effectively one large RUN handler. I understand a bit about set, global and property - but not sure how to apply this scope setting to a record list. is there a way to make a set of records scope global across all handlers in the script?

    thanks
     
  2. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    Perhaps not the answer you are looking for but here's an example from the ASLR_about_handlers

    Code:
    on hello(a, b, {length:l, bounds:{x, y, w, h}, name:n})
        set q to a + b
     
        set response to "Hello " & n & ", you  are " & l & ¬
            " inches tall and occupy position (" & x &  ", " & y & ")."
     
        display dialog response
     
    end hello
     
    set thing to {bounds:{1, 2, 4, 5}, name:"George", length:72}
    hello (2, 3, thing)
    --result: A dialog displaying "Hello George, you are 72 inches  tall
    --          and occupy position (1,2)."
     
  3. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #3
    A property or global variable would allow the record to be used, well, globally. In order for anyone to help diagnose the problem, you will need to provide more specific information about the conditions where you are not able to access the record.
     
  4. ShuddaRunThat, Apr 2, 2014
    Last edited by a moderator: Apr 2, 2014

    ShuddaRunThat thread starter macrumors newbie

    Joined:
    Apr 1, 2014
    #4
    more specific example

    this shows an example of the problem...

    Code:
    set people to {{name:"Fred", Mood:"Happy"}, {name:"Jim", Mood:"Sad"}, {name:"Gary", Mood:"Happy"}}
    
    on changeMood()
    	
    	set whoseHappy to {}
    	
    	repeat with theRecord in people
    		if Mood of theRecord = "Happy" then
    			set end of whoseHappy to name of theRecord
    		end if
    	end repeat
    	
    	set pickMoodChange to (choose from list whoseHappy default items "" with prompt "who is not happy?") as string
    	
    end changeMood
    
    changeMood()
    
    
    run it and you will see the problem. basically code inside the handler does not know that the records exists - if I take the code out of the handler and run it, it works fine.
     
  5. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #5
    Your record is not set as a global - in this case it is essentially a local variable of the (implied) run handler. You will need to use something like:

    Code:
    global people
    set people to {{name:"Fred", Mood:"Happy"}, {name:"Jim", Mood:"Sad"}, {name:"Gary", Mood:"Happy"}}
    
         -- or --
    
    property people : {{name:"Fred", Mood:"Happy"}, {name:"Jim", Mood:"Sad"}, {name:"Gary", Mood:"Happy"}}
     
  6. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    Couldn't you pass "people" as a parameter to changeMood(), which I believe was the point of a previous poster.
     
  7. ShuddaRunThat thread starter macrumors newbie

    Joined:
    Apr 1, 2014
    #7
    many thanks red menace i figured it was to do with the records not being set globally but was missing the : out of the property command - many thanks
     

Share This Page