A Taste of Applescript

Hey everyone.

This is JohnAustin here with some cool FileMaker functionality that may be helpful for future projects that need to interact with applications outside of FileMaker on the Mac.

There are two different script approaches for communication outside of FileMaker. First is Send Event (Windows and Mac compatible) and second is Applescript (only Mac compatible obviously).

Even though Mac can use Send Event, Applescript is a much preferable option. It is much, much easier (Take my word for it). Send Event is not overcomplicated. Instead, Applescript is just so easy to get up and running.

Ok let’s give an example of Applescript’s awesomeness. For this example, there is a simple file that has a container field containing a zipped FileMaker Pro file that will open automatically so that the user does not have to open it manually.

Here is the simple FMP file called Applescript:

Applescript screenshot

As can be seen, there is a zipped file called ZippedFM inside a container. The button below that container will do all the necessary work to download it, unzip it, and then open the file.

Here is the script that performs these actions in its entirety:

(Don’t worry. We will go through each step one by one too.)

Code screenshot 1

Now, let’s start with the first step:

Code screenshot 2

The first script step involves grabbing that filename from the container. This is done by using the function, GetAsText, to grab just the file name, ZippedFM.fmp12.zip, from the container. Note that this simple GetAsText() only works on locally stored containers. If the container is stored externally or remotely, you will have to do a little more modification to get just the filename.

Code screenshot 3

The next two script steps involve setting the file location and actually downloading or exporting the field contents from the container. For the file location currently, it has been set to the local Documents folder. FileMaker has a nice function, get(DocumentsPath) that will return the documents path. The filename created earlier is now appended to the end. Now, the location to the file has been set. To download the file, the script, Export Field Contents, will need to be utilized. This is very easy. The container just needs to be selected and the file location obtained earlier set as the destination.

Code screenshot 4

These two script steps are also very important. The original $filelocation variable that holds the zipped file’s location is required. A destination must also be set. For this example, the Documents folder was selected. However, what is important here is that you will need to use the Substitute function on both variables to get rid of “/Macintosh HD”. Taking a look at the variables, it will be noted that “/Macintosh HD” is at the front of the file path. This is the root folder. In Terminal, instead of “/Macintosh HD”, it is handled as “/”. So by removing it completely, the script has the correct syntax for Terminal.

Code screenshot 5

Now comes the all-important Perform Applescript step. Everything has come together for this. For this example, I said we were going to download a file, unzip it, and then open the file. Perform Applescript is here to do the actual unzipping. The easiest and least user interaction way to unzip a file is to run a shell script that will unzip the file. Luckily, Applescript has all of the functionality within itself. Applescript can perform shell scripts, “do shell script” as seen in the picture.

Code screenshot 6

All that remains now is following the correct script syntax. This is not too bad, because unzipping in Terminal is fairly straightforward. It is as follows:

Unzip filelocation –d destination

That is the simplest way to unzip a file. For this example, –u was also added which just updates and overwrites any files that may have been unzipped in the Documents folder before running this command.

Now, the file is unzipped in the chosen file destination. Now comes a second AppleScript to open that file.

Code screenshot 7

Just like the previous Applescript, this one performs another shell script to open the file.

The script syntax for this is also easy. It is as follows:

Open filepath

Now, one final thing when running these Applescripts is that any needed quotation marks (“) must be escaped, because “ is used for text in FMP. ” is the method to escape.

With that, you have your nifty script that not only can download a file and unzip it, but also open it automatically without any user intervention.

5 thoughts on “A Taste of Applescript”

  1. Your script assumes that the user’s “home” drive is called “Macintosh HD”.

    To make your script more portable, maybe you can extract the name of the boot drive from the documents path with this:

    set variable [ $homeDrive ; GetValue ( Substitute ( get ( documentspath ) ; “/” ; ┬╢ ) ; 2 ) ]

    and then doing your $fileLocation substute using that value with appropriate wrappers.

    However, if the user’s documents location is not on the boot drive, that might not work either.

  2. Hans Erik Hazelhorst

    Hi John
    It’s been a while since your original blog post (3 years…), so let’s hope it is still ‘current’.
    I was looking for a way to select a script (and if possible, scroll to a particular scriptstep), all by pressing a button in FileMaker. I have an authoring database containing the scriptsteps of a solution (converted to XML), and the next step is making the correction process a little easier by directly locating the script in the solution. I’m talking of a pretty complicated solution here…

    Now, Filemaker does have a command ‘Open Script Workspace’ but that’s where it stops.
    Id’d like select the script and AppleScript just may do the (rest of the) trick. Do you have any working experience with that? Or some demo?

    best regards
    Hans Erik Hazelhorst

    1. JohnAustin Lamprecht

      Hi there,

      You cannot create a script that would allow you to go through the scripts in the FileMaker file and modify them to my knowledge. Open Script Workspace is a simple script step that opens the script interface for a user of appropriate privilege set can view.

      BaseElements is a great product that contains all the metadata for a FileMaker file from the Database Design Report. If you use it, you could search your scripts there. You would still not be able to make modifications, but you could find what you are looking for more easily.

  3. Hi. I have run into the problem that whenever I send out an update of a runtime, the runtime goes back Ito trial mode and the user has to re-enter the username and password. It would be nice if that could be remembered somehow, Can AppleScript be of any help here?

    1. Dag André,
      Runtimes don’t have a trial mode, they are automatically licensed by virtue of creating the runtime, so I’m not entirely sure what you are seeing. Can you post to community.filemaker.com and add a screenshot of what you are seeing?

Leave a Comment

Your email address will not be published. Required fields are marked *

We're celebrating 20 years! Read about our journey here.

Party horn and confetti
Scroll to Top