How to pass an AppleScript variable to Perl script? I have a Perl script that does some text transformation on a given file. 

Now I would like to call this script from an AppleScript which in turn I could execute from the script menu in order to run the Perl script on a set of files with a choose folder prompt.  My Perl script starts like this: 

open my $in, '&lt;', 'myfile.txt' or die "No input: $!";
open my $out, '&gt;', 'myfile-modified.txt';


And then it does a bunch of search-and-replace and what not.  

I would like to somehow call this script from an AppleScript and process multiple files. The AppleScript should look more or less like this: 

set myFolder to choose folder with prompt "Choose a folder:"
tell application "Finder"
    try
        set myFiles to (every file in entire contents of myFolder whose name ends with ".txt") as alias list
    on error
        try
            set myFiles to ((every file in entire contents of myFolder whose name ends with ".txt") as alias) as list
        on error
            set myFiles to {}
            display dialog "No files in folder."
        end try
    end try
end tell


How can I run the Perl script from AppleScript while passing myFiles as a variable to it (the perl script) and looping through them?  I assume I have to do a do shell script command but I don't know how to pass the variables on.

I also don't know whether it'd be better to pass the files as a file list to perl or to somehow put that into a repeat loop within AppleScript?
The maximum length of a command line is 2**18 bytes:

$ getconf ARG_MAX
262144


So that for example this results in an error:

read "/usr/share/dict/web2" for 270000
do shell script "printf %s " &amp; quoted form of result


If the input is short enough, you can pass it to perl as part of the command line:

read "/usr/share/dict/web2" for 260000
do shell script "printf %s\\\\n " &amp; quoted form of result &amp; "|perl -pe'$_=uc'"


Otherwise you can use a temporary file:

set input to read "/usr/share/dict/web2" for 270000
set f to (system attribute "TMPDIR") &amp; "my.tempfile"
set fd to open for access f with write permission
set eof fd to 0
write input to fd as «class utf8»
close access fd
do shell script "perl -pe'$_=uc' " &amp; f &amp; ";rm " &amp; f


The default text encoding is still an encoding like MacRoman or MacJapanese, so that adding as «class utf8» to the write command preserves non-ASCII characters, even though it is not necessary in the example above.

In your case you could also do something like this:

quoted form of POSIX path of (choose folder)
do shell script "find " &amp; result &amp; " -name \\*.txt|perl -pe'$_=uc'"


quoted form of escapes text for shells, so that it replaces ' with '\'' and surrounds text with single quotes.