8.3. Scripting QuickTime
Once you're tired of still graphics, music, and
voice, you've got one multimedia medium left to
cover: video. Apple's powerful video technology,
QuickTime, comes preinstalled on Mac OS X, and you can access all its
power with QuickTime Player (stored in your Applications folder).
Double-click just about any movie on your hard drive, and QuickTime
Player promptly launches.
|
QuickTime Player can open more than just movies. For example,
you can use QuickTime Player with music files, still pictures, and
even plain text files. Still, QuickTime Player isn't
the best program for handling those
formatsthat's why you've got
iTunes, iPhoto, and TextEdit, for example.
|
|
Even beyond the capabilities of QuickTime Player, you can use
AppleScript to unlock additional multimedia
features. For example, AppleScript lets you play videos full-screen
(see below)a stunt that's normally reserved
for the $30 QuickTime Pro.
8.3.1. Getting Started
As a budding AppleScripter, you should take some time to meet
QuickTime
Playerin part, because there are more than 100 million copies
of QuickTime in the world! QuickTime is available for both Macs and
Windows PCseven though the AppleScript features work only with
the Mac version.
If you want to play with QuickTime Player, though, you first need a
movie to use. Chances are you've got a few lying
around on your hard drivejust do a search in the Finder for
files that have an extension of .mov. Or, if you'd
prefer, you can download a folder of QuickTime demo scripts from
www.apple.com/applescript/quicktime/,
and included with those scripts are a few demo
movies.
Once you've got a movie you like, double-click to
open it in QuickTime Player, and then read on.
8.3.2. Presenting a Movie Full-Screen
One of the neatest tricks QuickTime Player can
perform is showing you a movie
full-screen. Yes, the movie might appear a little grainy from being
enlarged, but it still turns your computer into a mini-cinema. And
after a while, it almost makes you feel guilty for not keeping a
popcorn maker next to your computer.
Once you've got a movie open, the only command you
have to use is
present. That's what turns an
unsuspecting QuickTime movie file into a
full-screen theater
imitation (Figure 8-8).
Here's how you use present:
tell application "QuickTime Player"
activate
present the front movie --This is the important line
end tell
As soon as you click Run, AppleScript brings QuickTime Player forward
and presents the front-most movie in full-screen mode. Unfortunately,
you lose access to the menu bar and Dock while the
movie's playing. Therefore, Table 8-2 shows you how to use the keyboard to navigate
a movie while it's in this mode.
Table 8-2. Useful QuickTime keystrokes
Keystroke
|
What it does
|
---|
Space bar or Return key
|
Pauses the movie (press it a second time to resume)
|
Right arrow/Left arrow
|
When paused, moves the movie one frame forward/back
|
-Right
arrow/-Left arrow
|
Plays the movie in forward/reverse
|
Option-Right arrow/Option-Left arrow
|
Jumps to the end/beginning of the movie
|
Up arrow/Down arrow
|
Increases/decreases the movie's volume
|
Escape key
|
Exits full-screen mode and pauses the movie
|
|
If you'd like, you can play movies in other modes
toolike half-size or double-size. Simply
add the
scale option to the end of your
present
command, like this:
tell application "QuickTime
Player" activate present the front movie scale
half end tell
Mac OS X
still darkens your screen with a black border; the difference is,
your movie won't appear as grainy, since
you're not stretching the image to fill your entire
monitor. (You can replace half with
normal, double, or current,
too, to adjust the scale at which your movie plays.)
|
|
| Figure 8-8. A movie, playing full-screen. If the movie is too wide or narrow for your screen, QuickTime automatically places black bars around the sides. |
|
8.3.3. Including a Movie with Your Script
If you're writing a script that involves playing a
specific movie, it'll work
great on your computer. The trouble is, the script
won't work on other
people's computers unless they, too, have the same
movie stored in the same place on their hard drive.
Luckily, AppleScript lets you include a movie with your script. Then,
when you send your script to your aunt in Beijing, the movie file
goes along for the ride, tooguaranteeing that the script can
find and play the movie. Here's how to pull of this
stunt:
Find the movie you want to include with your
script, and keep its folder open. For the purposes
of this example, suppose the movie is called Poodles.mov
and it's saved in your
Movies folder
(Home Movies). In the Finder, navigate to the script you just
saved. Control-click the script's icon, and from the
shortcut menu, choose Show Package Contents. A new
window opens, letting you navigate into the guts of your
script's application bundle. Inside the new window, open the
Contents
Resources
folder. This is where you can store any files you
want distributed with your script. Copy Poodles.mov into this
new window. The movie is now stored
inside your script. Back in Script Editor, write out the code that
incorporates the movie into your script. Use the keywords
path to me so your script can locate the file
inside your application bundle. Here's one possibility for a script
(it'll play Poodles.mov full-screen): set locOfMovie to (path to me as string) & "Contents:Resources:Poodles.mov"
--locOfMovie is now the file path to your Poodles.mov file
tell application "QuickTime Player"
activate --Bring QuickTime forward
open file locOfMovie --Open Poodles.mov
present the front movie --Play the movie full-screen
end tell
Save your script again. If you
try to run your script from Script Editor, you'll
get an error message, because AppleScript thinks path to
me is referring to Script Editor
itself, not your specific script. To get around
this problem, simply close your Script Editor window and run your
script by double-clicking its icon in the Finder.
8.3.4. Rotating a Movie
In addition to simply showing movies at a different size, QuickTime
can rotate a movieand
it'll play just fine in its rotated state.
It's the perfect solution if you accidentally
videotaped your sister's wedding at a 90-degree
angle.
Here's a script that rotates whatever movie you drag
to the script's icon by
90 degreesand then starts the
footage rolling:
--Part 1:
on run
display dialog "Drag a movie onto my icon to rotate and play it"
end run
--Part 2:
on open selectedItem
if (selectedItem count) > 1 then
display dialog "Sorry, you dragged more than one movie onto my icon."
--Part 3:
else
tell application "QuickTime Player"
activate
open selectedItem
--Part 4:
rotate the front movie by 90
play front movie
end tell
end if
end open
|
Just like the script on Section 7.3.3, this script is
a droplet. That means you have to save it as an
Application before the script will work properly.
|
|
Here's how the code works:
Part 1 runs only if you double-click
the script without dragging a movie to it. In that case,
you'll see a dialog box with instructions for what
to donamely, "drag a movie onto my icon to
rotate or play it." Part 2 (and all the subsequent
parts) runs if you do drag a movie to the
script's icon. First, the script checks whether you
dragged more than one movieif you did, it presents an error
dialog box.
|
This script chooses to work with only one movie because QuickTime
Player can become jerky when playing several movies at once. If you
want to do away with this one-movie limitation, though, just erase
the if statement.
And if you'd like to be able to watch multiple
unrotated movies with this scriptsay, so
you can see several views of your sister's wedding
simultaneouslydelete both the if
statement and the rotate the front movie by 90
line..
|
|
Part 3 brings QuickTime Player
forward, and opens a new window for the movie you dropped on top of
the script's icon. Part 4, finally, rotates your movie
clockwise by 90 degrees and starts playing it (Figure 8-9).
| Figure 8-9. Apple's famous 1984 commercial (http://www.apple.com/hardware/ads/1984/), playing in two different modes. Top: Normally, if you simply double-clicked the movie's icon in the Finder, this is what you'd see. Bottom: However, if you run this script, you'll see your movie play sideways, like this. (For even more fun, use AppleScript to rotate your movies by irregular numbers of degrees96.7, for example.) |
|
|
To rotate your movie counterclockwise, you can use a negative
number with the rotate command (for instance, rotate
the front movie by -45). Alternatively, you can simply use
a positive number greater than 180 (rotate front movie by
315, for example, would accomplish the same thing).
|
|
Beyond these features, QuickTime Player has many other capabilities,
too. Download Apple's well-commented sample scripts
from www.apple.com/applescript/quicktime/ for more
scripting inspiration, and don't forget to check out
QuickTime Player's dictionary
(File Open Dictionary,
Shift--O) for a list of additional AppleScript
commands
Power Users' Clinic DVD Player | If you're one of the lucky Mac owners with a
DVD drive,
you've got a whole set of extra AppleScript commands
at your service. The DVD Player program (in your Applications
folder) has a dictionary that includes such useful commands as
step DVD (for skipping ahead one movie frame at
a time) and go (for quickly jumping to the
DVD's main menu or title screen). The truth is, though, Apple's done most of the hard
work for you. DVD Player includes its own Script menu (Sidebar 1.1), where you can access a bunch of useful,
built-in scripts to control all facets of your DVD watching.
Here's what they do: Go to Time lets you pick a
specific time in the movie to jump to. If you remember that an action
sequence starts exactly 23 minutes and 42 seconds into a movie, for
instance, this command can get you there in a snap. Preferred
Playback is a
shortcut for using Apple's optimum DVD Player
settings. That involves a full-screen DVD window and a horizontal
Controller window, among other minor preferences. Random Info
Color, an utterly useless script, uses
randomly colored text for the Info window, which shows the title and
chapter of the movie you're watching. Show Wide
Info and
Show Standard
Info let you switch between the two display
modes of the Info window: either square with big text (Standard) or
rectangular with small text (Wide). Reset
Windows sets your DVD Player windows to their
factory-fresh size and position.
Finally, the Applets submenu has three extra scripts: Changing Info
Color
continuously changes the text color in the Info window.
It's totally useless but kinda fun to watch. Loop Movie plays your
movieand then plays it over and over again from the beginning
when it finishes playing. You could use this if
you'd made a corporate DVD for a trade show, for
example, but didn't want to hover over your computer
and keep restarting the DVD by hand Preview
Movie plays the first few seconds of each
scene, much like the script in Section 8.2.2.1 does
for iTunes music. If you need to prepare a next-day movie review for
a newspaper article, this trick lets you watch a DVD in only a
fraction of the time that it takes to watch the entire thing.
|
|