MBS FileMaker Advent calendar - Door 24 - Merry Christmas with Apple Script
Day 24 - Merry Christmas with Apple Script |
It's December 24th and it's time for the presents to be delivered. That's why we want to wish you a Merry Christmas in our application and we want to do this in a very special way, with Apple Script.
AppleScript is a scripting language developed by Apple that makes it possible to automate applications on macOS and simplify various tasks. Since its introduction in 1993, AppleScript has served as a bridge between the operating system and numerous programs to control them through scripts. The scripting language makes it possible to automate recurring processes, exchange data between applications or create complex workflows. AppleScript is particularly useful when many recurring tasks have to be performed manually. An example of this would be editing and sorting files, extracting information or controlling external applications such as Safari, Finder or Microsoft Word. The basic syntax of AppleScript is comparatively easy to learn. With the MBS FileMaker Plugin you can also start Apple Scripts from FileMaker. Today I will show you how this works. I would like to take this opportunity to point out that this door is not a course on Apple Script syntax, but simply presents three different examples.
We use the functions from the AppleScript component. Here you will find various functions for compiling AppleScript, for working with properties and processing errors.
The function we want to concentrate on today is AppleScript.Run. This function compiles an Apple Script text and then runs it. If this script returns a result, this is also the return of the AppleScript.Run function. Now we want to see this function in use and what would be more obvious today than wishing Merry Christmas. If we write Say "Merry Christmas" as text in this function, we get an acoustic return of this text via the speaker.
Set Variable [ $r ; Value: MBS("AppleScript.Run"; "Say \"Merry Christmas\"") ]
But we can not only generate speech output, as would have been possible with the functions of the Speech component, but we can also interact with other applications if they offer an interface for Apple Script. We now want to do this with the Notes application. This application should now create a new note and we also want to define the content of the note. To do this, we must first address the application with tell application “Notes”. With the command activate we then go to this application and start it if it is not already running. A new note is then created with make new note. With with properties we can then define a title and the total text in the curly brackets. This is what the Apple script would look like if you ran it in an editor:
tell application "Notes" activate make new note with properties { ¬ name:"Marry Christmas", ¬ body:"Today is Christmas Eve! We wish you " & ¬ " a Merry Christmas and a Happy New Year 2025"} end tell
And this is what it looks like as a FileMaker function:
Set Variable [ $r ; Value: MBS("AppleScript.Run"; "tell application \"Notes\"¶ activate¶ make new note with properties " & "{name:\"Marry Christmas\", body:\"Today is Christmas Eve! " & "We wish you a Merry Christmas and a Happy New Year 2025\"}¶" & " end tell ") ]
We must pay attention to the line breaks after the individual commands, otherwise errors will occur if these are forgotten.
It works in a similar way if we want to add a new reminder in the Reminders app. Here too, the app is activated first. We want to add the new reminder to a specific list. To do this, we address this list with tell list, followed by the list name. Here we then add the new reminder with the desired properties. All comands tell that are called must also be closed again. For this reason, we have the closing tag end tell twice here.
tell application "Reminders" activate tell list "Christmas dinner" make new reminder with properties {name:"Honey Glazed Ham"} end tell end tell
The ¬ character is the line continuation character in Apple Script. We use it here to avoid very long lines in the blog post.
And this is how it looks in FileMaker again.
Set Variable [ $r ; Value: MBS("AppleScript.Run"; "tell application \"Reminders\"¶ activate¶ tell list \"Christmas dinner\"¶ make new reminder with properties " & "{name:\"Honey Glazed Ham\"}¶ end tell¶" & " end tell ") ]
This brings us to the end of our calendar. I hope you enjoyed it and that you were able to learn something for your everyday work from one or two of the doors. The whole MBS team wishes you a Merry Christmas and a successful year 2025
23 👈 | 24 of 24 |