Re: Shell-Perl-Applescript
Re: Shell-Perl-Applescript
- Subject: Re: Shell-Perl-Applescript
- From: Christopher Nebel <email@hidden>
- Date: Sun, 4 Jan 2004 16:33:15 -0800
On Jan 4, 2004, at 2:14 PM, John Fowler wrote:
Just in case anybody was waiting with 'bated breath (which I doubt) ...
Wow, someone who not only knows how to spell "bated" correctly, but
also proves by punctuation that he knows where it comes from! (The
apostrophe isn't necessary; "bated" is a word in its own right, though
it derives from "abated.") I don't suppose you're any relation to H.
W. Fowler?
here is the best I've been able to come up with as a perl
find-and-replace handler for AppleScript.
Not bad, but a few quibbles:
on perlregexreplace(inputstring, targetstring, replacementstring)
set theFile to "Macintosh
HD:Users:johnfowler:Library:jwffolder:regexreplacetempfile.txt"
writeOutputFile(inputstring, theFile)
--delay 30
set theFile to convertFolder(theFile)
Converting from HFS to POSIX paths like this is a very bad idea -- it
won't work on files that aren't on the startup disk, for one thing.
Say 'set theFile to POSIX path of file theFile' instead.
try
set shellscript to "/usr/bin/perl -0777e 'open (INFILE,\"" & theFile
& "\")||die (\"here\");$thisvar=<INFILE>;$thisvar=~s/" & targetstring &
"/" & replacementstring & "/sg;print $thisvar;'"
This works, but I'm not fond of doing my own I/O in Perl. (I know JD
is, though his reasons escape me. Maybe something to do with
portability, or having learned Perl on a non-Unix system.) The
following would be equivalent:
set shellscript to "/usr/bin/perl -pe 's/" & targetstring & "/" &
replacementstring & "/g'" & theFile
Well, mostly equivalent -- this one won't match multi-line target
strings, since it only processes one line at a time. (To do that,
you're more or less obliged to read the whole file at once.) Notice
that we're passing the file as an argument to perl and reading it off
stdin, and exploiting the -p switch to make it read in the file and
write it out for us. Your use of -0 is unnecessary, since '$x =
<FILE>' will suck in the entire file no matter what the record
separator is.
--Chris Nebel
AppleScript Engineering
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.