FileMaker 101
Part 30

This month’s article is going to be a bit of a departure. Rather than being just about FileMaker Pro (FM), it is going to include some AppleScript (AS) as well. They work quite well together, and if you like working in FM, you may find that AS can lend a helping hand for tasks that FM can’t or is not well-equiped to handle natively.

AppleScript, on the other hand, is not terribly difficult to learn. It isn’t as easy as it looks, though. I don’t think that they’ve tried hard enough to really it make approachable for beginners. For example, the best book on it, “Danny Goodman’s AppleScript Handbook,” was written long ago and was, I believe, out-of-print for a while. It is available now though, due to the new technologies of printing on demand.

The AppleScript Language Guide (PDF) and other documentation is available from Apple’s AS section, AppleScript
There is also a fair amount of info in the newer OS Mac Help systems. There are extra modules on the OS CD itself; they’re not installed by default. They have an intelligent installer that checks if they are or not, so go ahead and install them.

I’ve also included most of the big AppleScript sites in the Bookmarkss file itself.

General Comparison of AppleScript and FileMaker Pro Scripting

There are two crucial differences between scripting in AS and in FM. The first is that FM uses a basically point-and-click script construction. The command steps are there already; you just have to put them together. AS doesn’t have any “list” to choose from. You have to type everything in.

In FM the commands are there; but you are must create all of the objects on your own, Fields, including calculations, Relationships, Layouts, etc.. In fact, only the commands are defined, everything else, and there’s a lot of it, is up to you.

In AS, the commands are also defined, to an extent. They are what is in the “dictionaries” of the applications you are working with (incl. the Finder) or the Scripting Additions, or within the power of AS itself.

The problem is that you have to find out, either by reading said dictionaries or by trial and error (mostly error), just what works and what doesn’t.

Fortunately there is a pretty decent debugger within AppleScript that tells you what might have gone wrong. Sometimes you even know what it’s talking about.

In AppleScript many of the objects that you want to deal with are things that are familiar to us already, files, folders, windows, documents, etc.. The way that it refers to them is a bit more precise than what we are used to, so it takes a little bit of effort to see things its way, the computer’s way.

The main reason to use AS with FM is that is can easily do things with files themselves. Two areas where this is useful are working with internet applications and working with graphics files. We’ll cover the first one this month, as it’s easier.

Bookmarkss Example File

The example file, BookMarkss, is a basic URL bookmark manager. It can be used with any web browser.* The advantage of using an FM file is that you can have hundreds (thousands) of bookmarks, all in one place, with all the power and flexibility of FM to find, organize and label them.

I wouldn’t say that it would replace your browser’s bookmarks, but it might let you trim them down to a reasonable size again.

I didn’t mess with Exporting, Importing existing browser bookmarks; it’s too much for this article. If you can export them as tabbed text, that would work; or parse the html export. Lots of fun.

The main Apple Event that you’ll use is actually built into FileMaker already, the script step Get URL [“URL address”]. All it needs is the URL. It will use the default web browser in the Internet Config (or Internet now) Control Panel. If you have “Connect automatically” checked in the Remote Access (was it PPP before?) Control Panel, then it will launch; otherwise you’ll have to launch it first.

If that’s all you wanted to do, then you’d be done. But what use is a Bookmarks file that can’t get the current web page and turn it into a bookmark for you?

To do that you’re going to need AppleScript. You need to tell the browser to get the web address, and tell FM to create a new record and put it in the right place.

Launch Script to Get Web Page Address

You’re also going to need a way to run that script. You could save the script as an application (applet) itself, and double-click on it to do this. But I generally have a full screen view of the web page on my iMac.

Fortunately a wise man has created an excellent free tool for the purpose of running AppleScripts from the Menu Bar.

It’s called OSA Menu, by Leonard Rosenthol.

It drops down from an AppleScript icon, next to the Application Menu. There are a bunch of useful scripts included. In fact, I’ve learned many techniques from studying those examples.

It has another useful feature. It automatically creates a folder for each of your scriptable applications, for you to put your scripts in (it comes with a few already). That folder appears at the bottom of the menu only when you are in that application; you can open it then to add scripts as well as run the ones that are there.

This is very useful for our purposes, because each web browser uses slightly different (and kind of weird) language to identify the web address. We have to customize the script for each. They are pretty short though.

Copy/Paste URL

The way that I’ve done this is to just get the web address with AS, then tell FM to run a script (with the “do script “script name”” command). I wanted to bring the Bookmarkss file to the front at this time, paste in the web address, and drop down a “type” field list. That way I can quickly assign the web page to one of my categories. I can then either add notes about it, or go back to the web page.

The FM script also attempts to extract the title of the web page by parsing the URL. If you don’t like the name they’ve given it, you can edit it. It’s just text, not used for anything else really.

Refresh Web Page

Next to each web page entry is an icon of the earth. This is the “Get URL” button for that address. But clicking that when the page is already displayed by the browser will cause it to “refresh,” which is unnecessarily slow.

So we just get AppleScript to tell the web browser to “activate,” which is much faster.

There is a faded earth icon at the top left of the layout to run this script. It is an FM script that uses the Perform AppleScript step (see more below).

But FM itself has no command for this. AppleScript can’t do it either if it doesn’t know what application to tell it to.

The simple solution is to store your web application as a global field in FM. It is then put into a text calculation field that produces an AppleScript.

AppleScript in a Calculation Field

This is a good way to store a short AS, especially one that is put together with other fields (the Browser g field in this case).

It is, however, a miserable way to write long AppleScripts. FM calculations require quotes around any text element. AS also has specific places it wants quotes: application names, text blocks, etc..

To get one quote out of an FM calculation you can use 4 quotes (“”””). But it can get confusing when you’re mixing the two, as you sometimes only need 3 quotes, when you’re already inside a text block.

Another way to deal with it is to put the actual quote for AS in a separate FM global field, Quote g. Then type, or have a script put a quote in there.

Then the FM calculation looks like this; Refresh c =

“tell application ” & Quote g & Browser g & Quote g & “¶” & “activate” & “¶” & “end tell”

The result will look like this, standard AS text:

tell application “Internet Explorer”

activate

end tell

This could also be written as, “tell application ” & “””” & Browser g & “””¶” & “activate¶” & “end tell”

In this case it’s pretty simple, but it can get messy. Fortunately, between FM’s calculation parser and checking it visually, you can eventually get it right.

Perform AppleScript

You can imagine what a long script would look like in the calculation box. Unreadable.

The script step to run AppleScripts is “Perform AppleScript” (duh). You have the choice to either run a field, which is what we’re going to do in this case, or to paste in the actual AS text into the little box.

When you click the OK button FM compiles (or tells AS to compile) the script.

The reason I say “paste” above is that, with only a couple of extra steps, in order to tell “FileMaker Pro” to activate, you can write, compile and test the AppleScripts within the Script Editor application (or other editor).

It’s at least a hundred times easier. Plus, you can then turn on the Events Log to really see what went wrong that time! Not to mention the pretty colored and properly indented text.

More on this next month, when we really get into some script writing.

Pick a Browser (by creator code)

I figured that as long as we had the browser name, we might as well allow you to choose which browser you want to use to open a web page. You can change it. All you have to do is pick the name from the drop-down list in the header, and click the “Use to open URL” checkbox.

If the checkbox is off, FM will “Get URL” with your default browser.

I added a short optional routine to the beginning of the Get URL c fields. Rather than trying to address the application by name, it looks for it first by its application Id (which is the same as its creator code).

The Finder knows how to find that, but it can’t “tell” it to do anything. But it can then get the name. Then it can tell application “name” to do something.

It’s a round-about way, but it works better than just using the name. There is a new way to do this, by creating an alias of the application in the Scripting Additions folder, then addressing that somehow. If you know this technique, please write and tell me.

*I added the creator codes and matched them with the names of common web browsers with a Case calculation. I’ve got iCab, Internet Explorer and Netscape. If you’ve got something else, get the creator code from it or any of its files, and add it to the Case statement.

Another feature of the above routine is that it doesn’t matter if the name changes slightly, such as different version numbers or silly â„¢ symbols. It will work with any version of the application. (If you want to have 2 versions and switch between them, then you should use the exact name; but that would be unusual.)

Bookmarkss Copy Backup File

One problem with using an FM database for your bookmarks is that FM databases do not like to crash, and web browsers seem to. This is better now than it used to be; but it’s still a problem. If you are really serious about a database, then it is best to not use it after it crashes. It’s safer, in the long run, to use a backup copy that has never crashed. This may be extreme, but it is the safest.

The Bookmarkss file automatically backs itself up every time it closes properly. So you’ll always have backed-up bookmarks from the last time you closed it.

A. If you didn’t add, or don’t need bookmarks from the crashed session, then do this (after restarting).

  1. Throw the Bookmarkss file in the Trash.
  2. Remove ” Copy” (notice the space) from the “Bookmarkss Copy” file.
  3. Open it, then close it, to create a new “Bookmarkss Copy” backup, for the next session.(This is actually good for the file anyway.)

B. If you really need those few bookmarks from the session that crashed, then follow these instructions carefully.

  1. Move the Bookmarkss Copy (backup) file out of the folder.
  2. Open the Bookmarkss file (the one that crashed).
  3. Allow FM to check it out. Everything should be fine (as nothing was really wrong with it).
  4. Find all the records. View Unsorted (in creation order). Omit the new ones. Find Omitted.
  5. Export those records, as text (safest) or a FileMaker file (safe and easy).
  6. Close the Bookmarkss file. It will create a new Bookmarkss Copy file, in its folder.
  7. Throw them both in the Trash.
  8. Put the other Bookmarkss Copy file back in the folder. Remove ” Copy” from the file name.
  9. Open it and Import the new exported bookmarks.

 

When you close it, it will recreate a new Bookmarkss Copy file. That is why you had to move the original “Copy” out earlier. Otherwise it would have been overwritten with the crashed Bookmarkss file.)

C. Just use the backup “Copy” file, as in A., and use the History function of the browser to go to and recreate those bookmarks. Of course they are often not kept properly for a crashed session.

D. Alternatively, create and keep a Clone of the original, export all the bookmarks from the crashed file, then import into a copy of the Clone.

E. For those who are lazy and like to live dangerously, just open the Bookmarkss file. It’ll probably be fine. It’s not a real complex file anyway. Remember how to rebuild it 😉

Down Arrow in List View

There is a layout trick in the example file that I use a lot, but have never explained before. It was originated by Matt Petrovsky. I saw it in ISO’s FileMaker Magazine, <http://www.filemakermagazine.com> (hopefully he won’t mind, now that we’ve given them a plug).

It involves 2 things. First, a Subsummary Part on the layout, below the Body. It is defined to be sorted on a field that is never used for sorting. That way it will never appear in List View mode.

To make it appear, you just need to go into Form View, and voila, there it is, with only its record showing in the “List View” row above it. It appears to drop-down out of nowhere; and it gives you almost the entire screen for more record data without having to actually change layouts. To return to List View you just toggle back to List View with a script.

The second part is a down arrow button to run the above toggle, which itself toggles to a down arrow, just like in the Finder. It’s a bit harder to explain.

The arrow itself is a calculation, getting the graphic from one of a global container field with two repetitions.

It is based on a simple toggle number field, which is set with the Toggle script to either 1 or “” (nothing).

The tricky part is for the script to be able to tell which to set it to. There is no “Status(CurrentView)” function.

The technique is based on the fact that you can’t paste into a global field in most parts while in List View.

So the script pastes a 1, then checks to see if there’s a 1 in the global field. If there is, you must be in Form View, if not, then you’re in List View. The script then sets the global to its opposite, ready for the next time.

It works, but it’s confusing to explain. Check it out.

If you don’t use the test, then sometimes the arrow will be pointing the wrong way. It will still work fine, and will reset itself after you click it, but it looks a little funky.

Quickstart Instructions to use Bookmarkss:

Get and install the latest OSA Menu extension, by Leonard Rosenthol.

Restart. Put the “Copy URL” scripts for each browser you have in its scripts folder, created by OSA Menu. They are regular Finder folders; you can open them from the OSA Menu, next to the Date-Time in the Menu Bar.

You can assign a command key to the scripts by adding a backslash and a letter. Please read the instructions, as that feature doesn’t work in earlier OS versions.

Open the Bookmarkss File

Download the Bookmarkss example file. Put in on your Desktop for the first time. After that you can move it.

You don’t have to open the Bookmarkss file before you can use it, but the script may have trouble finding it the first time if you don’t.**

You can launch the web browser directly by clicking a bookmark in Bookmarkss; that is, if you’ve got the automatically connect choice on in Remote Access.

Optionally, if you want to switch to using a different browser, choose it from the drop-down list.

Disclaimer

I write these articles and create example files as learning devices. They are not really for people who just want something that works “out of the box,” but don’t care how.

I try hard to make them compatible, but they may or may not be usable on your computer (especially if it is old). This is more true of AppleScript, due to changes over the years (or due to machine specific references that I’ve stupidly left in).

I can’t be responsible for the performance or effects on your computer; but please let me know the specifics and maybe we can figure it out.

**I’ve referenced it as an alias in the applescript. This is usually a good way to put actual file names into scripts. What it means is that after you run it the 1st time, AppleScript makes an “alias reference” to it. This functions much like any other alias. You can then move the actual file and its folder wherever you want and AS will find it without asking again.

I’ve referenced it as this: “Macintosh HD:Desktop Folder:Bookmarkss folder:Bookmarkss”

So if you haven’t changed your hard drive name, you can put its folder on the Desktop the first time, and you’ll never even be asked. If you don’t, you’ll probably get an error message from AS. You’ll have to Edit the script (click the button), by typing in your path to the Bookmarkss file.

I’ve included my main FileMaker and AppleScript bookmarks in the file, so go crazy.

 


Fenton Jones

Fenton Jones is a FileMaker database designer and consultant, based in San Diego, CA. FileMaker is a cross-platform rapid-development tool for affordable relational databases. If you have need of a FileMaker Pro expert, please be sure to visit his home page at http://www.fentonjones.com

Leave a Reply