Re: returning math answers given a string
Re: returning math answers given a string
- Subject: Re: returning math answers given a string
- From: John Stewart <email@hidden>
- Date: Sat, 25 Jan 2003 16:59:33 -0500
On Saturday, January 25, 2003, at 01:41 PM, Michael Glasser wrote:
Is there any way to have AppleScipt read a text message such as "what
is 3 + 3" and have it calculate the math so it can return the answer
(6, I believe. :) )
This works -
set txtmsg to "3 + 3"
run script txtmsg
--> 6
I would also like it to be able to do other math operations, and
recognize other terms like "plus" instead of "+".
It can be done, see below
Anyone ever do this before? Is there a semi-simple / efficient way?
Yes, it's simple but I'm not too sure I'd consider it very efficient.
If I need to have it parse the text in some clever way that is fine, I
do not need it to do large numbers... single digit would be good,
double digit would be great, triple would be overkill.
Thanks in advance
Then this or something similar is what you need to do. It doesn't
particularly care about number length unless you exceed AppleScript's
limits, with 3 digit numbers that shouldn't be a factor.
set txtmsg to "Thirteen times Thirteen"
set theInterimResult to my doRplc(txtmsg, "times", "*")
set theInterimResult to my doRplc(theInterimResult, "Thirteen", "13")
run script theInterimResult
--> 169
on doRplc(txtStr, srchStr, rplcStr)
set {oldDelims, AppleScript's text item delimiters} to {AppleScript's
text item delimiters, {srchStr}}
set temp to every text item of txtStr
set AppleScript's text item delimiters to {rplcStr}
set txtStr to temp as text
set AppleScript's text item delimiters to oldDelims
return txtStr
end doRplc
John
_______________________________________________
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.