Posted by on June 4, 2012

This article goes back quite a ways for me. I originally wrote it in 2000 for FileMaker Magazine when that publication wasn’t even on the web, but distributed its articles via a FielMaker database. Honestly, most of the information here is now obsolete, as the limitations of FileMaker’s Show Custom Message that I outline have been removed since FileMaker 4, which was the current version when I wrote this.

However, in the interest of continual archiving and assured publication of my articles here it is. Currently you can still find the original article online.

Unfortunately, I can’t yet find the technique file that originally accompanied this article. If I do, I’ll be sure to edit this to include a link to it.

Better Dialogs Through AppleScript

RATING: Intermediate
PLATFORM: Macintosh
VERSION: FileMaker 3/4

FileMaker’s built-in Show Message script step is a great way to get input from the user for scripts, but it has many limitations. The dialog box that comes up is fixed in size (except in FileMaker 5 where it can be resized), so there is a severe limit to the amount of text you can place in it, you can’t reference the contents of fields, you can’t provide an area for the user to input information, you can’t have a helpful icon in the dialog box, and if you want three buttons, one of them has to be a default button.

If the solution is definitely going to be on the Macintosh, and you’re willing to put up with a bit of overhead on your part, then there’s a better way. The overhead comes in the form of AppleScript, the Macintosh system level scripting language. AppleScript was designed to allow applications to be controlled with a script, and is usually used to automate repetitive tasks and send data between applications that are scriptable.

But it doesn’t have to be used for either of those purposes. Here I’ll show you how to use it simply to give FileMaker additional useful capabilities. You’ll need a Macintosh with System 7.5 or later (or System 7 with AppleScript installed).

The technique will work with any version of FileMaker, but in my opinion, works best with version 4 or later. The reason is that while versions prior to 4 are AppleScriptable, versions 4 and 5 allow the AppleScript to be embedded within the FileMaker file and address commands to FileMaker. If you’re using this solution with version 3, for instance, you’ll have to have the script as a separate script file, and use the Send AppleEvent ScriptMaker step to run it.

Open up the technique file ASDialog.FP3 and try both the Dialgo buttons. The FileMaker Dialog uses the Show Message script step. Some things to notice about it:

  • You can’t see all of the text that is entered in the script step because the dialog won’t resize itself for it.
  • The button on the far right is the default button, and you don’t have a choice about it. The default button is always the far right and if there’s a far right button, it’s default.
  • There’s nothing in the box other than the text and the buttons.

Now try the AppleScript Dialgo. You will notice:

  • While there are also three buttons here, none of them are default. You can make any one of them default, or none of them, and the default button can be in any position.
  • The dialog box resized itself automatically to accommodate the amount of text entered.
  • There’s an icon at the left of the text. In this case it’s an “Alert” icon, but you could have the choice of two others that are built into the command.
  • The text of the field “Input Field” is in the dialog box!
  • There’s a text entry field for the user to input text, and this text shows up in the “Dialog Text” field when you dismiss the dialog box.

If you take a look at the “AppleScript Dialgo” script you can see that it has a single step: Perform AppleScript. Inside of that is a small AppleScript that shows the dialog, takes the information from it (the button clicked and the text entered) and places it in a variable, and then sets the two FileMaker fields to parts of that variable.

Here’s the script:

tell me

display dialog "With AppleScript, we can show the value of a field: \"" & ¬
(cell "Input Field" of the current record) & ¬
"\", and we can get entered information from the user and place that in a field: "¬
default answer "" buttons {" Cancel ", "Enter", "Maybe"} with icon 2

set dialogResult to result

set (cell "gDialog Button" of the current record) to the button returned of the dialogResult

set (cell "gDialog Text" of the current record) to the text returned of the dialogResult

end tell

I’ll go over it with you line by line. AppleScript sees only a single line of instruction. To make this clear, I’ve separated the commands with a blank line.

The entire script is enclosed with what’s called a “tell block”. All of the statements within the tell block are sent to whatever is after the word tell at the beginning of it, in this case, FileMaker Pro. Since the application could be named anything, and we may not know the name, I use a tell me. “Me” in this case, refers to the programming running the AppleScript, which for versions 4 and 5 of FileMaker, is FileMaker itself.

Power Tip: It’s not necessary, when using FileMaker 4 or 5 to address FileMaker Pro by using the tell me.

The second line is the meat of the script. display dialog is a command in AppleSceript that is included with a scripting extension. The only thing that absolutely has to go after the words display dialog is a string of characters (enclosed with quotations), but the flexibility of it comes with additional arguments.

The string used here is actually a concatenation of three strings, separated by the & character, two entered directly into the AppleScript, and one that’s received from a field in the database. The backslash-quote combination is there so a quote character can be included in the dialog box. If I didn’t include the backslash, called “escaping the quote,” then the script would think I was finished with the string. The second quote after the backslash-quote closes the first string.

The second string is the contents of the field “Input Field” in the record that is currently showing. This allows us to show values from the database in our dialog box.

Notice in the third string the backslash-quote combination again. The combination from the first string and this one makes it so that the field contents appear in quotes.

By including the words default answer with double quotes after it, we’re telling AppleScript that we want a text area that the user can enter information into, and that we want that text area to be blank. If we wanted some default answer to appear, we would place that within the double quotes.

Next we tell the script what buttons to include. We want three, and we enter them all within curly braces, individually in quotes, separated by commas. We’re still limited to three buttons, however.

Take note of the way the Cancel button is specified: there is a space on either side of the word. The reason is that if we simply use the exact word Cancel, and the user clicks it, then AppleScript will cancel execution of the script, which we normally don’t want to happen. So, we add a space to the beginning. The space at the end is so that when the word appears in the button, it is still centered.

In this command, the dialog box will not have a default button, but we could include one if we wanted to, and we could make it any of the three buttons. We would simply add the words default button after the closing brace with a number of the button (numbered from left to right), as in default button 1, or the name of the button, as in default button " Cancel ".

The last part of the display dialog command tells AppleScript to include the icon. There are three built-in icons to choose from (numbered 0, 1, and 2), although it is possible to include custom icons. Try changing the script to have a different number and see how it changes the appearance of the dialgo box.

As you can see, a lot of power can be packed into a single AppleScript command. The rest of the script concentrates on getting the information that the user entered into the FileMaker database.

When a display dialog command is run, the user’s input is placed in a container called result. The third line of the scirpt places this result into a variable called dialogResult for us to use later.

The fourth line takes text of the button clicked, and places it into the “gDialog Button” field. The fifth line takes the text entered in the dialog (if any was input) and places it into the “gDialog Text” field. These fields are defined as global text fields, but they could just easily have not been globals.

Lastly, we close the tell block.

In Conclusion

Yes, it is more complicated than a simple Show Message script step in ScriptMaker, but it’s much more flexible. And this is only one way to augment FileMaker with AppleScript. FileMaker doesn’t allow scripting the placement of windows, but AppleScript does! You can’t write to a file outside the database system using FileMaker (other than exporting records), but AppleScript can do this!

The AppleScript dialog isn’t always the best choice. It too has limitations. It won’t work on Windows. Just like the Show Message ScriptMaker step, it can only have three buttons and only one input field. It is also a bit slower than FileMaker’s Show Message, since it’s using AppleScript. We can get around these limitations wither by using FileMaker plugins, by using “faux” dialog boxes with layouts in FileMaker, or by using AppleScript scripting additions. I like this solution because it is guaranteed to work on a Macintosh with AppleScript, and it is a real dialog box. I don’t have to worry about having the right plug-in or scripting addition for success.

Even with the limitations, this solution does give you more power, and can make your solutions much more professional. You don’t always need it, and I still use Show Message when I don’t need the extra power of AppleScript, but it’s a good tool to have in your arsenal.

Posted in: Articles


Be the first to comment.

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>