On 9/24/02 6:03 PM, "Rob Barris" <[EMAIL PROTECTED]> wrote:

> How would I go about translating either of these scripts into a form
> that I could embed in a Perl program?
> 
> Can I just take the raw text (after having dealt with any variable
> references and moving data between the Perl world and the AS world) and
> run it as shown here in the link below?
> 
> http://search.cpan.org/author/JLABOVITZ/Mac-AppleScript-Glue-0.03/
> Glue.pm
> (under "FUNCTIONS")

I'm the author of that one.

If you're simply wanting to run AppleScript from within your Perl script, I
recommend Mac::AppleScript (that's different than Mac::AppleScript::Glue),
which does what you want quickly.

If you've run AppleScript from your Perl script, but now want to translate
the AppleScript return value to Perl data structures, then you could use the
from_string() function in Mac::AppleScript::Glue to do the hard part of the
parsing.  You could use also run() in that same module, which is just a
combination of calling Mac::AppleScript's run() function and my
from_string() function.

If you want to write Perl *instead* of AppleScript, then my module is for
you.

Here's an example of asking the Finder for the names of all the files on the
desktop:

---cut here---

use Mac::AppleScript qw(RunAppleScript);
use Mac::AppleScript::Glue qw(from_string);

my $script = <<EOF;
tell application "Finder" to get files
EOF

my $as_result = RunAppleScript($script);

my $perl_result = from_string($as_result);

---cut here---

Note that the from_string() function may return scalars, arrays, hashes, or
Mac::AppleScript::Object objects.  The latter may seem weird, but they are
useful when you may refer to those objects later.  So the previous example
will return a list of all the files on your desktop, but those files will be
objects, not simple strings.  If you need to get the string, you can use the
object's ref() method:

    $perl_result->[0]->ref
    # will be something like 'document file "Workflow" of desktop of
application "Finder"'

If you're really wanting to deal with this stuff, though, you'll want to
create an application object before you call from_string():

    my $finder = new Mac::AppleScript::Glue::Application('Finder');

then:

    my $perl_result = from_string($as_result, $finder);

Then you can do such cool things as open the first file of that list:

    $perl_result->[0]->open;

Alternatively, you can just give up on the whole AppleScript thing and write
it in Perl:

    my $finder = new Mac::AppleScript::Glue::Application('Finder');

    my $files = $finder->files;

    my $first_file = $files->[0];

    $first_file->open;

--
John Labovitz
[EMAIL PROTECTED]
www.johnlabovitz.com

Reply via email to