[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Calling Perl script from AppleScript/"run script" osax




Well, I admit it, I've been asking this before, but I didn't get a solution.

I want to run a Perl script from AppleScript and I very much prefer to use
the OSA component for this instead of the application. The scripts are very
simple and don't use any of the libraries. All I want to do is extract
matching text from files and replace strings in files. That means that my
Perl scripts are at most 10 lines long.

As it doesn't seem to be possible to call subroutines in a compiled perl
script from AppleScript I tried to make up the scripts on the fly and call
them with "run script". The result always was a visit to MacsBug.

Here's an example:


property _head : "
    open(INFILE, \"<$infile\") || die;
    open(WORKFILE, \">$workfile\") || die;

LINE:
    while ($ln = <INFILE>) {
        chop;
"
property _trail : "
        print WORKFILE $ln;
    }
close INFILE;
close WORKFILE;
"

on multireplace(inFile, oldStrings, newStrings)

    copy "Diskus:work:test:test.out" to workFile

    copy "$infile= \"" & (inFile as string) & "\";" & return & ,
        "$workfile= \"" & (workFile as string) & "\";" & return to _files

    copy "" to _replacements
    repeat with i from 1 to (length of oldStrings)
        copy item i of oldStrings to os
        copy item i of newStrings to ns
        copy _replacements & ("$ln=~sto _replacements
        -- the illegible chars above are bullets
    end repeat

    copy _files & _head & _replacements & _trail to sc

    run script sc in "Perl"

end multireplace

multireplace(alias "Diskus:work:test:test.in", {"old1", "old2"}, {"new1",
"new2"})



Am I doing something wrong?

TIA
Michael