14 Replies Latest reply on Sep 9, 2016 10:40 PM by tays01s

    Set variable from a variable in a sub-script

    tays01s

      I have a script to create a filepath that would be useful in several other scripts.

       

      For the filepath to be used in these scripts, does the variable need to be global? ie. $$path?

       

      And is it OK to set a variable by another variable (eg. where it's better to re-name the sub-script variable within the main script?

        • 1. Re: Set variable from a variable in a sub-script
          mikebeargie

          local variables are local to the current executing script only, they are not inherited by operating subscripts.

           

          What I would do is use a utility script that exits with the result of whatever path you have.

           

          Utility script:

          -Set Variable [ $file ; your path ]

          -Exit Script [ $file ]

           

          Then call that from anywhere to recall $file:

          -perform script [ create path ]

          -Set Variable [ $file ; get(scriptresult) ]

           

          The other option can be to use a global variable to store path, but that seems like overkill.

          • 2. Re: Set variable from a variable in a sub-script
            jbrown

            Afternoon.

            I would certainly put the file path in a field that is stored in some admin table or something. This allows it to be changed much easier than in a script.

            WHen opening the file, the first script to could set a global field or variable (i prefer the former) and be used throughout the session in any script that needs it.

             

            Variables can be set by other variables. I see that all the time. It seems pretty standard in programming languages.

            • 3. Re: Set variable from a variable in a sub-script
              David Moyer

              tays01s wrote:

               

              ... that would be useful in several other scripts.

              Hi,

              based on all of the above, including the last line of your original text ...

              You should use global fields or $$globalVariables for data shared between scripts.

              Many folk will say "never" use $$globals; others say "carefully".  Passing $locals between scripts (ScriptParameters, mentioned above) is a practice I use often.

              • 4. Re: Set variable from a variable in a sub-script
                tays01s

                Thank you for all the good ideas. It appeals to have this script run at startup, 1-off, then store the result in a global text file.

                • 5. Re: Set variable from a variable in a sub-script
                  tays01s

                  Might you instead of setting the global field via a script, instead have it a calculation field?

                  • 6. Re: Set variable from a variable in a sub-script
                    David Moyer

                    If you can run it at startup, maybe the path is calculated from the current date?  Or maybe some other dynamic source that can be determined at startup?  If so, yes, you can should make it a calculation instead.  You can create a calculation with global storage.

                    • 7. Re: Set variable from a variable in a sub-script
                      BruceRobertson

                      I think Mike B's suggestion is better.

                      • 8. Re: Set variable from a variable in a sub-script
                        David Moyer

                        Indeed Bruce.

                        mikebeargie's idea can utilize all sorts of logic - a black box, so to speak.  I call these "function scripts".  Even if it's not perfect for tays01s, it's still a great technique to utilize, regularly.

                        p.s. tays01s, you can send and retrieve multiple parameters to/from a "function script" using value lists.

                        • 9. Re: Set variable from a variable in a sub-script
                          David Moyer

                          I just poo-poo'd jbrown's answer - nothing wrong with his answer, at all.

                          There are usually many roads to the Emerald City.

                          • 10. Re: Set variable from a variable in a sub-script
                            mikebeargie

                            In this case though, you are adding the overhead of a table/field being added to your schema, with (you guessed it) the need to still script to create or grab those values.

                             

                            There's no reason why you can't just have a "settings" portion of your OnFirstWindowOpen script that sets global variables for your default settings.

                             

                            The benefit of table/fields is in the case of having non-developer users that need to adjust the settings. THEN you need the permanent schema to satisfy that need. However if you don't have users tinkering with settings, absolutely no need to add a table/fields to store settings, and in some cases, could be dangerous if someone accesses that record.

                             

                            Additionally, calling a utility script at script runtime guards against malicious tampering with the data viewer to modify global variable settings. Granted you should already be guarding against this with privilege sets, but security should always be in your mind.

                             

                            David pointed out a good reminder of "always three ways to do things in FileMaker", and jbrown gave a good second method. However, I would advise tays01s to really understand the difference in methodology here to suit their needs.

                            • 11. Re: Set variable from a variable in a sub-script
                              jbrown

                              Mike. I appreciate the clarification and the additional information. I don't usually have any problem with adding schema but it is nice to hear of alternatives. your point is well taken: if it doesn't need to change, then why bother putting it into fields and then have to retrieve that.

                               

                              As a former teacher, I appreciate your thoughtful comments to posts, and it reminds me to do the same all the time. A teacher worth his/her salt isn't going to say "use this way, dammit" but instead explain alternatives and let the person make the best choice.

                              • 12. Re: Set variable from a variable in a sub-script
                                tays01s

                                The utility script is used to set a filepath for 4 scripts: Export/ Import of records, export of a Request license & Activate files.

                                 

                                The 2 reasons I'd thought to add a field (I already have Global table) were:

                                1. I wasn't familiar with Mike's suggestion (see below)

                                2. If I put the Utility script into the Startup script, it need only run once per session and so reduces script processing time and makes all the above scripts shorter.

                                 

                                My utility script is:

                                Set Field [ Global::Path;

                                /*Windows*/

                                If ( Get ( SystemPlatform ) = -2 ;
                                Case( Home::Folder_site = "Desktop";

                                Get(DesktopPath)&"ToolsFolder\"";

                                Get(DocumentsPath)&"ToolsFolder\""
                                );

                                /*Mac*/
                                Case(
                                Home::Folder_site = "Desktop";

                                Get(DesktopPath)&"ToolsFolder/";

                                Get(DocumentsPath)&"ToolsFolder/"
                                )
                                )]

                                 

                                If you think Mike's method more efficient in the case I've described am I correct to say that I'd put the above script 'create path' into 'path' below:

                                -Set Variable [ $file ; path ]

                                -Exit Script [ $file ]

                                 

                                Then I'd put the following script lines into the 4 scripts I described at the beginning of this post:

                                -perform script [ create path ]

                                -Set Variable [ $file ; get(scriptresult) ]

                                • 13. Re: Set variable from a variable in a sub-script
                                  BruceRobertson

                                  You don't put the utility script in the startup script.

                                  You put it in the export scripts.

                                  As previously described.

                                  • 14. Re: Set variable from a variable in a sub-script
                                    tays01s

                                    Then using the alternate approach of running a script once, at start up, to set a field value that can then be used in any future script seems like it would entail fewer script lines and time running the 'utility script'.

                                     

                                    I assume I'm missing some point in the above?