6 Replies Latest reply on Mar 13, 2014 9:36 AM by jwilson

    set local variable to pathname within script?



      set local variable to pathname within script?


           Within a script, is it possible to set a local variable to the full pathname of a particular file (chosen by the user via dialogue box)

           and then later

           Use that same full pathname in the Import Records script step, without further dialogue?


           I am trying to simplify / automate a data import process.


           Many thanks for any help

        • 1. Re: set local variable to pathname within script?

               Yes. I do it in some of my "update the file" scripts.

               Define a container field. I make mine a global container field.

               Use Insert File to insert the file into the container field with the "by reference" option specified. This produces the dialog where the user can select the file.

               Then extract the file path from the container field and set the $Path variable to it in order to use it with your Import records steps.

               For more on $Path variables, container fields, and a calculation for extracting the file path from a container field, see: Exploring the use of a $Path Variable in Scripts

          • 2. Re: set local variable to pathname within script?

                 Thanks for this.  The approach has enabled me to resolve a number of other challenges I had up my sleeve, BUT I have not yet managed to get the Import Script step to properly understand the $Path variable.

                 Inserting the file "by reference" into the container field, and setting $Path to that field results in a complicated value for $Path:

                 file:GPS Data/21 02/GPS 5/Pontos de passagem_21-FEV-14.gpx
                 filemac:/Macintosh HD/Users/jw/Documents/Project/Surveys/Base de Dados/Base de Dados 4_03_14/GPS Data/21 02/GPS 5/Pontos de passagem_21-FEV-14.gpx

                 It looks like the value stored in the container field is both a relative file path as well as the Literal File Path.  This results in the scripted import process failing with the error "XML/XSL information is not enough to proceed with the import/export"

                 As I understand things, the literal file path should be fine in this case as $Path is temporary and generated by locally the script.

                 If I specify the file to be imported manually, the process runs OK, so it shouldnt be a problem with either the file to be imported or the XSL stylesheet.

                 Grateful for any further guidance you may be able to offer.



            • 3. Re: set local variable to pathname within script?

                   The container field contains the file path, but also a lot of other text. The thread that I recommended in my last post should have provided the following info:

                   GetValue ( YourTable::ContainerFIeld ; 2 )

                   should return the file path in the container field when you use Insert File with the "by reference" option selected.

              • 4. Re: set local variable to pathname within script?

                     Many thanks again for this.

                     Although $Path is now successfully set to the literal path name, the script still fails with the same error.  Looking at your database I think the issue is related to the following:

                     "When using $Path with Import Records, you need two entries: $Path should be the first line, but you also need a second line that is a valid reference to an existing file. That second line has to be a valid reference to a file at the time that you specify field mapping for the script step. FileMaker needs to be able to open that file before field mapping can be specified and retained in the script step.

                     This second line does not need to be valid when the script executes."

                     However from the above I do not understand exactly where the "two entries" should be inserted.  As a script step? In the Import dialogue box?

                     Again grateful for any assistance.  Just cant get my head around this one.....


                     The relevant bit of my script as it stands is shown in the attached image



                • 5. Re: set local variable to pathname within script?

                       You put the two entries into the specify output file dialog.

                       Type in $Path as line one, then click the Add File... button and add a reference an actual copy of the file. This should produce a second file reference on the line below the $Path entry. When importing records, FileMaker will use the first entry unless that reference is invalid, then it will try the next listed entry.

                       When you have the script open in the script editor and are setting up the mapping fields portion of Import Records, $Path has no value and FileMaker uses the second entry to open a file in order to list fields in the field mapping dialog. When you actually run the script, $Path has a file path and thus it uses the first entry.

                       Note: Use Get ( LastError ) immediately after the insert file script step to detect when a user might click cancel instead of inserting a file. If the user cancels and your script does not detect it, the $Path variable will be empty and FileMaker will attempt to import from that second file reference that we just added to your import records script.

                  • 6. Re: set local variable to pathname within script?

                         Yes, that's cracked it!  The problem was that I had put the $Path variable in the http reference section of the specifiy file dialog, an area where the Add File... button doesn't exist.  However all solved now. 

                         MANY THANKS!