Finalizing Script
Finalizing Script
- Subject: Finalizing Script
- From: Greg Back <email@hidden>
- Date: Wed, 28 Aug 2002 13:44:04 -0400
Hello
I have written the following script, and am planning to post it to
applemods in the next couple of days. I was wondering if any of you
scripting experts had any tips for optimizing or simplifying the code.
Thanks in advance for all help.
======================================================================
script BaseLib
property charString : "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
on ConvertBase(num, startingBase, endingBase)
set numStr to num as string
ValidateBase(numStr, startingBase)
set {l, m} to {0, 0}
set text item delimiters to "."
set theNum to text items of numStr
set wholePart to item 1 of theNum
set fractionPart to ".0"
try
set fractionPart to "." & item 2 of theNum
end try
set text item delimiters to ""
set wFlag to (wholePart [NOT-EQUAL] "0")
set fFlag to (fractionPart [NOT-EQUAL] ".0")
set l to length of wholePart
set m to length of fractionPart
set {wholenum, fractionNum} to {0, 0}
if wFlag then
repeat with i from 1 to l
set wholenum to wholenum + ((offset of (character i of
[NO-BREAK]wholePart) in charString) - 1) * (startingBase ^ (l - i))
end repeat
end if
if fFlag then
repeat with i from 2 to m
set fractionNum to fractionNum + ((offset of (character i
[NO-BREAK]of fractionPart) in charString) - 1) / (startingBase ^ (i -
[NO-BREAK]1))
end repeat
end if
set {wholePart, fractionPart} to {"", ""}
if wFlag then
repeat 1000 times
set a to wholenum / endingBase
set ipart to a div 1
set fpart to (wholenum - (ipart * endingBase)) div 1
set wholePart to character (fpart + 1) of charString &
[NO-BREAK]wholePart
set wholenum to ipart
if ipart = 0 then exit repeat
end repeat
end if
if fFlag then
repeat 10 times
set b to fractionNum * endingBase
set ipart to b div 1
set fpart to b - ipart
set fractionPart to fractionPart & character (ipart + 1)
[NO-BREAK]of charString
set fractionNum to fpart
if fractionNum = 0 then exit repeat
end repeat
set fractionPart to "." & fractionPart
end if
set d to wholePart & fractionPart
return d
end ConvertBase
on ValidateBase(num, base)
set allowableChars to {"."} & characters 1 thru base of
[NO-BREAK]charString as list
repeat with a from 1 to length of num
if character a of num is not in allowableChars then
display dialog "Error: Number contains invalid
[NO-BREAK]characters" & return & return & "Character: " & character a
[NO-BREAK]of num & return & "Base: " & base buttons "Cancel"
end if
end repeat
end ValidateBase
end script
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
PS. And yes, I still use ScriptToEmail, has.
--
Greg Back
email@hidden
_______________________________________________
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.