The AppleScript programming language can be used to send send apple
events to applications running under Mac OS X. These events can be
used to programmatically control the applications, enabling you to do
the GUI equivalent of chaining applications together like you would do
for non-GUI applications with the shell.

AppleScript was only ever designed to be one language that can be
used to send apple events - one with a particularly baroque syntax.
Mac::Glue allows you to send the same events from Perl, with a
similar (but less...well, confusing) syntax.

We're going to create a little Perl application that we can link from
a Finder window that when executed will open a Terminal in the same
location.

Before you can do anything with Mac::Glue you need to create a
binding for the application that you want to control. This enables
Perl to translate the commands you pass to the correct binary signals
to send. This can be done with the gluemac command that's installed
when you install Mac::Glue

bash$ sudo gluemac /Applications/Utilities/Terminal.app
What is the glue name? [Terminal]:
This creates a file containing the binding:

/Library/Perl/5.8.1/Mac/Glue/glues/Terminal

It also creates a bunch of documentation in the same directory:

/Library/Perl/5.8.1/Mac/Glue/glues/Terminal.pod

This is the same documentation that's available from the Open
Dictionary command in the Script Editor application, except
(obviously) how to send the events from Perl rather from AppleScript.
You can read either, whichever makes the most sense to you. Translating
between to the two is relatively straight forward.

tell application "Finder"
activate
set path to URL of target of front window
end tell

Let's look at the Perl code line by line. First we need to create a
finder object that we can call methods on.

my $finder = Mac::Glue->new("Finder");

We now want to construct a command that we'll send across the wire
later to request what we want to know. We grab the first window from
the finder (which will be the top one, so the one that's showing) and
then get the object that represents the directory that's showing in the
window, and then get the path of that directory.

Making Scripts Into Applications

Converting a script into an application that we can simply double
click in the Finder is actually quite complicated and requires quite a
lot of files to be created.

Luckily, many Mac OS X applications - like Platypus - that can create
.app Applications from a script. They pop up dialogs that ask you
a few questions - the name of the script, what icon you want - and then
create all the files needed from templates. Almost no thought needed.