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 ]

23 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.

  11. Auridas

    Robert,

    I think You need the layout script trigger, called on record commit?:)

    Cheers,
    Auridas

  12. Bruce Robertson

    None of this is necessary in FileMaker 10. Different temp folder, it IS cleaned up every time you start FileMaker.

  13. Justin

    Just FYI:

    For some reason on my computer, Get(TemporaryPath) results in:

    /C:/Path/to/temp/file

    the leading / was keeping this from working, deleting it made it work without problems, just though someone else might benefit from this :-)

    Justin

  14. Eric Kightley

    I want to be able to open and edit files stored in the database from within the database. Is that possible?

    Thanks,
    Eric

  15. Geoff Coffey

    @Eric:

    Ummm… My head hurts. :) If I understand you correctly, I see no reason why this shouldn’t work. It will be tricky:

    1. Store the .fp7 file in a container field.
    2. When you need to access it, export it to the temporary folder
    3. Deal with file references

    For #3, if you need to interact with the file programmatically (open it with a script, run scripts in it from another script, define relationships to it, etc…) then you’ll need to be double-creative because I don’t think you can use variables in the paths of file references. You might be best off exporting the file to a more knowable location like /tmp on Mac OS X and C:\TEMP or C:\windows\temp on Windows. Then you can define a multi-entry file reference that “discovers” your file in one of several possible temporary locations. If you don’t use the Get(TemporaryPath) location, you’ll want to be sure to clean up your mess when you’re done or you’ll leave files lying around.

    Anyway, I’ve never done this, but it sounds do-able.

    Geoff

  16. Ian Mazursky

    Hi Guys,

    This is a fantastic tip!! Thanks so much.
    I can confirm that the file prefix “FMTEMPFM” works on the mac FMP9.
    The file is removed on the relaunch of FMP.

    Thanks again!
    -ian

  17. Dan Smith

    I was playing with Get(TemporaryPath) and Export Field Content today. To make it work for both a PC and a Mac, I had to use this calculation for the $path:

    Case (
    Get(SystemPlatform) = “-2″ ;
    “filewin:” ;
    “filemac:”
    ) &
    Get(TemporaryPath) & “file.ext

    As Justin said, the leading “/” caused the path to be invalid on a PC, but without it, the path was invalid on a Mac. Adding filewin: or filemac: fixes that problem.
    The strange thing was that, even without the leading “/”, the path did not work on my PC (Vista OS), however adding filewin: did work.

    - Dan

  18. Robert Gosney

    I’ve gotten stumped. I have a project that calls for a text file output that is NOT tab delimited nor CVS delimited. It needs to be a raw text file and one that I can predetermine the amount of space between the data fields.

    It doesn’t seem like Filemake will do this. OR can it? Have you ever tried anything like this before. The export feature is very limited.

    Thanks

    Bob

  19. Geoff Coffey

    Robert:

    FileMaker does not directly support this kind of export format, but you do have several options:

    1: Use XSLT. FileMaker can apply an XSLT stylesheet to any exported data, effectively producing just about any text output you can dream up. Unfortunately, you have to know how to write XSLT, which is notoriously complicated. So this is the powerful-built-in-hard option.

    2: Use a plug-in. Something like the Troi File plug-in will let you create a file and add text to it using FIleMaker scripts. This is quite flexible and powerful, and often easier for a non-programmer than XSLT. The down side, of course, is that you have to buy and install the plug-in on every machine that needs the feature.

    3: Use a hack. You can make a new table with just one text field. Then put one full *line* of your output text in this field, and one new record for each additional line. Then export this file as a Tab Delimited Text format. Since you have only one field, you won’t get any tabs in your output, and FileMaker will automatically put line break after each line for you. The end result is that you can export any line-by-line text format this way. The down side is that you can’t include a Tab character in your field (FileMaker will remove it on export since it is the delimiter).

    Often #3 is good enough, and it is quite easy to do. If you need a little more control, #1 or #2 should work.

    Hope this helps,

    Geoff

  20. Robert Gosney

    Thanks Geoff.

    This is embarrassing, but I’ve already used solution # 3 with Microsoft Access to create a text file for inport into Famous Software Database (Oracle). If I had slowed down to think about it, I would have connected the dots. Thanks for connecting them for me!

    Bob

  21. Robert Gosney

    Geoff,
    is there anyway to make the format for date maintain the Leading Zeros in the actual data table, not just in the display format on the layout?

    I’ve tried, but when I export the dates to a text file, they still have no Leading zeros in them anymore, just as selecting a date on a layout. It looks like the leading zeros are there (01/02/2010), but when you click on it to edit, the Zero go away, so I’m lead to believe that the filemaker table is not maintaining the leading zero’s in date fields.

    Bob

  22. Atalanta

    Thank you for this tip! I’m making great use of it.

    I’m working in FM11ADV on Win 7 and I noticed the temp folder go bye-bye when I closed FM. Don’t think you need the FMTEMPFM tag anymore.

    I’ve also found that the temporary folder is a great place to store a PDF that I need to create on the fly and paste into a record without cluttering up the user’s desktop.

    I could be wrong. Please let me know if I am.

  23. Mike

    Just as an update, in FileMaker 12, this works without the need to make it a global reference. I was able to take this and attach to an email, and no residual file is left. I can confirm this works on FileMaker Go 12 as well.

Tell Us What You Think

*
* (will not be published)