How to use AppleScript to extend the functionality of FileMaker, and enable web browsers to bookmark web pages directly into a bookmarks database.
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. There is also a fair amount of info in the newer OS Mac Help systems. There are extra AppleScript Help 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), in 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. They are all structured somewhat differently. If you can export them as tabbed text, that would work, or parse the html export with a text editor like BBEdit Lite. 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 it to tell the browser to get the web address, tell FM to create a new record, put the URL in it and pause so you can write a note to describe it.
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. 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).
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 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 gets confusing when you mix the two; sometimes you 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 you use 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, to tell "FileMaker Pro" to activate,* you can write, compile and test the AppleScripts within the Script Editor application (or other AS 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.
*It's kind of a Catch 22. You need the tell "FileMaker Pro" to activate block to run an AS from the Script Editor. But you can't have the block in the Perform AppleScript FM script step (esp. in v.5). So, write and test your applescript in the Script Editor, with the blocks. Then remove them just before pasting into FM (keep the block version too, for later editing). FM doesn't need the blocks. It knows who it is :-)
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.
*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. It's 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. If you crash:
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. 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.
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.
Send comments, questions, toys to Fenton Jones