FileMaker 9 Tip#4: Temporary Files

Leave a comment

7-10-2007 by Geoff Coffey

There comes a time in every FileMaker developer’s life when she needs to export a file temporarily. Maybe you’re exporting records only to import them right back in again later. Or perhaps your creating a PDF file that you only want to email to someone.

And with this need comes an eternal question: Where should you put it?

Finally, in FileMaker 9, we have an answer.

In fact, every modern operating system provides a perfect place for this sort of thing: The so-called “temp folder.” This special folder has a few special powers that make it an ideal candidate:

  1. It is always there, so you never have to worry that your user might not have one.
  2. A user on the computer can always write to his temp folder. There’s no need to stress about permissions problems. Also, the temp folder can’t be seen by any other user, so it’s secure.
  3. Best of all, files in the temp folder are automatically deleted whenever the computer restarts.

The temp folder is nothing new. It’s been right there on your Windows or Mac OS X computer from the very beginning. But it isn’t always in the same place, and it can be tough to find inside a FileMaker script.

FileMaker 9 works around this problem with a new calculation function: Get(TemporaryPath). This function figures out where the best temp folder is on the computer FileMaker’s running on and returns its path, ready to use in a variable-based export.

Here’s an example. Normally, you can’t “open” a file stored in a container field. FileMaker only lets you open the file directly if you’ve stored a reference to the file in the container. Here’s a script that works around this limitation. It exports the file to the temp folder, then imports a reference to it into a global. Finally, it tells FileMaker to open the temporary file. export the contents of a container field to the temp folder on any computer:

 Set Variable [ $path; Get(TemporaryPath) & "my_temporary_file.jpg" ]
 Export Field Content [ My Container Field; $path ]
 Insert File [ Reference; My Global Container, $path ]
 Go to Field [ Select/perform; My Global Container ]

10 Comments

  1. George D. Appel

    Great idea! However, I don’t know what to use for “Insert File” in FMP 9 Advanced.

    Thank you for your tips and help.

  2. Dom

    Thanks for this tip, but how can I give to the temporary file name the same file extension of the file I wish to open.

    For example if I have several containers with different kind of files types (pdf, word, etc…) the temporary file should have the same file extension .pdf or .doc as the original file so that it opens in the right application.

    Thanks in advance for your help.

  3. Geoff Coffey

    @dom: I’m going to apply two little-known calculation tricks to get your answer.

    First, when you use a container field in a text-like way, and the field contains a file, you get the name of the original file. For example, if you have a container field called Document and you make a calculated text field with this formula: GetAsText(Document) you’ll see the name of the original document in the field.

    Second, the Position function can search from the end of a text value if you set the start to -1

    So here is a formula that gets the file extension of the file stored in a container field:

    
    Let(
       name = GetAsText(Document);
       Middle(name; Position(name; "."; -1; 1) + 1; 99999)
    )
    

    This will only work if the file is stored in the field. If you store only a reference to the file, you can use a similar technique. In your data viewer, use GetAsText to see what FileMaker reports about the reference to the file, and write a calculation to parse it as appropriate.

    Hope this helps.

  4. Dom

    Thanks !

    With the GetAsText function it works perfectly

    1 Table “files”
    1 container field “container1″
    1 container field “global_container”

    and here is the script:

    
    Allow User Abort [ Off ]
    Set Variable [ $file_name; Value:GetAsText ( files::container1 ) ]
    Set Variable [ $path; Value:Get ( TemporaryPath ) & $file_name ]
    Export Field Contents [ files::container1; “$path” ]
    Insert File [ Reference, files::global_container; “$path” ]
    Go to Field [ Select/perform ; files::global_container ]
    
  5. Homer

    I’ve had some issues with the Get(TemporaryPath) Function.

    It was my understanding that this dir was “temporary” meaning the documents I export to this dir would be deleted at shut down. I have not found this to be the case.

    In order to have files removed (and thats at FMP startup not shutdown), I have had to preface the filename with the string FMTEMPFM. This seems to indicate to FM that this is a true temp file.

    I haven’t seen any documentation on any of this and was looking for additional insight.

  6. Geoff Coffey

    @homer: I had the same misconception you had. I thought this temp folder was emptied on reboot, just like the /tmp folder on a typical unix-ish system. But in my more recent testing, it does not actually empty automatically. Apparently it is our job to delete the file when we’re done.

    I have not discovered the FMTEMPFM thing you mention. If that works it is a great tip. I’ll play with it and see what I discover.

    Otherwise, it is possible to delete files in FileMaker without a plug-in. You just have to get an empty found set:

    
    Show All Records
    Show Omitted Only
    

    Then do an export. When you do the export, if the file you target already exists it will be deleted. And since there’s nothing to export, no new file will be created. This is a hack, and may certainly stop working in a future version of filemaker, so a sanctioned technique would be much better.

    Thanks for the tip.

  7. Robert Gosney

    I have had a similar issue with variable that are available during a script, and allow for all sorts of data comparisons, but in the end, if I have a result of a calculation that can only be addressed with a Variable, I’m stuck with it, as I can find no way to store a Variable to a field in a table ($VariableName). Is there some way to store these Variables?

    Thanks,

    Bob

  8. Geoff Coffey

    @Bob: I may be misinterpreting your question, but you can put a variable value into a field using the Set Field script step:

    
       Set Field [ My Field; $my_variable ]
    

    Does that help?

  9. Robert Gosney

    Geoff,

    Thanks. That’s the answer. I’ve been a programmer since 1982, and my company has just purchased Filemaker Server and the Clients. I now have to “re-educate” myself! This is a whole different world from PHP, or C++. Thanks for the help.

    My next great discovery will be to find the function that “flags” when the contents of a field have been changed in a table. I believe that there is a function out there in Filemaker for this, just don’t know where.

    Thanks again!

    Bob

  10. dontoon

    As always, there are many ways to achieve the same results in FileMaker. I just create 2 container fields, “file,” and “fileref.” Then Insert File is a scripted button where first a reference is stored in “fileref,” than a variable GetAsText grabs the filepath, and uses it to insert the actual file in “file.” Now you can have both the benefits of having the actual file, and that of opening the file using the techniques described above.

Tell Us What You Think

*
* (will not be published)