Re: Scripting Additions: Embracing the Horror of Unix
Re: Scripting Additions: Embracing the Horror of Unix
- Subject: Re: Scripting Additions: Embracing the Horror of Unix
- From: Arthur J Knapp <email@hidden>
- Date: Sat, 02 Feb 2002 13:15:13 -0500
>
Date: Fri, 1 Feb 2002 18:09:58 -0800 (PST)
>
From: "L. Dubb" <email@hidden>
>
Subject: Re: Scripting Additions: Embracing the Horror of Unix
>
RegEx was originally my attempt to cope on a Mac without 'grep' or
>
'sed'. So it would be slightly ironic to convert them for use on a
>
system which already has those commands.
>
Not that I'm suggesting anyone learn sed -- Perl or Python would make
>
much more sense.
>
But the main limitation for me is that I don't have an OS X Mac -- That
>
makes coding and testing pretty difficult.
JavaScript OSA's RegExp object is based on Perl's implementation
of regular expressions, and you can use JSOSA whether or not you
have gone over to OS X.
<
http://www.latenightsw.com/freeware/JavaScriptOSA/>
There is a very simple way to make use of JavaScript from inside
an AppleScript: create a JSOSA evaluator script, and save it as
a stay-open applet:
// file "run_source.jss"
// As an AppleEvent handler, it is best for the
// function name to be all lowercase:
//
function run_source( src )
{
return eval( src );
}
In your AppleScript, treat it as an application:
tell application "run_source.jss"
run_source("1 + 1")
end tell
Here is a more useful example:
-- Validate a US telephone number:
display dialog "Phone Number:" default answer ""
set USPhone to text returned of result
-- Construct: "re = \(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;"
--
set src to "re = /" -- begin regexp source code
set src to src & "\\(?" -- 0 or 1 open parenthesis
set src to src & "\\d{3}" -- 3 digits, (area code)
set src to src & "\\)?" -- 0 or 1 close parenthesis
set src to src & "([-\\/\\. ])" -- 1 dash, slash, decimal, or space
set src to src & "\\d{3}" -- first 3 digits of local number
set src to src & "\\1" -- back-reference to "([-\\/\\. ])"
set src to src & "\\d{4}" -- last 4 digits of local number
set src to src & "/; " -- end code for regexp
-- Construct: 're.test("default answer...");'
--
set src to src & "re.test(" -- begin regexp.test() command
set src to src & "\"" & USPhone & "\"" -- quote the parameter
set src to src & "); " -- last line executed is returned
tell application "run_source.jss"
set IsUSPhone to run_source(src)
end tell
if (IsUSPhone) then
display dialog "Input was a valid US phone number."
else
display dialog "Input was NOT a valid US phone number."
end if
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www3.sympatico.ca/victor.yee/>
on error number -128
end try
}