the review site with a difference since 1999
Reviews Interviews Articles Apps About



Susti Heaven

Brand Perfect

Closet Nomad

Scripting Apple DVD Player 3.1.1

by Jesse Shanks

The latest update to Apple's DVD Player includes the ability to write scripts for the application and run them from a scripts menu, either in the player or system-wide. This allows for some interesting extensions to the operability of the program in itself, as well as working in concert with other Macintosh applications.

AppleScript is the native scripting language for the MacOS. Long lurking in the Apple Extras folder of every Macintosh shipped, AppleScript was one of the technologies on the chopping block upon the return of Steve Jobs and the beginning of the move to the Unix-based system OS X. However, feedback from professionals revealed that AppleScript was one of the most important, albeit under-publicized, workflow technologies on the Mac. Besides allowing the ability to automate repetitive system functions (the publishing industry used it for batch-processing), AppleScript allowed users to extend, customize and enhance the workings of applications. Since its near death experience, AppleScript is enjoying a renaissance of popularity with the new system integration capabilities, including access to Unix shell scripting, remote data services and enhanced support by popular applications. Apple has also recently released an enhancement to their Cocoa development environment, known as AppleScript Studio, that allows AppleScript to be utilized in full-blown applications. The system-wide Script Menu can be downloaded from the Mac OS X section of the AppleScript web site.

Popular scriptable applications include Roxios's Toast CD-DVD writing software, Apple's iTunes music player, their Quicktime Player media software, Microsoft's Office v.X productivity suite, Bare Bones Software's BBEdit text editor and Stairways Software's Interarchy FTP program. Adobe Systems has enhanced the scriptability of its Illustrator drawing program and the new version of its flagship Photoshop image-editing program boasts a more powerful implementation of the language.

The concept of AppleScript from the beginning was to create a computer language that resembled natural language, as closely as possible, in order to make it accessible to a broad range of users. Rather than using obscure commands and constructs, AppleScript allows the user to write scripts that corresponds to what they might say. Each scriptable application comes with a scripting dictionary that reveals that commands the application will understand. For example, the script:

Tell application "BBEdit 6.5"
Make new text window
End tell

This tells BBEdit to make a new document. Another script:

tell application "Internet Explorer"
set s to GetSource
end tell

This tells Internet Explorer to place the HTML source of a web page into the variable "s". These two applications can work together through AppleScript as is shown in this script:

tell application "Internet Explorer"
set s to GetSource
end tell
tell application "BBEdit 6.5"
make new text window
set contents of window 1 to s
select insertion point before character 1 of text window 1
end tell

So here we see, in a very easily understandable fashion, Internet Explorer is told to get the source of a web page and store it in a variable and then BBEdit is told to come to the front, make a new window and insert the source of the page into the text window. This is a very handy little script!

It all seems straight forward enough but it is true that in order to write more than the simplest of scripts one must understand some fairly complex computer concepts. Some applications are recordable, which means that one can proceed through a series of actions and record these to a script, which can then be viewed and edited into a final version. However, things like concatenation, repeat loops, "else if" statements and other aspects of programming the computer must be learned.

The DVD Player application comes with a scripting dictionary that allows the intrepid user to control aspects of the way the player acts, looks and responds. Besides commands to start, stop, pause and stop DVDs, there are commands for the arrow keys, the return key and ability to go to any of the menus found on a disc. The following properties allow for gathering information about the current activity of the player:

has media - allows you to determine whether a disc has been inserted.
active dvd menu - allows you to determine the current state of the DVD menu (none/main/title/subpicture/audio/angle)
dvd state - allows you to determine what the DVD is doing (playing/playing still/paused/stopped/scanning/idle)

Also included are properties that gather information about the status of the controller window of the player such as its visibility, position and the state of its drawer. The info window of the player has been enhanced in this version to allow the user to change the way it looks and the script allows one to automate this process and remember favored configurations. The viewer window is similarly controllable: A scripter can gather information about the time length of a title and current time position. The audio can be muted and changed with the appropriate commands. A script is also allowed to count the number of available angles, audio tracks, chapters, and subtitles.

Then the question emerges: what can be done with these scripting commands? The example scripts provided give some clues. The Preferred Playback script is run when one has configured the layout of the various windows in a particular way. The script will remember all the settings and positions and restore them when it is run again. The Go to Chapter script can remember a chapter and then when it is run again will return to that chapter. This is somewhat of a "bookmarking" capability that attempts to make up for, albeit feebly, a feature that is lacking in Apple's DVD Player that is common on equivalent software for PCs. Similarly, the Go to Time script allows one to mark a particular time in a movie and then return to it.

One of the powers of AppleScript is the ability to save a script as an application that can be launched and run like any other program on the computer. At DVD Player there are several examples of these programs. Preview Mode allows the user to set a number of seconds and then plays that amount of time from each chapter in a movie. Loop Movie runs and causes a move to start over at the end. The script runs as an application and contains a special function that sends queries to the DVD Player at regular time intervals and then acts on the response. For example, the Loop Movie script asks the player if the movie has ended every minute and then when the response is yes, it tells the player to start the movie again.

These scripts are editable and can be opened in the script editor that comes with the Macintosh and the code can be examined and altered. I took the Loop Movie application and changed its command to restart the movie into a command to stop the DVD. Often I put in a disc late at night and sometimes fall asleep while watching it. Many discs will return to their main menu when a movie ends and these menus often include very annoying sound loops. It is very disturbing to awaken after hearing some snippet of film score looped a hundred times! This script runs in the background and checks every minute to see if the movie is still playing. When it finds that the end has been reached, the player is stopped and the application quits. A blessed event.

One potential use is to create a series of commands that gather the available information about a disc and creating a record that is a foundation of a database about a DVD collection. Ultimately, when it exists, an online DVD database can be connected to in the way that CD players query the CDDB or FreeDB, and song lists can be obtained as well. At this time, one would have to type most of the information along with what can be gathered by AppleScript and insert into a Microsoft Excel spreadsheet, an AppleWorks spreadsheet or a text file. One could keep a record of the "bookmarks" that have been recorded and launch the player and go to that point in time straight from the database. The record in the database can include the chapter names and allow for quick access to favorite sections of films and other DVD materials. This would be especially useful for informational discs.

One often-annoying aspect of DVDs is that the menu design requires you to click through multiple screens to get what you want, especially in the case of getting to a particular chapter. The following script is made for use with The Criterion Collection's Spartacus release. Although I like the Overture that plays Alex North's score for 3m:16s and I enjoy the titles by Saul Bass that play for 3m:12s, I don't necessarily need to see them each time I watch the film. In order to skip those I would have to go to the Chapters menu and then choose Chapter 3. Instead, I run this script from the script menu after putting in the disc and it takes me right to the beginning of the film.

tell application "DVD Player"
set title to 1
set chapter to 3
play dvd
end tell

One of the sample scripts provided with the player is "Go to Chapter..." which pops up a dialog with a list of available chapters by number and allows a user to choose a chapter to start playing. This can be less than satisfactory since one must remember the number of the chapter to choose; a movie like Spartacus can have 47 chapter stops. I wanted to create a script that allowed me to choose by chapter title. Not wanting to mess with typing in titles for that many chapter stops, I put in Star Trek: The Original Series Volume #13. One of the annoying things about the Star Trek discs is that first, you get a menu that shows the two episodes and then you have to click "play" to start the episode. A small thing I grant you, but still a pain. I opened the "Go to Chapter..." script in the Script Editor and added the following list of chapter titles:

property chapTitles : {"Schmitter", "Butchering Monster", "A Wounded Animal", "The Creature Must Die", "No Kill I", "Mind-Meld", "Make An Agreement"}

I then altered the code to use this list to generate the dialogue of chapter selections to choose from when the script runs:

set listResult to choose from list chapTitles with prompt "Select Chapter Name:"

To get the number necessary, I looped through the chapter titles and got the index number of the item that matched my selection.

set selectedChapter to listResult as string
repeat with i from 1 to count of chapTitles
if selectedChapter is equal to item i of chapTitles then
set selectedChapter to i
end if
end repeat

One last small change was to insert this property setting to alert the player that the second title was the active title when creating the list of chapters.

set title to 2

The result is that I can put this disc in the player, run the script and then choose what chapter to go to by name, thus bypassing the menu screens.

Similarly, a particular desire of mine has been the ability to program a DVD to play the chapters that I want or even play a series of time segments that I designate. A script can be created that allows for this very thing. One could essentially re-edit a movie to play as one would want—to a degree, anyway. Sometimes, a disc contains the original movie as well as some deleted scenes in the "extras" section. A script could insert those deleted scenes into the playback. Conversely, in those cases where additional scenes have been added to a movie, a script could be created that bypasses this added material and plays the movie as it was originally released.

Another interesting aspect of using AppleScript to control the DVD Player is the ability to send commends without making it the active application. MacOS X has excellent memory handling, which allows the DVD Player to play without stuttering when one is working in another application. Right now, the Star Trek disc I was scripting is playing as I type this in Microsoft Word, an application that is a known processor hog. Although Word does not have a script menu, it does have the capability of creating macros that run scripts. So for example, if the phone rang while I was typing in Word and a DVD was playing, I could click on a button in a toolbar and tell the player to pause or mute without switching to the application.

By clicking on Tools > Macro > Macros... I opened the Visual Basic Editor and created three macros (Play, Pause, Stop) to use in controlling the DVD player.

Sub PlayerPause()
MacScript ("tell application" & Chr(34) & "DVD Player" & Chr(34) & Chr(13) & "pause dvd" & Chr(13) & "end tell")
End Sub
Sub PlayerStart()
MacScript ("tell application" & Chr(34) & "DVD Player" & Chr(34) & Chr(13) & "play dvd" & Chr(13) & "end tell")
End Sub
Sub PlayerStop()
MacScript ("tell application" & Chr(34) & "DVD Player" & Chr(34) & Chr(13) & "stop dvd" & Chr(13) & "end tell")
End Sub

Then I clicked on Tools > Customize... and created a new toolbar called "DVD Player" and added each Macro to it and gave each a custom icon. Now, as I type in Word, I have the DVD Player in its smallest size visible in one corner of the screen and a floating toolbar that allows me to control it without switching applications. By creating macros that control viewer position, access the menu and control the keys, it becomes a pretty full-featured DVD navigation device. (Great for writing reviews of discs.) Now we are having some fun! This can be accomplished with the Script Menu Apple provides as a special place to run compiled scripts from any application. I can put my pause script in the Script Menu and wherever I happen to be working right then, I can run the script and pause the DVD without switching applications.

And, in the mode of Steve Jobs' famous "And one more thing..." bits at MacWorld, you can use scripts like this to issue voice commands to your Macintosh. With Speech Recognition enabled, I can place the script in the Speakable Items folder with a name like "Pause Player" and then say it into the microphone. Yep, you guessed it... the Macintosh will pause the player!

One of the most intriguing aspects of the computer is the control that it gives its user over media and materials. On the Macintosh, the use of AppleScript gives interested users unprecedented control of the workings of their applications and the way they operate. It allows the creation of super applications that utilize the specialized tools of different programs to accomplish complex tasks.

When one begins scripting small tasks, these elements can be combined into more complex operations that involve multiple applications. But, it doesn't have to be that way. Many simple tasks can make the computer easier to use. A small script that does one thing well can be a powerful tool. However, for the enterprising scripter, the new AppleScript Studio takes this even further by allowing for the creation of graphic user interfaces that control multiple scripts. An example of a potential application would be one that lists DVDs. When a title is clicked, a window opens allowing for setting of the various elements of a DVD's playback including screen size, visibility of controller and info, chapter displayed, audio channel set and whether subtitles are visible from one window and so on. In fact, I have a headstart on just that thing.

You can download several editable AppleScripts at the Studio Log Gallery including DVD Stopper and A.I. Artificial Intelligence Chapter Selector. The first is as described above; the second includes the capability of selecting chapters by name for the DVD version of A.I. Artificial Intelligence.