9 Replies Latest reply on Nov 4, 2016 12:05 PM by xenophon61

    Run a script when switching from one window to another.

    KylePutzier

      Is it possible to run a script when the user switched from one window to another by simply clicking on it?

      My program has a navigation routine that normally handles window switching, but it is possible to switch to a background window by just clicking on it. When this is done, it leaves my navigation routine in an obsolete state. It's not catastrophic, just annoying.

       

      Kyle

        • 1. Re: Run a script when switching from one window to another.
          Mike_Mitchell

          Can you use modal windows to prevent it?

           

          Mike

          • 2. Re: Run a script when switching from one window to another.
            KylePutzier

            I can have many (4...6) windows open at one time and I'm trying to stay away from model windows. It's very convenient to easily switch from one to another whenever you want.

             

            I need a OnWindowActive script trigger.

             

            Another way I could solve my problem is when I click on my navigation pulldown field, it would run a script before it becomes the active object. OnObjectEnter triggers after it becomes the active object.

             

            Kyle

            • 3. Re: Run a script when switching from one window to another.
              Mike_Mitchell

              Kyle -

               

              This has sort of been discussed before (see https://fmdev.filemaker.com/message/67904#67904), but it's not an easy conundrum.

               

              My suggestion would be to capture the current window in a variable whenever you initiate any scripted operation, compare it with where you last were (also stored in a variable), and then take the necessary action.

               

              But you're right; if you could fire a trigger when a window becomes active, it would be easier.  

               

              Mike

              • 4. Re: Run a script when switching from one window to another.
                steve_ssh

                Hello Kyle and Mike,

                 

                Are plugins fair game?

                 

                I'm not sure whether this would be of help to you, but :

                 

                Given the context of a single FileMaker file with multiple windows open:

                 

                It seems as though it might be easy enough to fire off a FM script on window change if you are willing to use the (free) ScriptMaster plugin from 360Works.

                 

                http://www.360works.com/scriptmaster/

                 

                ScriptMaster has functionality whereby a FileMaker script can be invoked from within a FileMaker calculation (when the calculation is evaluated).

                 

                 

                The idea would be:

                 

                Define a Custom Menu Set and install it as file default.  The Custom Menu Set would include a Custom Menu which utilizes a calcuation to determine whether or not the Menu should be loaded.  Such a calculation could follow Mike's suggestion of checking the current window name against a global variable, and if a change is detected, it could call the ScriptMaster plugin, which would in turn invoke a predefined "OnWindowChange" handler script.  When a window change occurs, presumably FM would evaluate said calculation in order to properly load the Menu Set associated with the new window.

                 

                I've tried this out for kicks -- given just a cursory look, it seems to work OK.

                 

                I'll attach my sample/test file in case it is of interest.

                 

                Kind regards,

                 

                -steve

                 

                 


                 

                Message was edited by: steve_ssh:  Removing archive -- Please see subsequent post for an example which handles both single and multi-file scenarios. [Edited 17Aug2012]

                • 5. Re: Run a script when switching from one window to another.
                  KylePutzier

                  Steve,

                  Your example works well. However, I need to have it trigger  not only when switching from within a single FM file, but across multiple FM files. The global variable ($$varCurrentWinName) limits that. To get around it, I replaced the global variable with a global field and referenced that. I then update the global field in the script that gets fired. The Menu calc then checks against the global field to see if the window has changed. It works as expected, except the script occasionally fires multiple times. I'm guessing that the global field is not fast enough. It doesnt always fire twice. Mainly just when switching from one file to the other. Need a super global var.

                  If someone smarter than I could experiment around with this, I would be grateful.

                   

                  Kyle

                  • 6. Re: Run a script when switching from one window to another.
                    steve_ssh

                    Hello Kyle,

                     

                    I had some fun playing around with the multi-file scenario, and, in doing so, learned a little bit more about some of the timing issues going on with this.

                     

                    The short answer to your last reply:

                     

                      I think that you will find success if you move the triggering calculation from the property that enables the custom menu to the property that determines the title of the custom menu.

                     

                     

                    In case others would like to see this and perhaps play around with it:

                     

                      I'm attaching a new archive that illustrates this idea, both in a single-file version, as well as a multiple file version.  More info follows...

                     

                    Hope this helps & best (and thanks!  This was fun to try to figure out.)

                     

                    -steve

                     

                     

                     

                    Synopsis:

                     

                    Triggering a script when user switches windows, utilizing 360Works ScriptMaster Plugin.

                    Trigger up to two scripts when the focused window changes, e.g. when a window is closed, a background window is moved to the foreground, a new window is opened, etc..

                     

                     

                    Requirements:

                     

                    - ScriptMaster Plugin installed and enabled  [ http://www.360works.com/scriptmaster/  ]

                     

                    - FMP Advanced [ To install Custom Functions and Custom Menus ]

                     

                     

                    Underlying Concept:

                     

                    A custom menu set is employed by each file that needs to utilize this technique.  The custom menu set includes a special custom menu which defines a calculation for the menu's 'title' attribute.  This calculation is evaluated whenever a window focus change occurs, thus giving us the hook to detect a window focus change.  The ScriptMaster plugin gives us the wherewithal to respond to such detection, by giving us the functionality to trigger a script during the evaluation of a calculation by FM's calculation engine.  The implementation details are encapsulated into two custom functions, and two simple scripts.

                     

                     

                    Files In The Archive:

                     

                    There are two groups of files to demonstrate this technique:

                     

                      1) Files:  'WindowChange_01'  through:  'WindowChange_04' work together to demonstrate using this technique to monitor windows which are spread across multiple FM files

                     

                      2) File: 'WindowChangeSingle_01' is a standalone file which demonstrates using this technique for the case where all windows belong to a single FM file

                     

                      Sample files are provided in both fp7 and fmp12 formats.

                     

                     

                    Single File Use Configuration:

                     

                      - Applicable when all windows belong to the same FM file.

                     

                      - Triggers a single script when a change in window focus occurs   [ Window_Focus ]

                     

                      - The script is passed a script parameter which populates local script variables which specify:

                     

                            - The name of the window which lost focus   [ $windowBlur ]

                            - The name of the window which received focus   [ $windowFocus ]

                     

                      - Requires adding the following to the file:

                     

                            - Two Custom Functions

                            - A short script

                            - A Custom Menu Set, including Custom Menus

                     

                      - Does not require any schema modification

                     

                     

                     

                    Multiple File Use Configuration:

                     

                      - Applicable when windows may belong to more than just a single FM file.

                     

                      - When a change in window focus occurs:

                     

                            - First: Triggers a "blur handler" script in the file whose window lost focus   [ Window_Blur ]

                            - Second: Triggers a "focus handler" script in the file whose window gained focus   [ Window_Focus ]

                     

                      - Both of the above scripts are passed a script parameter which populates local script variables which specify:

                     

                            - The name of the file containing the window which lost focus   [ $fileBlur ]

                            - The name of the window which lost focus   [ $windowBlur ]

                            - The name of the file containing the window which received focus   [ $fileFocus ]

                            - The name of the window which received focus   [ $windowFocus ]

                     

                      - Requires adding the following to each file:

                     

                            - Two Custom Functions

                            - Two short scripts

                            - A Custom Menu Set, including Custom Menus

                     

                      - Requires additional schema

                     

                            - One file must be designated to store two global field values [ a single global field with 2 repetitions works fine ]

                            - All other files must have the necessary Data Source and Table Occurrences to access these added global fields

                            - Additionally, an opening and/or closing script should be employed to clear these global values before the start of a user session

                    1 of 1 people found this helpful
                    • 7. Re: Run a script when switching from one window to another.
                      KylePutzier

                      Steve,

                      Thank you for putting in the effort to make your solution multi-file capable. My navigation routine now behaves as I was hoping it would.

                      Also, with your Blur script, I have the added benefit of being able to commit the record when a user switched windows without using my nav routine.

                       

                      Kyle

                      • 8. Re: Run a script when switching from one window to another.
                        steve_ssh

                        Hi Kyle,

                         

                        Glad it helped.  Thank you for letting me know how it turned out.

                         

                        Very best,

                         

                        -steve

                        • 9. Re: Run a script when switching from one window to another.
                          xenophon61

                          Subscribed to the forum, just to say thanks to steve_ssh. Thanks!

                           

                          With greetings from Athens,

                           

                          Xen