Re: Shell-Perl-Applescript
Re: Shell-Perl-Applescript
- Subject: Re: Shell-Perl-Applescript
- From: John Fowler <email@hidden>
- Date: Sun, 4 Jan 2004 16:14:10 -0600
Just in case anybody was waiting with 'bated breath (which I doubt),
here is the best I've been able to come up with as a perl
find-and-replace handler for AppleScript. If I use handler
"perlregexreplace" in handler "convertFolder" I get a stack overflow
due to recursive operations on the temp file regexreplacetempfile.txt.
Since the one-line path to the file theFile is unlikely to exceed size
limits for an inline input variable, I am using the older version
"perlreplace" in convertFolder. Although I know some will say it is a
long way to go around the block to get next door, this currently meets
my need to replace BBEdit grep methods with non-proprietary
find-and-replace capabilities in perl.
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)
try
set shellscript to "/usr/bin/perl -0777e 'open (INFILE,\"" & theFile
& "\")||die (\"here\");$thisvar=<INFILE>;$thisvar=~s/" & targetstring &
"/" & replacementstring & "/sg;print $thisvar;'"
set theResult to (do shell script shellscript)
return theResult
on error thiserror
return thiserror
end try
end perlregexreplace
on writeOutputFile(theText, theFile)
set openedFile to ""
try
set openedFile to open for access theFile with write permission
set eof openedFile to 0
write theText to openedFile
close access openedFile
on error
if openedFile is not "" then
close access openedFile
end if
end try
return theFile
end writeOutputFile
on convertFolder(AplFolderString)
set unixFolderString to perlreplace((item 2 of
getEveryTextItem(AplFolderString, "Macintosh HD:")), ":", "/")
log "unixFolderString: " & unixFolderString
return unixFolderString
end convertFolder
on perlreplace(inputstring, targetstring, replacementstring)
log ("/usr/bin/perl -e '$thisvar=" & inputstring & ";$thisvar=~s/" &
targetstring & "/" & replacementstring & "/;print $thisvar;'")
set theResult to (do shell script "/usr/bin/perl -e '$thisvar=\"" &
inputstring & "\";$thisvar=~s/\\" & targetstring & "/\\" &
replacementstring & "/g;print $thisvar;'")
end perlreplace
on getEveryTextItem(thestring, thedelimiter)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to thedelimiter
set theVarList to every text item of thestring
set AppleScript's text item delimiters to oldDelims
--display dialog (theVarList as text)
return theVarList
end getEveryTextItem
_______________________________________________
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.