8 Replies Latest reply on Dec 6, 2009 5:21 PM by Steve Wright

    Buttons and scripts working together

    scubed_1

      Title

      Buttons and scripts working together

      Post

      I am a newbie with Filemaker. When I originally designed a fairly complex system, I found that my system was filled with lots of scripts - one for each button - instead of using one script that would recognize that a button was pressed and then execute code within the same script, based on the button chosen. I created complex state charts to represent all the possible choices of buttons and code on one or a group of layouts. I wanted a way to keep all the code together instead of trying to remember which script was used for what button and in what state...

       

      To get around this, I came up with a scheme that I would like to share. I am sure that there are other ways to work this problem, but this is what I came up with:

       

      I created a general script, called ButtonID, that is called by every button on the layout. The button passes a script parameter with the unique button name. The 'Current Script' setup option for each button is set to "resume". The ButtonID script is one line. It simply sets a global variable - $$Button - to the passed script parameter. The main script that does the button interpretation executes a 'pause/continue' step and waits for the the resume, at which point it checks the $$Button variable to determing what button was pressed. Using this method, I am able to keep all code for a given layout in the same script, making maintenance, debugging and the addition of new buttons much easier.

       

      This works really well for me. The only drawback is that it is not possible to execute most of the pull down menu options when the layout is waiting for a "resume" from one of the buttons, although the script debugger and data view work fine.

       

      I'm not sure if anyone else has suggested this, but if this is a redundant post, accept my apologies.

       

      If anyone wants more clarification, please let me know. Hope this is helpful.

        • 1. Re: Buttons and scripts working together
          comment_1
             I don't see why the buttons cannot call the "main" script directly - with a different parameter for each button.
          • 2. Re: Buttons and scripts working together
            scubed_1
              

            That would work if you wanted to restart the script for each button and then execute steps conditionally on the script parameter. The problem I have is that my scripts tend to do set up code at the start before putting up the layout that contains the buttons. If I use the ButtonID script as I implemented it, I can pause in the middle of the layout controlling script and return to the pause/resume script step. The ability to resume in the middle of the script makes it easier for me to follow the flow of the code.

             

            I'm guessing that I could have the buttons execute a pause/resume script step and then have the code conditionally execute based on the name of the current object. I think I might have tried this and either it didn't work or it seemed more cumbersome than a one script step script that resumes with a global variable set to the button tag

             

            Then again, I could be missing something really obvious!

             

            • 3. Re: Buttons and scripts working together
              comment_1
                

              I'm afraid I don't get this part:


              scubed wrote:

              my scripts tend to do set up code at the start before putting up the layout that contains the buttons. If I use the ButtonID script as I implemented it, I can pause in the middle of the layout controlling script and return to the pause/resume script step. The ability to resume in the middle of the script makes it easier for me to follow the flow of the code.


               

               

               

               


              • 4. Re: Buttons and scripts working together
                Steve Wright
                  

                I dont get it atall, it sounds like you are over-complicating the entire situation

                 

                Why not just categorise the scripts in sub folders for each layout ?

                I have thousands of scripts and dont have a problem debugging anything with the script debugger.

                Of course, if you only have filemaker pro, I can sort of see where you are coming from but still I would think it would be more difficult to debug 1 script which caters for a lot of different actions.

                 

                From what I can make of the posts above, it sounds to me like your going to outgrow this implementation rather quickly which will then make it hard work to change, since you would need to edit every layout to change the button actions, and re-create a load of scripts.   

                 

                • 5. Re: Buttons and scripts working together
                  scubed_1
                    

                  SW, your sub-folder approach is great. It is a real clever way to do encapsulation of the layout. I like it a lot and will use it for sure.

                   

                  I must be explaining this very badly or it must be a horrific idea...let me try again.


                  In response to SW, am not suggesting that you tear up your existing code apart and use this method. On the other hand, I find that using this technique from the get-go results in layouts and code that are easier to maintain. If I want to add a button to a layout, I simply set it up so that it calls my universal button interpreter script with a descriptive script parameter and with the ‘resume' option. The script handling the layout pauses, waiting for a button to be pressed, and then executes the appropriate script steps based on a global variable which has been assigned the script parameter by the button interpreter script.

                   

                  I don't use this technique for every layout. But I do find it easy in those that have many possible button options.

                   

                  My implementation in action is similar to a ‘show custom dialog' / ‘get(LastMessageChoice)' combo, but the custom dialog is a layout and layouts are not limited to three buttons, one type font, etc. Structurally, the code is driving the layout, rather than the layout driving the code - less object oriented, if you will. It also has the advantage of being modal...just like the 'show custom dialog' box.

                   

                   

                  Here is an example of a script snippet that might be used instead of a ‘show custom dialog'/get(LastMessageChoice) combo. This script and layout could be generalized into a user designed custom dialog box with a little more work, but I think it illustrates what I am doing. The script snippet assumes that there is a layout with some data and buttons that might be similar in content to a custom dialog, with fields and data entry ability, as well. This hypothetical layout has three buttons, let's say. ‘Yes', ‘No' and ‘Cancel'. In the layout editing, each button invokes a call to the common
                  button handler with the ‘resume' option and the name of the button as the script parameter. There is only one code instance of the common button handler script in the entire project.

                   

                  The common button handler script is one line and it just moves the script parameter to a global variable, $$Button, e.g:

                        Set variable [$$Button; get(ScriptParameter)]


                  The rest of the code would look like this:


                       Go to Layout [ <the hypothetical layout>]

                       Set variable[$$Button; ""]  /* clear the button field before starting to make sure the last usage isn't still around */

                       Pause/Resume Script [Indefinitely]

                       If [$$Button = "Yes"]

                             ...do the ‘yes' response code

                       Else if [$$Button = "No"

                             ....do the ‘no'

                       Else if [$$Button = "Cancel"]

                             ...do the ‘cancel' code

                       End if

                   

                  This would be akin to:


                       Show Custom dialog["What do you want to do"; "Choose a button";<put in some data entry fields>]

                       If [ get(LastMessageChoice) = 1]

                            ...

                       Else if [get(LastMessageChoice) = 2]

                            ...

                       Else if [get(LastMessageChoice) = 3]

                            ...

                       End If

                   


                  But the first example has all sorts of flexibility in layout design, number of buttons, etc.

                   

                  Maybe I'm on the wrong track, but it has helped me organize my code and layouts where there is lots of user interaction with lots of choices.






                  • 6. Re: Buttons and scripts working together
                    comment_1
                       How about:

                    Go to Layout [ <the layout with buttons> ]
                    Pause/Resume Script [Indefinitely]
                    #
                    If [ Get(ScriptResult) = "Yes" ]
                    # DO THE "YES" THING
                    Else if [Get(ScriptResult) = "No"
                    # DO THE "NO" THING
                    Else if [Get(ScriptResult) = "Cancel"
                    # DO THE "CANCEL" THING
                    End if



                    And for the script attached to the buttons:

                    Exit Script [ Result: Get(ScriptParameter) ]









                    • 7. Re: Buttons and scripts working together
                      scubed_1
                        

                      Comment,

                       

                      Very cool indeed!!

                       

                      Your implementation is elegant, eliminating the global variable.

                      • 8. Re: Buttons and scripts working together
                        Steve Wright
                          

                        It does indeed make more sense now.

                         

                        With regards to the drop down menus etc, can you not use an 'onObjectModify' trigger