Extending ScriptMaker Scripts with AppleScript Commands - File Maker

FileMaker Pro’s own ScriptMaker tool is a wonderful environment for creating scripts, and there’s no substitute for what it can do within FileMaker Pro. However, for all that it is, ScriptMaker has a finite number of commands. You can’t add your own commands to ScriptMaker (unless you buy or write a plugin), but you can harness the power of AppleScript to create new commands and calculations that go well beyond the scope of ScriptMaker. You can also invoke some of the “off the shelf” commands from FileMaker Pro’s AppleScript dictionary to make FileMaker Pro perform some familiar commands with interesting twists. AppleScript is extensible, meaning that there’s no limit to the number of commands you can bring to FileMaker Pro.

You’ll start by comparing some FileMaker Pro ScriptMaker commands to their AppleScript counterparts, and you’ll discover in a few short steps how AppleScript can enrich the computing power and user interactivity of FileMaker Pro.

Show Message vs. Display Dialog
FileMaker Pro 6 introduced the “Show Custom Dialog” ScriptMaker script step, and opened up a rich new way of providing user interactivity within a script. In earlier versions of FileMaker Pro, however, a developer had to contend with the much more limited “Show Message” command. If you’re supporting older versions of FileMaker and wish to mimic some of the functionality of “Show Custom Dialog,” you can use AppleScript’s “display dialog” command.

FileMaker Pro’s Show Message command can display up to 255 characters of static text, and the user must respond by clicking one of (up to) three static buttons. The rightmost button is always the default button.

Show Message vs. Display Dialog

AppleScript’s display dialog command can display up to 255 characters of dynamic text, and offer (up to) three dynamic buttons. You can also add a color icon and an editable text field seeded with any value. The default button can be any of the three buttons, and you can instruct the dialog box to disappear after a prescribed duration.

Show Message vs. Display Dialog

To construct this example, assume that you have a database with the following fields, shown as name value pairs:

You also have a ScriptMaker script that executes the Perform AppleScript command with the AppleScript script below pasted into the Script text field. You can dispense with the outer tell blocks because any AppleScript script found inside the Perform AppleScript script step automatically targets FileMaker Pro and the current database.

It takes some extra work to get to this point, but the payback is obvious. The Display Dialog Example.fp5 database on the CD-ROM recreates the above example.

Sort vs. Sort How to Sort and Stay on the Same Record
When you perform a standard sort command in FileMaker Pro, the current record is lost. Following a sort, the first record becomes the current record.

When you use AppleScript to sort the records, the current record is retained. Depending on the situation, this result may be more in line with the users’ expectations.

Find vs. Filtered References How to Perform a Find Within a Find Without Losing the Original Find Request
Suppose your current found set contains all the invoices for a given customer. You can create a Show Unpaid button that executes this ScriptMaker script step:

And only this customer’s unpaid invoices will be found. As a bonus, the prior find request remains intact, so you could perform your original find again to restore all the invoices for this customer.

One important drawback to be aware of when using filtered references is that they do not make use of FileMaker Pro indexes. If you’re not careful, this can be deadly slow on a database with hundreds of thousands of records. If you’ve written a script that contains time consuming statements (typically sorts and finds) you may have to add extra logic to handle timeouts. By default, AppleScript will allow any script statement to execute for two minutes without acknowledgement. Then it throws an error (“Apple Event timed out”) and halts.

As a precaution against encountering the timeout execution error, you can do two things. You can extend the timeout interval, and you can trap for the timeout error (error number 1712). Here’s a script that does both:

String Comparison with Considering and Ignoring
AppleScript’s string comparison qualifiers can be a powerful complement to FileMaker Pro’s own text functions (e.g., Exact) and comparison operators (e.g., “=”). AppleScript’s considering and ignoring controlling statements can form a block around one or more lines of script, and the evaluation of each comparison operation within that block is influenced as specified. You can specify whether to consider or ignore case, white space, diacriticals (accent marks), hyphens, and punctuation in your string comparisons.< considering case but ignoring white space and punctuation

Parsing Repeating Fields
When an AppleScript script gets the contents of a FileMaker Pro repeating field, it stores the value of that cell as a list. You can use AppleScript’s powerful list operators to count, reverse, append, extract a range, and parse this repeating field as desired.

In this sample script, you convert the contents of the FileMaker Pro repeating field called “Flavors Repeating” {“cherry”, “raspberry”, “vanilla”, “chocolate”} into a string delimited by spaces (“cherry raspberry vanilla chocolate”) and assign that string to the cell “Flavors String”.

Conversely, you can copy data into a repeating field from a string by parsing the source string into a list and then assigning that list to the repeating field.

The sample database Repeating Fields.fp5 explores this example further.

Resizing a FileMaker Pro Window and Tiling Multiple Windows
FileMaker Pro’s AppleScript dictionary indicates that it’s possible to change the value of a window’s bounds property. A window’s bounds are a rectangle defined by its upper left point and lower right point {x1, y1, x2, and y2}.


Armed with this information you can write an AppleScript script that will automatically tile all of the FileMaker windows on the screen, regardless of the monitor resolution that’s used. In the following example, you’ll tile four windows.

First, create two unstored calculation fields in your primary database:

Then paste the following AppleScript script into the script text box of a ScriptMaker script’s Perform AppleScript script step:


You’ll find the sample databases named Tile1.fp5 through Tile4.fp5 on the companion. Opening Tile1.fp5 will perform a -start-up script that will cause all of the Tile databases to be opened and tiled.

Repositioning a FileMaker Pro Window
Another handy script is one that will change a window’s position but leave its size unchanged. This script prompts the user for x and y coordinates and uses the bounds property to get the original position and set the new position of the frontmost window. This script is also available in the Tile1.fp5 database.

Here’s what the dialog will look like:


Pasting Styled Text from One Field to Another
ScriptMaker’s Set Field script step copies unformatted text into a field. If you want to preserve the text formatting, you must use Copy and Paste via the Clipboard, which is generally frowned on because it intrudes upon the user’s workspace. AppleScript can help. AppleScript supports a variation on the string class called “styled text”, and it can be used to copy formatted text from one FileMaker Pro cell to another without using the Clipboard.

ASCII Functions
A pair of functions sorely lacking in FileMaker Pro are the ASCII character and ASCII number functions. AppleScript can fill that void with commands from its Scripting Additions folder (see the heading “And So On . . .”, below).

Using the Perform AppleScript script step, you can convert any character to its ASCII number equivalent with this two line script:

And you can convert any integer between 0 and 255 to its ASCII character equivalent with this two-line script:

The FileMaker Pro fields ASCII Character and ASCII Number must exist in your database for the above scripts to function.

  • ASCII character 9 (horizontal tab) is used as a tab character inside a text field.
  • ASCII character 11 (vertical tab) is used as a carriage return inside a text field.
  • ASCII character 29 (group separator) is used in repeating fields as a delimiter between each repetition.

As an example of how you can make use of these special characters, the following Perform AppleScript script step constructs a text field with an embedded tab character:

All of the above sample scripts are found in the sample ASCII and Styled Text.fp5 database.

And So On . . .
AppleScript comes with an extensive library of commands, installed in the Scripting Additions folder inside your System folder. You can explore the syntax of these commands with Script Editor using the Open Dictionary command. The picture below shows all of the commands furnished by the Standard Additions scripting addition, and the String Commands suite is profiled in detail on the right.


You can add many more functions to your script library by installing thirdparty scripting additions into the Scripting Additions folder.

Controlling the Finder from FileMaker Pro with AppleScript
The most under appreciated database on the Macintosh is the Finder itself, and you can tap into your database of files and folders with AppleScript. AppleScript can query the Finder, navigate through nested folders, and catalog the contents of a folder. AppleScript can also create and write to text files and then read back the contents of those files into FileMaker Pro fields. It can even perform these tasks on remote computers over TCP/IP!

Additionally, many of the system software components of Mac OS 9 and Mac OS X are AppleScript savvy. QuickTime, Sherlock, URL Access Manager, Speech Manager, ColorSync, Terminal, and many others can be controlled and queried using AppleScript.

Creating an Apple System Profile Report and Importing into FileMaker Pro
The following AppleScript script creates a profile of your computer using the Apple System Profiler and then imports this report into a FileMaker Pro database.

The Apple System Profile.fp5 database provided on the companion demonstrates the use of this AppleScript script, called from a ScriptMaker Perform AppleScript script step. Although Mac OS X includes an Apple System Profiler application, the above script only works with Mac OS Classic, although a good exercise would be to edit it to work with OS X.

Hiding an Application
Perhaps you’re familiar with the use of the ScriptMaker step “Toggle Window [Hide]” to hide a particular window (database). How do you hide a whole application (without making a trip to the application menu)? Here’s how:

You need to include the try block here because there might be any number of reasons this command would fail. If FileMaker Pro isn’t running, for instance, there is no process of that name, and the mere mention of it causes the script to throw an error. You can think of AppleScript’s try statement as playing the same role as the ScriptMaker script step “Set Error Capture [on]”.

More generally, the following script hides the front most application. It’s a wonderful script to put in your OSA Menu’s Universal Scripts folder and assign to a keyboard shortcut. (Oops! — the boss is coming . . . click click.)

Revealing an Application and Its Windows
Here’s a script that could save you some user support headaches. It will activate and/or reveal your FileMaker Pro solution no matter what state it’s in.

If you’re running Mac OS 9, stick this script in your OSA Menu’s Universal Scripts folder and call it “Reveal FileMakerR+S”. Then if your solution ever becomes buried under another application’s windows, or it has its own windows collapsed or hidden, you can tell your users to just type COMMAND-SHIFT-R to bring FileMaker Pro to the foreground and reveal your solution. Under Mac OS X, you can place the same script in your Scripts folder, located in your user directory at ~/Library/Scripts/. Create a subfolder called “FileMaker Pro Scripts” and place the Reveal FileMaker script inside. Note that the Mac OS X Script menu does not support command-key shortcuts.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

File Maker Topics