Publishers of technology books, eBooks, and videos for creative people

Home > Blogs > Using AppleScript's built-in database tool, Five AppleScript Tips in Five Days

Using AppleScript's built-in database tool, Five AppleScript Tips in Five Days

Scripters often need to save the data used by their scripts on a disk so that it remains available when the script is used again at a later time. Traditionally, scripters have used text files or the somewhat technical File Read/Write suite of commands in AppleScript's StandardAdditions scripting addition for this purpose.

Starting with Mac OS X 10.4 (Tiger), however, a new scriptable Database Events application is available on every Macintosh computer. Its reputation was somewhat tarnished by a few bugs in Tiger, but the important ones were fixed in Mac OS X 10.5 (Leopard). Database Events is now a superb tool for lightweight data management in AppleScript, but you have to know how to use it correctly.

Chapter 23 of our book Apple Training Series: AppleScript 1-2-3 explains everything you need to know to get full value from Database Events. This chapter takes you step-by-step through the development of a database of science fiction movie classics, using techniques that you can apply to your own scripts. For example, here's a script that retrieves the names of the lead actors in one of the all-time sci-fi greats, The Man Who Fell to Earth.

   tell application "Database Events"
      tell database "Movie Collection"
         tell record "The Man Who Fell to Earth"
            get value of every field whose name is "actor"
         end tell
      end tell
   end tell
   --> returns: {"David Bowie", "Rip Torn", "Candy Clark", "Buck Henry"}

You can click this link to open the script in the Script Editor window: Script 23.19

One of the idiosyncrasies of the Database Events application is that it may create a separate copy when you attempt to open an existing database that is already open, potentially resulting in data inconsistency. Today's tip is a standardized 'if' clause that you should always use when opening a database in order to avoid this problem:

   tell application "Database Events"
      set quit delay to 0 -- run indefinitely
      if not (database "Movie Collection" exists) then
         if not (exists database ¬
            (POSIX path of (path to documents folder) ¬
               & "Databases/Movie Collection.dbev")) then
            make new database ¬
               with properties {name:"Movie Collection"}
            save result
         end if
      end if
   end tell

Click this link to open the script in the Script Editor window: Script 23.11

As we explain in our book, this snippet always opens an existing database if it already exists, creating a new, empty database only if it does not already exist.

In addition, our book's Web site contains a downloadable file called The Presidents Database Scripts with still more examples of how to use the Database Events application.

These two scripts are Script 23.19 and Script 23.11 in our book Apple Training Series: AppleScript 1-2-3. Read Chapter 23 for the complete story on Database Events.

To learn more about AppleScript, such as how to script applications that are not scriptable by design, return to this blog each day this week for new tips from our book Apple Training Series: AppleScript 1-2-3.