Add Folder Trigger
Add a folder action, which will trigger a script when the monitored folder is changed.
Add Folder Trigger [ Select ; Result (error) ; Folder path ; File name ; Script name ; Trigger for ; Switches ]
|Select entire contents||replaces the entire contents of a field with the result. If not checked the result replaces the currently selected portion of the field|
|Result (error)||an error code|
|Folder path||the path of the folder which needs to be monitored for changes|
|File name||the name of the FileMaker file that contains the script|
|Script name||the name of the script to trigger when a change in the folder occurs|
|Trigger for||specify if you want to trigger for files, folders or for both|
|Switches||further modify the behavior of this step|
Switches can be left empty or set to this switch:
|-AddedItemsOnly||only trigger a script when an item is added (not when an item is removed from the folder or modified)|
Data type returned
The returned result is an error code. An error always starts with 2 dollars, followed by the error code. You should always check for errors. Returned error codes can be:
|$$-50||paramErr||there was an error with a parameter|
|$$-120||dirNFErr||the monitor folder could not be found|
|$$-4295||maxFolderTriggersErr||the maximum number of folder triggers is already running|
Other errors may be returned.
Activator Plug-in 6.0
FileMaker Pro 16 to 19
The script parameter of the triggered script will contain all the information on what was changed in the folder. This is a return separated list, containing a multiple of 2 lines, where the first line will contain what action happened, for example ADDED or REMOVED. The next line contains the path to the file or folder that was changed. The path is a fully qualified path compatible with FileMaker Pro’s paths, on macOS a path will be formatted like this:
on Windows it is formatted like:
NOTE the script might be triggered multiple times when multiple items are changed, with each ScriptParameter containing only a part of the change information.
Possible returned actions can be: ADDED, REMOVED, MODIFIED, RENAMED_OLD_NAME and RENAMED_NEW_NAME.
When a file or folder is renamed you will first receive the RENAMED_OLD_NAME action with the old path, followed with the RENAMED_NEW_NAME action and the new name.
NOTE You don’t need to delete any trigger data, all data comes in via the script parameter.
Add Folder Trigger [ Select ; Result: $ErrorCode ; Folder path: "/MacHD/incomingData/" ; File name: "FolderMonitor.fmp12" ; Script name: "Folder_TriggerScript" ; Trigger for: Files and Folders ; Switches: "" ]
This will start to monitor the folder “/MacHD/incomingData/”. When there is a change in the monitored folder (like a file or folder is added to this folder), it will trigger the script “Folder_TriggerScript” in file “FolderMonitor.fmp12”. Note that this example is somewhat simplified, normally you should not use a hard coded filename like in the above example.
When the script “Folder_TriggerScript” is run, use Get( ScriptParameter ) to get detailed information on what was changed in the monitored folder. The script parameter might contain for example this text with 4 lines (on macOS):
ADDED /MacHD/incomingData/new_report.pdf REMOVED /MacHD/incomingData/stale_folder/
on Windows ScriptParameter might contain:
ADDED /D:/incomingData/new_report.pdf REMOVED /D:/incomingData/stale_folder/
This indicates that the file new_report.pdf was added to the folder and the folder stale_folder was removed.
Create a trigger script called Folder_TriggerScript. Add the following script steps to your script:
Enter Browse Mode # Get the data from the script parameter Set Variable [ $ScriptParameter ; Get ( ScriptParameter ) ] # We are getting each pair of information out in a loop and store it Set Variable [ $index ; 1 ] Loop # Get the action first Set Variable [ $Action ; GetValue ( $ScriptParameter ; $index ) ] # Now get the path: Set Variable [ $Path ; GetValue ( $ScriptParameter ; $index + 1 ) ] # If $Action and/or $Path is empty, there are no more lines of information, and we are done: Exit Loop If [ $Action = "" or $Path = "" ] # We have a new change, store it into the database: New Record/Request Set Field [ folderEvents::ActionType ; $Action ] Set Field [ folderEvents::Path ; $Path ] # ** Add your own stuff here. ** # Point the index to the next pair: Set Variable [ $index ; $index + 2 ] End Loop Go to Field[ ] Exit Script [ Result: 0 ]
To start monitoring you can create a “Start Folder Monitor” script. Add the following script steps:
Set Variable [ $DesktopFolderPath ; Get ( DesktopPath ) ] Add Folder Trigger [ Select ; Result: $ErrorCode ; Folder path: $DesktopFolderPath ; File name: Get ( Filename ) ; Script name: "Folder_TriggerScript" ; Trigger for: Files and Folders ; Switches: "" ]
This will start monitoring the wanted folder (in this example the Desktop folder). When running on Windows and you rename a file this will trigger the script and result in this ScriptParameter:
RENAMED_OLD_NAME /C:/Users/brad/Desktop/name.txt RENAMED_NEW_NAME /C:/Users/brad/Desktop/newname.txt
Related script step
|Remove Folder Trigger|