Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Scripting Addition Devs..?

Scripting Addition Devs..?
Thread Tools
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 1, 2001, 08:35 PM
 
Can anyone tell me if this Scripting Addition exists or if someone could develop it..? I want something that can calculate strings. Like:

set theStringToCalc to "1+2-3*32*3+/2342+-32"
calculate theStringToCalc


I mean I know AppleScript can calculate things nativly... but not this can anyone help?
     
Junior Member
Join Date: Apr 2001
Status: Offline
Reply With Quote
Aug 2, 2001, 12:37 AM
 
applescript should be able to handle any basic math function (+,-,/,*,^ (powers)) no matter the complexity of the string without a third party scripting addition so long as the string's set up/defined to generate the correct result. so something like this should work fine:<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set theStringToCalc to <font color = blue>2</font> ^ <font color = blue>11</font> + (<font color = blue>2</font> - (<font color = blue>-399</font> * (<font color = blue>27</font> / (<font color = blue>7</font> * <font color = blue>33</font>))) / <font color = blue>787</font>) - (<font color = blue>23875</font> + <font color = blue>5431</font> / <font color = blue>33</font>) ^ <font color = blue>0.5</font> + (<font color = blue>9000</font> * <font color = blue>79</font>) / <font color = blue>5500</font>
-- <font color = blue>2024.284974232783</font>
set roundstring to round theStringToCalc rounding as taught in school
-- <font color = blue>2024</font></font>[/code]
if you need more than basic functionality (trig functions, complex conversions, etc...), you could either define the function within the script using the basic functions if possible, or try some of the data/math scripting additions at Macscripter.net's fairly comprehensive Scripting Additions section.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 2, 2001, 05:30 AM
 
Originally posted by acur128:
<STRONG>applescript should be able to handle any basic math function (+,-,/,*,^ (powers)) no matter the complexity of the string without a third party scripting addition so long as the string's set up/defined to generate the correct result. </STRONG>
Yes.. but as I said I know it can calculate stuff nativly but not strings. Strings have quotes around them.. *This* will be what I have to work with: "1+2-3*4/234"
Not: 1 + 2 - 3 * 4 / 234

How can I calculate what I have to work with without getting too complex?

set stringToCalc to "1+1*2342-24+242+24324/24242"

Now I just need to calculate it... If you can find a way to easily do something with stringToCalc then please show me.
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Aug 2, 2001, 02:14 PM
 
I don't know of any Scripting Addition that will do this, but here's a pure AppleScript solution that will do.

It's not all that fast, but it does appear to work. It probably needs some error checking added to trap invalid strings (e.g. contains characters other than /, *, -, +, or 0-9) or dual operands (e.g. 10-+2), but I'll leave that to you.

it simply breaks the supplied string into parts at the "/", "*", "+", and "-" characters, based on operand priority of Division, Multiplication, Addition and Subtraction. It then tries to perform the required operation on the two parts of the string, checking that each part of the string is a valid number. If either side isn't a valid number it breaks the string down again, and again until two numbers are obtained.

The code is pretty well commented, so you should be able to follow it.

Argh!

After NUMEROUS attempts at pasting the code into this board, and having UBB trash it every time, I've created a web page containing the code.

Anyone successfully added AppleScript code to a UBB board without it getting munged?

[ 08-02-2001: Message edited by: Camelot ]
Gods don't kill people - people with Gods kill people.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 2, 2001, 06:29 PM
 
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
-- These properties specify the possible actions to take.
-- To add other functions, such as exponention, define it here
-- and add a handler in the DoCalc() and ParseStr() functions.

property doDivide : <font color = red>"/"</font>
property doMultiply : <font color = red>"*"</font>
property doAdd : <font color = red>"+"</font>
property doSubtract : <font color = red>"-"</font>

-- here's our string
set stringToCalc to <font color = red>"<font color = blue>1</font>+<font color = blue>1</font>"</font>

set theResult to parseStr(stringToCalc)
get ((stringToCalc & <font color = red>" = "</font> & theResult) as string)


on doCalc(operation, num1, num2)

-- This function takes an operation and two numbers or strings and calculates the result.

set operand1 to <font color = blue>0</font>
set operand2 to <font color = blue>0</font>

-- Parse the first num to decide <font color = green>if</font> it's a string or a number we can work with

-- Assume we don't have a number
set gotOp1 to false

-- and keep going until we <font color = green>do</font>

repeat until gotOp1 is true

try

-- see <font color = green>if</font> AppleScript can type the parameter to a number
set operand1 to num1 as number

-- <font color = green>if</font> we get here we have a number
set gotOp1 to true

on error
-- <font color = green>if</font> we get here we have a string, so parse the string out again
set num1 to parseStr(num1 as string)
end try
end repeat

-- now, repeat the entire process again <font color = green>for</font> the second operand
set gotOp2 to false

repeat until gotOp2 is true
try

set operand2 to num2 as number
set gotOp2 to true

on error
set num2 to parseStr(num2 as string)
end try
end repeat


-- by the time we get here, we have two numbers (operand1 and operand2)
-- so we can perform the calculation based on the operation parameter

<font color = green>if</font> operation = doDivide then
set theResult to operand1 / operand2
<font color = green>else</font> <font color = green>if</font> operation = doMultiply then
set theResult to operand1 * operand2
<font color = green>else</font> <font color = green>if</font> operation = doAdd then
set theResult to operand1 + operand2
<font color = green>else</font> <font color = green>if</font> operation = doSubtract then
set theResult to operand1 - operand2
<font color = green>else</font>
set theResult to <font color = blue>0</font>
end <font color = green>if</font>

<font color = green>return</font> theResult

end doCalc

on parseStr(theStr)
-- first check to see <font color = green>if</font> the string can be classed as a number

try
set theNum to theStr as number
-- <font color = green>if</font> we get here, AppleScript can convert theStr to a number, so we don't need to <font color = green>do</font> anything
<font color = green>return</font> theNum
on error
-- <font color = green>if</font> we get here, theStr is, indeed, a string
end try


-- first check <font color = green>for</font> division
<font color = green>if</font> class of theStr is string then
-- see <font color = green>if</font> theStr contains a <font color = red>"/"</font> character
set dividePos to offset of <font color = red>"/"</font> in theStr
<font color = green>if</font> dividePos &gt; <font color = blue>0</font> then
-- now split the string at the <font color = red>"/"</font> character
set numerator to characters <font color = blue>1</font> through (dividePos - <font color = blue>1</font>) of theStr as string
set denominator to characters (dividePos + <font color = blue>1</font>) through <font color = blue>-1</font> of theStr as string
-- and calculate the result
set theResult to doCalc(doDivide, numerator, denominator)
end <font color = green>if</font>
end <font color = green>if</font>


-- now check <font color = green>for</font> multiplication
<font color = green>if</font> class of theStr is string then
set multPos to offset of <font color = red>"*"</font> in theStr
<font color = green>if</font> multPos &gt; <font color = blue>0</font> then
set num1 to characters <font color = blue>1</font> through (multPos - <font color = blue>1</font>) of theStr as string
set num2 to characters (multPos + <font color = blue>1</font>) through <font color = blue>-1</font> of theStr as string
set theResult to doCalc(doMultiply, num1, num2)
end <font color = green>if</font>
end <font color = green>if</font>


-- now check <font color = green>for</font> addition
<font color = green>if</font> class of theStr is string then
set addPos to offset of <font color = red>"+"</font> in theStr
<font color = green>if</font> addPos &gt; <font color = blue>0</font> then
set num1 to characters <font color = blue>1</font> through (addPos - <font color = blue>1</font>) of theStr as string
set num2 to characters (addPos + <font color = blue>1</font>) through <font color = blue>-1</font> of theStr as string
set theResult to doCalc(doAdd, num1, num2)
end <font color = green>if</font>
end <font color = green>if</font>


-- now check <font color = green>for</font> subtraction
<font color = green>if</font> class of theStr is string then
set subPos to offset of <font color = red>"-"</font> in theStr
<font color = green>if</font> subPos &gt; <font color = blue>0</font> then
set num1 to characters <font color = blue>1</font> through (subPos - <font color = blue>1</font>) of theStr as string
set num2 to characters (subPos + <font color = blue>1</font>) through <font color = blue>-1</font> of theStr as string
set theResult to doCalc(doSubtract, num1, num2)
end <font color = green>if</font>
end <font color = green>if</font>

<font color = green>return</font> theResult


end parseStr</font>[/code]

Wow, it works. I didn't exactly want it to return as a dialog but I fixed that Thanks alot. I'll add a few things and that get it to do what I want. Again, thank you.

synotic@mac.com
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Aug 2, 2001, 06:51 PM
 
Wow, I'm kinda bummed that you managed to include the code on the board here, but happy that it works for you.

Maybe it's something in the way OnmiWeb handles returns in forms.

Oh well.

Gods don't kill people - people with Gods kill people.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 2, 2001, 08:10 PM
 
Originally posted by Camelot:
<STRONG>Wow, I'm kinda bummed that you managed to include the code on the board here, but happy that it works for you.

Maybe it's something in the way OnmiWeb handles returns in forms.

Oh well.

</STRONG>
I added a few things.. i.e. illegal character scanning and stuff... but I couldn't figure out how to allow ()s... Like

(1+1)*2

...Any ideas?
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Aug 3, 2001, 03:14 AM
 
Originally posted by Synotic:
<STRONG>I added a few things.. i.e. illegal character scanning and stuff... but I couldn't figure out how to allow ()s... Like

(1+1)*2

...Any ideas?</STRONG>
Add the following code to the parseStr() function, BEFORE the division check.

The idea behind this code is that is looks for ( and ) characters in the string. If it finds any it copies out that substring and calculates the contents. It then rebuilds the string substituting the result of the inner calculation in place of the parentheses.

For example, "(10+2)*(20/4)" processes as:

stringToCalc: "(10+2)*(20/4)"
Get the substring: "10+2"
Calculate substring: "12"
Rebuild theStr: "12*(20/4)"
Get the 2nd parens "20/4"
Calculate subsstring "5"
Rebuild theStr: "12*5"
Calculate: "60"

&lt;sigh&gt; same problems pasting AppleScript code in this board. I've updated my web page with an updated version

The code may fail with nested parentheses, e.g. "(10+2)*(2*(30/5))" since I don't match the open/close parentheses pairs. Some better error checking in the 'offset of ")" in theStr' should solve that problem.

[ 08-03-2001: Message edited by: Camelot ]

[ 08-03-2001: Message edited by: Camelot ]
Gods don't kill people - people with Gods kill people.
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Aug 3, 2001, 03:23 AM
 
Originally posted by Camelot:
<STRONG>

Add the following code to the parseStr() function, BEFORE the division check.

The idea behind this code is that is looks for ( and ) characters in the string. If it finds any it copies out that substring and calculates the contents. It then rebuilds the string substituting the result of the inner calculation in place of the parentheses.

For example, "(10+2)*(20/4)" processes as:

stringToCalc: "(10+2)*(20/4)"
Get the substring: "10+2"
Calculate substring: "12"
Rebuild theStr: "12*(20/4)"
Get the 2nd parens "20/4"
Calculate subsstring "5"
Rebuild theStr: "12*5"
Calculate: "60"


&lt;sigh&gt; same problem with pasting code in this board. I've updated the copy on my web page

The code may fail with nested parentheses, e.g. "(10+2)*(2*(30/5))" since I don't match the open/close parentheses pairs. Some better error checking in the 'offset of ")" in theStr' should solve that problem.

[ 08-03-2001: Message edited by: Camelot ]</STRONG>
Gods don't kill people - people with Gods kill people.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 3, 2001, 04:23 AM
 
Did you forget to pay your host? 404...
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Aug 3, 2001, 01:27 PM
 
Originally posted by Synotic:
<STRONG>Did you forget to pay your host? 404...</STRONG>
Bummer.. looks like I typoed the URL. Darned case-sensitive UNIX systems.

Try http://home.talkcity.com/InfiniteLoo...ingToCalc.html
Gods don't kill people - people with Gods kill people.
     
Forum Regular
Join Date: Nov 2000
Location: Menlo Park, CA, USA
Status: Offline
Reply With Quote
Aug 4, 2001, 01:40 AM
 
Interesting code! However, wouldn't this have been easier?:

set theStringToCalc to "1+2-3*32*3+/2342+-32"
run script theStringToCalc


-boo
noticing that +/ is not a valid operation
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Aug 4, 2001, 03:02 AM
 
LOL! That's wonderful. I didn't even think about that one &lt;slaps forehead&gt;!

Oh well, it was an interesting exercise in recursive string parsing, and was fun to do. :-)
Gods don't kill people - people with Gods kill people.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 4, 2001, 03:44 AM
 
Originally posted by Boodlums:
<STRONG>Interesting code! However, wouldn't this have been easier?:

set theStringToCalc to "1+2-3*32*3+/2342+-32"
run script theStringToCalc


-boo
noticing that +/ is not a valid operation</STRONG>
lol, well that is much easier. Oh well. BTW, I am also working on this... but am having troubles.. can anyone help?

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>property HipScriptBangCommandList : {<font color = red>"!roulette"</font>}

on bangcommand(isPrivate, con, source, target, argString, thecmd, theargs)
tell application <font color = red>"ircle"</font>
<font color = green>if</font> thecmd is <font color = red>"!roulette"</font> and target is not <font color = red>"#macintosh"</font> then
set theNumber to item <font color = blue>1</font> of theargs as number
set theColor to item <font color = blue>2</font> of theargs
set theBet to item <font color = blue>3</font> of theargs
set newBet to (hipTranslateSubstrings theBet replacing any of {<font color = red>","</font>, <font color = red>"$"</font>} with corresponding {<font color = red>""</font>, <font color = red>""</font>}) as number
set randNumb to <font color = blue>3</font>
set randBlackRed to <font color = red>"red"</font>
set theMoney to <font color = blue>500</font>
<font color = green>if</font> newBet is less than <font color = blue>50</font> then
type <font color = red>"Sorry, we have a minimum bet of <font color = blue>50</font> dollars here."</font> in channel target of connection con
<font color = green>else</font>
<font color = green>if</font> the newBet is greater than theMoney then
type <font color = red>"Sorry "</font> & source & <font color = red>", you don't have that much money. You only have <font color = blue>500</font> dollars."</font>
<font color = green>else</font>
type <font color = red>"The ball runs around the table...."</font> in channel target of connection con
<font color = green>if</font> randNumb = theNumber then
<font color = green>if</font> randBlackRed = theColor then
InputIn(<font color = red>"<font color = blue>1</font>"</font>, target, <font color = red>"/delay qadd qexec <font color = blue>1</font> Congratulations "</font> & source & <font color = red>", you are our new winner! "</font> & randNumb & <font color = red>" "</font> & randBlackRed & <font color = red>", "</font> & <font color = red>"you won "</font> & newBet & <font color = red>" "</font> & <font color = red>"dollars!"</font>)
<font color = green>else</font>
InputIn(con, target, <font color = red>"/delay qadd qexec <font color = blue>1</font> Sorry the number was "</font> & randNumb & <font color = red>" "</font> & randBlackRed & <font color = red>" you lost "</font> & newBet & <font color = red>" & dollars."</font>)
end <font color = green>if</font>
<font color = green>else</font>
InputIn(con, target, <font color = red>"/delay qadd qexec <font color = blue>1</font> Sorry the number was "</font> & randNumb & <font color = red>" "</font> & randBlackRed & <font color = red>" you lost "</font> & newBet & <font color = red>" dollars."</font>)
end <font color = green>if</font>
end <font color = green>if</font>
end <font color = green>if</font>
end <font color = green>if</font>
end tell
<font color = green>return</font> false
end bangcommand</font>[/code]
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Aug 4, 2001, 01:59 PM
 
Originally posted by Synotic:
[QB]lol, well that is much easier. Oh well. BTW, I am also working on this... but am having troubles.. can anyone help?
Maybe if you could be explicit as to what problems you're having I might be able to help.

Clearly the script compiles OK, so syntactically it is correct. Without knowing the goal or what makes you say it isn't working, though, it's almost impossible to identify the fault.
Gods don't kill people - people with Gods kill people.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 4, 2001, 05:28 PM
 
Yeah.. I know it compiles.. but it just doesn't work. Here is what I'll tyoe in ircle:

!roulette 3 red $340

Then it'll do:

The ball runs around the table...
(1 second delay)
Sorry, the number was 3 red. You lost 340 dollars.


I mean I put like <BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier><font color = green>if</font> randColor is equal to theColor</font>[/code] and it just won't match up.. I tried other veriations.. like: if randColor = theColor or if randColor is theColor.. I just don't know what is wrong....
     
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Aug 5, 2001, 02:31 PM
 
Well, it appears to work fine here, which leads to two possible places where the script could be failing.

Firstly, I don't have hipscript installed, so I commented out that line, making sure I passed in a pure string (e.g. "500") rather than "$500"). So there could be a problem with how you're calling HipScript.

The second possibility is that you're passing the wrong arguments into the script, which is impossible for me to tell since I don't have the rest of your source. One thing I do notice is that you have two parameters that appear to relate to the same thing... argString and theArgs.

I'm guessing that argString contains the whole text as entered in the input window in ircle. However, you totally ignore this parameter in the code.
You *are* passing theArgs as a list, right, and not as a string?

Since you "set theColor to item 2 of theArgs", this will only get 'red' if theArgs is a list like {3, "red", 500}. If theArgs is a string "3 red 500", then item 2 of theArgs will be the second character, i.e. a space, which won't match the randBlackRed variable later in the script.

So, have a look at where you're calling bangcommand() and check if you're passing in a list or a string.

Secondly, the easiest way to test ircle scripts like this is outside of ircle. If you copy the bangcommand() procedure to a new script then add:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
bangcommand(<font color = blue>1</font>, <font color = blue>1</font>, <font color = red>"noone"</font>, <font color = red>"#here"</font>, <font color = red>"<font color = blue>3</font> red <font color = blue>500</font>"</font>, <font color = red>"!roulette"</font>, {<font color = red>"<font color = blue>3</font>"</font>, <font color = red>"red"</font>, <font color = red>"<font color = blue>500</font>"</font>})</font>[/code]

You can run the entire script in your Script Editor which makes life a little easier. Once fixed, you can copy the procedure back into your ircle script.
Gods don't kill people - people with Gods kill people.
     
Synotic  (op)
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Aug 5, 2001, 10:05 PM
 
Well, it appears to work fine here, which leads to two possible places where the script could be failing.

Firstly, I don't have hipscript installed, so I commented out that line, making sure I passed in a pure string (e.g. "500") rather than "$500"). So there could be a problem with how you're calling HipScript.
&gt;&gt; I fixed the money problem already..

The second possibility is that you're passing the wrong arguments into the script, which is impossible for me to tell since I don't have the rest of your source. One thing I do notice is that you have two parameters that appear to relate to the same thing... argString and theArgs.

I'm guessing that argString contains the whole text as entered in the input window in ircle. However, you totally ignore this parameter in the code.
You *are* passing theArgs as a list, right, and not as a string?

&gt;&gt;theargs is everything AFTER the !roulette part as a list:
&gt;&gt;So here is the string: !roulette 3 red 400

&gt;&gt;theargs is {"3", "red", "400"}

Since you "set theColor to item 2 of theArgs", this will only get 'red' if theArgs is a list like {3, "red", 500}.
&gt;&gt;Yep, that is how it works.

If theArgs is a string "3 red 500", then item 2 of theArgs will be the second character, i.e. a space, which won't match the randBlackRed variable later in the script.
&gt;&gt;Nope, read above.

So, have a look at where you're calling bangcommand() and check if you're passing in a list or a string.

Secondly, the easiest way to test ircle scripts like this is outside of ircle. If you copy the bangcommand() procedure to a new script then add:


code:
------------------------------------------------------------------------


bangcommand(1, 1, "noone", "#here", "3 red 500", "!roulette", {"3", "red", "500"})
------------------------------------------------------------------------

You can run the entire script in your Script Editor which makes life a little easier. Once fixed, you can copy the procedure back into your ircle script.

&gt;&gt;Bang commands are part of scripting for HipScript. Like.. you add a list of all the commands:

hipBangCommands : {"!roulette", "!money"} whatever..

and then you'll do like, if thecmd is "!roulette" then

I don't think that I can edit this handler because it is called upon HipScript. I am making a plugin for HipScript... So if I have the hipBangcommands property then it will search for and any alterations might make it go haywire.. Oh and the hipTranslateSubstrings is from a scripting addition called HipTools. It is for replacing certain items with other items... I know how to do it without a scripting addition but for now it will do.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:09 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2