At 12:56 am +0000 17/1/04, I wrote:


However the applet method is very fast. I need to work out the semaphore bit because the Finder is too slow to update and you can't rely on modification times.

I have now got the backgrounded applet and the perl subroutine working predictably and consistently. Using the script below


tell app "Eudora"
        get whole text of front message
end

I am getting results of 0.66 seconds consistently. In case anyone's interested I print the routine below with apologies for my bad perl. The stay-open applet, whose script I omit here, gets the open command from the shell and reads the AS script from $fin. It then does 'run script _script', that is it compiles and runs the script, and writes the output to $fout and "OK" to $semaphore. DOAS loops until $semaphore gives the OK and then reads $fout and prints the output.


use strict; use Time::HiRes qw( gettimeofday ); my $as = <<_______AS; --quit tell app "Eudora" get whole text of front message end _______AS my $start = gettimeofday; my $result = DOAS( $as, 3 ); $result =~ s~\r\n?~\n~g ; my $timetaken = gettimeofday - $start; print "$result\n\t$timetaken\n";

sub DOAS {
    my ( $changed, $fin, $fout, $semaphore, @stat, $t, $time, $timeout );

$timeout = 5;

    my $tmp = "/tmp";
    ( $fin, $fout, $semaphore ) = split /\s/,
      "$tmp/pldfin $tmp/pldfout $tmp/pldsem";
    open FOUT, ">$fout";
    close FOUT;

    # Clear the semaphore file
    open F, ">$semaphore";
    close F;

    # Write the script to fin
    open FIN, ">$fin";
    print FIN $_[0];
    `open -a pld $fin`;

    # Don't bother to continue if the quit command was sent
    $_[0] =~ /^quit/i and return "pld quit at first word in \$as";

    # Wait for the semaphore file to signal "OK"
    $time    = time;
    $changed = 0;
    while ( $changed == 0 ) {
        $t = time;
        open F, $semaphore;
        <F> and $changed = 1;
        close F;
        if ( $t > ( $time + $timeout ) ) {
            print "Timed out after $timeout seconds.\n";
            exit;
        }
    }

    # All clear now to read the output
    open FOUT, $fout;
    $result = <FOUT> . $/;
}





Reply via email to