MBS FileMaker Advent calendar - Door 7 - Open Files With Roundabout Route Via Temporary Files
Door 7 - Open Files With Roundabout Route Via Temporary Files |
Welcome to day 7 of our advent calendar. Today we would like to add a function to our document portal from day 3. So far we can add documents to this portal via drag and drop, what we are missing now is the possibility to open them from FileMaker.
To do this, we use the MBS function Files.LaunchFile. We can pass a path to this function and the file or folder located at this path is then launched with the standard program provided for this purpose.
But we realize now that we have a container value and no path, how do we do this now? You could perhaps come up with the idea of writing the path to a field when reading it in. This procedure becomes problematic, if we move the original file to a new location and the path is no longer correct. So we have to take a different way. We save the file in the temporary folder and display it from there.
To do this, we first create the path where we want to save the file. We determine the temporary folder with the Folders.UserTemporary function. In the Folders area, we also have further functions for other special folders such as Desktop, Applications or Documents.
Once we have the path to this folder, we still need to append the file name with the correct file extension. The file name is made up of the primary key and the file name. The file name already contains the file extension. We now put together this determined file name with the Path.AddPathComponent function, specifying our temporary path in the parameters.
Now that we have the path, we can write the file to this path with Files.WriteFile. In the parameters, we first specify the container value of the file to be written and then the composite path. Next, we use isError to check whether there was a problem writing this file. If there was none, we now open the file with Files.LaunchFile. Again, we can make an error query so that the user is not confused that no file is displayed if we cannot open the file for some reason.
Here is the script for today:
Set Variable [ $Temp ; Value: MBS("Folders.UserTemporary") ] Set Variable [ $Name ; Value: Files::PrimaryKey & Files::File_Name ] Set Variable [ $Path ; Value: MBS("Path.AddPathComponent"; $Temp; $Name) ] Set Variable [ $r ; Value: MBS("Files.WriteFile"; Files::File; $Path) ] If [ MBS("IsError")=0 ] Set Variable [ $r ; Value: MBS("Files.LaunchFile"; $Path) ] If [ MBS("IsError") ] Show Custom Dialog [ "Error" ; "File can not be opened" ] End If Else Show Custom Dialog [ "Error" ; "File can not be written to Temp" ] End If
Now we can add the button to the portal and pass the script to the button.
That's it for today's door and I hope to see you again tomorrow.
6 👈 | 7 of 24 | 👉 8 |