Re: BIG number as string
Re: BIG number as string
- Subject: Re: BIG number as string
- From: Paul Berkowitz <email@hidden>
- Date: Sat, 21 Dec 2002 15:10:50 -0800
On 12/21/02 1:43 PM, "John W Baxter" <email@hidden> wrote:
>
OK, here's a simple proof-of-concept of an approach very different from
>
those proposed. Useful for Mac OS X, recent enough to have shipped with
>
Python, or with Python installed by you in command line form.
>
>
-- Pick a path name, and get the forms we need--better duplicate prevention
>
desired
>
set fn to (path to temporary items folder as string) & "xxxx.py"
>
set fnp to quoted form of POSIX path of fn
>
>
-- Unix end of line
>
set newline to ASCII character of 10
>
>
-- Create a temporary file and write a Python program into it
>
set f to open for access file fn with write permission
>
write "print 55555 * 55555" & newline to f
>
close access f
>
>
-- Run the Python program and delete it
>
set ans to do shell script "/usr/bin/python " & fnp
>
do shell script "rm " & fnp
>
>
-- -ans is a string with the answer
>
ans
>
>
>
Clearly, the thing has to be parameterized one way or another (either write
>
a permanent Python script which takes command line input, or write a
>
handler which writes the needed Python script, or something which does the
>
parameterization another way), although for a few quickies just changing
>
the AppleScript script is enough.
Very good, John.
At least:
-- Create a temporary file and write a Python program into it
set f to open for access file fn with write permission
set eof f to 0 -- just in case there's one there already
write ("print 55555 * 55555" & newline) to f
close access f
-- Run the Python program and delete it
set ans to do shell script "python " & fnp
do shell script "rm " & fnp
Although, as you say, it would be better as a handler with two parameters as
operands:
on PythonMultiply(number1, number2)
try
--works for string numbers too, not other strings
if not ({class of (number1 as number)} is in {integer, real} and
{class of (number2 as number)} is in {integer, real}) then error number -128
on error
display dialog "This routine works only for two numbers." buttons
{"OK"} default button 1 with icon 0
error number -128
end try
set newline to ASCII character of 10
set fn to (path to temporary items folder as string) & "xxxx.py"
set fnp to quoted form of POSIX path of fn
set f to open for access file fn with write permission
set eof f to 0
write ("print " & number1 & " * " & number2 & newline) to f
close access f
set ans to do shell script "python " & fnp
--do shell script "rm " & fnp -- not really needed with the eof 0 line
return ans
end PythonMultiply
PythonMultiply(12345678, 87654321)
-->"1082152022374638"
--
Paul Berkowitz
_______________________________________________
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.