8 Replies Latest reply on Mar 21, 2017 12:20 PM by monkeybreadsoftware

    Launching a script based on a variable.

    user28271

      I want to write a script that launches another script based on a set variable.

       

      I wrote a long script with nested if statements: If $name = name perform this script, but I would prefer a more elegant approach: Perform Script = $variable.  This will allow me to add infinite script option with a single line.

       

      Any thought?

       

      If you want to know more about why I will be happy to share.  So far it is working great, my clients love it, but they keep adding more options and the nested script if becoming a pain to maintain.

        • 1. Re: Launching a script based on a variable.
          Jason Wood

          FileMaker does not have a "Perform Script By Name" script step but I believe you could do this using "Open URL" and the fmp:// url protocol.

           

          I suspect there may be a better way to handle this rather than adding a new script for every new "option". If you describe your solution and goals in more detail, we may be able to suggest a different approach.

          • 2. Re: Launching a script based on a variable.
            user19752

            Open URL [ "fmp://$/" & Get(FileName) & "?script=" & $name ]

             

            This can fail on some cases.

            • 3. Re: Launching a script based on a variable.
              PeterWindle

              I found another way to "sort of" do this... setup a layout that has multiple objects on it, each object has an on object enter script trigger - your calling script could just simply goto layout, goto object (name by calc) and when the object is entered, it would then run the script. Not elegant, but I find it comes in handy also when you want to run scripts separately - put them on this layout and you can click on them when you wish...

              not elegant, I know, but it's more of a utility than anything else.

              • 4. Re: Launching a script based on a variable.
                alecgregory

                As people have suggested, there are ways to dynamically call a script by name, but it's not really supported. The two purely dynamic ways to do it currently are using the fmp url scheme which isn't totally reliable and via the PHP web-publishing API, which is often not available (i.e. if you're not using FileMaker Server) and a fair bit of hassle to set up just for this purpose.

                 

                And since the advent of script triggers we've been able to do a few more things.

                 

                The other approach is the one you are already using. It's not fully dynamic as you have to add each script as you make it, but it's generally more reliable than the dynamic methods.

                 

                In any case, I tend to find that being able to call any script by name is much better in theory than in practice. FileMaker only really gives us one tool to handle the metadata of solutions: the Database Design Report (DDR). It's much harder to work with a DDR that uses one big handler script or dynamic script calling. In the case of dynamic script calling most currently available DDR analysis tools will not register calls to scripts made using the fmp url scheme so are unlikely to be able to report where each script is used. In the case of a large handler script you tend to end up with hundreds or thousands of calls to the handler script and no easy way to work out how many times to subsequent scripts are called.

                 

                Given that it's usually larger systems that "benefit" from handler scripts or calling scripts dynamically by name this can lead to serious code maintenance issues.

                1 of 1 people found this helpful
                • 5. Re: Launching a script based on a variable.
                  user28271

                  Thanks everyone for the options.

                   

                  I kinda like the Object option from Peter.

                   

                  I can have one master layout with objects that then trigger scripts.

                   

                  Yes, I know, this sounds more complicate to maintain then my current scripting issue. 

                   

                  BUT....

                   

                  It will allow me to give maintenance to the InHouse IT persons without giving him access to edit scripts.  I can give him layout access to ONE layout and teach him about Object and Script Triggers.   This way he can change which scripts are available.

                  • 6. Re: Launching a script based on a variable.
                    alecgregory

                    If you've got a solid use case then there's no reason not to try it out. As long as you understand the maintenance implications and document the technique then I would go for it.

                    • 7. Re: Launching a script based on a variable.
                      rgordon

                      Through the FileMaker Security you can control which Scripts a user can edit.  Seems like a lot less work to allow them to edit just one script versus creating a new object and assigning whatever script they want to an OnEnter trigger.  You also have to leave the current layout to go to another layout which might cause your script to lose focus.

                      • 8. Re: Launching a script based on a variable.
                        monkeybreadsoftware

                        If you use MBS Plugin, you can use the FM.RunScript function.

                        Or the schedule functions here:

                        Monkeybread Software - MBS FileMaker Plugin: Component: Schedule