Drag a file into a container field and store only a reference

Adding content, such as an image, audio/video, PDF, or a file of any type, to a FileMaker Pro container field can be done very easily by dragging and dropping the file on the container. However, when you drag a file into a container field, FileMaker Pro always embeds* the data in the field. But what if you only want to store a reference to the file? In that case you can use Troi File Plug-in‘s Drag and Drop function.

Using the Drag and Drop function of Troi File Plug-in

To let your drag actions be handled by Troi File Plug-in rather than native FileMaker you need to tell the plug-in to start accepting drags. You can do this with the following script step in FileMaker Pro 16:

Add DragAndDrop Handler [ Select ; this::gErrorCode ; 
                          Get ( FileName ) ; "DragTriggerscript" ) ]

gErrorCode is the field where the result of the script step is stored (this is 0 (no error) or an error code). If you set the first parameter of the script step to ‘Select entire contents’ the contents of the gErrorcode field will be replaced with the result. If you do not use ‘Select entire contents’, the result is added to the currently selected portion of the field.
‘Get (FileName)’ indicates that the current file contains the script that needs to be triggered when a drag action occurs and the last parameter is the name of the script that needs to be triggered when files/folders are dropped on the current window.

*The FileMaker storage types

There are 4 types of container storage:

  1. By Reference – only a link to the original source location is stored
  2. Embedded – the file is stored within the database without any external storage
  3. External Open – the file is stored within the database within a navigable external storage structure
  4. External Secure – the file is stored within the database within an encrypted external storage structure

Options 3 and 4 treat the storage as part of the FileMaker data, the storage is not handled by the OS. When using drag and drop the file will always be stored as options 2, 3, or 4, depending on how you have set your container’s field options.

You can optionally also add the fieldbounds of your container field. This will make the plug-in only accept drags within this rectangle instead of on the whole window.

In earlier versions of FileMaker, where external script steps are not available, you need to use the TrFile_DragAndDrop function to start accepting drags with the plug-in, like this:

Set Field [ this::gErrorCode ; 
                        TrFile_DragAndDrop ( "-AddDragAndDropHandler" ; Get ( FileName ) ; "DragTriggerscript" ) ]

After this step has been executed the user can drag files on the window and the DragTriggerScript will be run.

Storing a reference to the file

To store a reference to the dragged file in your container field create a script called DragTriggerScript that reads something like this:

Set Variable [ $filepath ; Value:Get ( ScriptParameter ) ]
If [ Get ( FoundCount ) = 0 ]
    New Record/Request
End If
If [ Abs ( Get ( SystemPlatform ) ) = 1 ]
    Set Field [ this::path FM formatted ; "imagemac:/" & Substitute ( $filepath ; ":" ; "/" ) ]
Else
    Set Field [ this::path FM formatted ; // first substitute all backslashes; 
                    then substitute a possible 3 slashes to 2 slashes:
                    Substitute ( "imagewin:/" & $filepath ; [ "\\" ; "/" ] ; ["///" ; "//" ] ) ]
End If
Set Field [ this::container ; this::path FM formatted ]

The (incoming) ScriptParameter of the triggered script will contain the full path to the file that was dragged on the window. This path is of the form “Diskname:folder:file.ext” on macOS or “C:\directory\file.ext” on Windows. To store the reference we change this into a FileMaker style path of the form “imagemac:/Diskname/folder/file.ext” on macOS or “imagewin:/C:/directory/file.ext” on Windows.

This is a simplified example where we assume that only one file is being dragged and the file being dragged is always an image. Of course you can let the script handle other file types too. In that case first determine the mediatype of the dragged file and handle it accordingly. It is even possible to drag a whole folder of files at once and store each file in the folder in a new record. Have a look at the DragAndDrop example file in the download of Troi File Plug-in for a more extensive script.

Stopping drag and drop

When you no longer want the plug-in to accept drags, stopping drag and drop can be done with this script step in FileMaker 16:

Stop DragAndDrop Handlers

or this function call in earlier versions of FileMaker:

Set Field [ this::gErrorCode ; TrFile_DragAndDrop ( "-StopDragAndDrop" ; "" ; "" ) ]

More possibilities

This is just one example of how to use Troi File Plug-in’s DragAndDrop feature. By simply changing the DragTriggerScript you can of course do with the dragged file(s) whatever you like. You can also define up to three different drop zones at once, with a different script to be triggered for each zone that receives a drag. An other possibility is to accept email drags from the MacOS Mail app, with for example the ‘From’, ‘To’, ‘Subject’ and ‘Email Body’ being parsed into separate FileMaker fields.
Just download the fully functional demo version of Troi File Plug-in and start exploring what you can do with it!