Create Folders from FileMaker Pro Using AppleScript on a Macintosh


Someone on one of the Facebook FileMaker Group pages recently asked for a simple method, using AppleScript of creating a folder on the Desktop. FileMaker Pro can generate a variety of file formats, including Text, PDF and Excel, and it is possible to script the names of these files. It is also possible to script saving files to specific locations on your hard drive. Unfortunately FileMaker lacks the ability to create folders without the use of plugins. AppleScript on the Macintosh gives FileMaker this ability. I found the basics of this script on Stack Overflow. I thought I would flesh it out into a demo file, put in some error checking and share it here.

Download Demo File

 

Create a Folder

Enter a Folder Name and then click one of the buttons to create a folder in that location.

Folder Created on the Desktop

Button Setup

Each button runs the same script but use a different Script Parameter to determine where the Folder will be created.

Other Possible Locations

The options are: Desktop, Dropbox, Google Drive and Documents. It is possible to use other locations, basically any location starting from the User level, such as Pictures, Music, Sites etc. It is also possible to hard code the path into the script and skip the need for a Script Parameter on the button.

FileMaker Script

Edit Script “Create a Folder with Applescript”

The script is pretty simple.

  1. a check to see if this is a Macintosh capable of running AppleScript.
  2. setup four variables
    a) $FolderName – from a global field.
    b) $UserName – this is the Macintosh System User Name as opposed to FileMaker Account Name.
    c) $ScriptParam – where to place the folder – Desktop, Documents, Google Drive, Dropbox
    d) $Path – a calculated version of the Path to the folder. This is used in the AppleScript to check to see if the folder already exists.
  3. Check to see if the Folder Name has been filled in.
  4. Run the Calculated AppleScript using the four variables.

Adapting to your solution

You should be able to copy and paste this script into any FileMaker 11, 12 or 13 database and have it run correctly, once you set up the source field for your Folder. It is not necessary to use a global field, it could be any regular field.

Modify the $FolderName variable to something appropriate in your own database.

The AppleScript Calculation

This looks a little complicated, but all that is really happening is that the variables in the AppleScript are being replaced with values from FileMaker. Quotations marks are being escaped, because they confuse FileMaker, but AppleScript requires them. Finally paragraph returns are inserted so AppleScript can read the lines correctly.

The script sets up some parameters, including the Folder Name and some parameters for the error Notification, if the folder already exists.

Here is the Calculated AppleScript:

set folder_name to "•••My Folder Name"
 set notificationTitle to the 
      "The Folder '" & folder_name & "' Already Exists"
 set notificationSubTitle to 
      "This script has already been run."
 set notificationMessage to 
      "No further action is necessary at this time."
tell application "Finder"
 activate
--test to see if a folder exists.
if exists folder 
      ":Macintosh SSD:Users:yourusername:Google Drive:•••My Folder Name" 
then
display notification notificationMessage 
       with title notificationTitle subtitle 
Title sound name "Chime"
 delay 1
else
-- if no folder exists, then create one.
 make new folder at folder "Google Drive" 
       of folder "douglasalder" of folder "Users" 
       of startup disk with properties {name:folder_name}
end if
end tell

That’s it. Hope this is useful.

2015-05-21 Update: Added an AppleScript/FileMaker script to create sub-folders to the demo file. Simplified the AppleScript with a ‘Try/End Try’, instead of using the ‘If/Else’ to test to see if the folder exists.

Sub-Folders

11 Responses to “Create Folders from FileMaker Pro Using AppleScript on a Macintosh”

  1. The download is not working today, Douglas.

    I thought that FileMaker 14 had something new concerning handling folder …

    Any confirmation ?

    Have a sweet day.

  2. this is (almost 😉 ) exactly what i’ve been looking for… thank you!

    Is there a way to add subfolders within the named folder too?

  3. very kind! incredibly swift…

    however… I’m getting this error:

    Finder got an error: Can’t get folder “” of folder “dommoir” of folder “Users” of startup disk.

    when i run the script in my solution… any ideas?

    • Make sure that your list has no double spaces in it. Activate the debugging steps in the script. Copy the generated AppleScript from the global field into Script Editor and see where the problem lies.

  4. thx douglas… I’ll check it tomorrow… very late in the UK now

    grazie mille

  5. Hi Douglas,

    Managed to sort the issue… i wasn’t specifying the variable “Desktop”… silly me 🙂