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, '<', 'myfile.txt' or die "No input: $!";
open my $out, '>', '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?
Best Answer
The maximum length of a command line is
2**18
bytes:So that for example this results in an error:
If the input is short enough, you can pass it to perl as part of the command line:
Otherwise you can use a temporary file:
The default text encoding is still an encoding like MacRoman or MacJapanese, so that adding
as «class utf8»
to thewrite
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
escapes text for shells, so that it replaces'
with'\''
and surrounds text with single quotes.