Log in
Hot & Trending on FMT

A Taste of Applescript

Featured A Taste of Applescript

 

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:

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.)

Now, let’s start with the first step: 

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.

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.

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.

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.

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.

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.

 

Read more http://www.soliantconsulting.com/blog/2014/05/taste-applescript

Soliant Consulting

Soliant Consulting employs the largest FileMaker development team in the world. We have a deep bench of talent starting with our CEO, Bob Bowers: he's co-authored seven books, led the team that has written six editions of the Authorized Training Series for FileMaker, Inc., spoken at more than a dozen FileMaker Developer Conferences, and has taught literally thousands of students spanning two decades of leadership in the field. Certified Expertise Soliant's team is certified in every version of FileMaker Pro and are specialists in building high-performance databases, hybrid web-FileMaker applications, integrations with a range of technologies including SQL, and conversions from prior versions. We work with your solution as is or can build from scratch. Find out how we can take your FileMaker databases to the next level.

Website: www.soliantconsulting.com/filemaker