Re: applescript-users digest, Vol 2 #84 - 13 msgs
Re: applescript-users digest, Vol 2 #84 - 13 msgs
- Subject: Re: applescript-users digest, Vol 2 #84 - 13 msgs
- From: "Geoff Graham" <email@hidden>
- Date: Wed, 29 Nov 2000 17:11:43 -0600
I'm not sure if I liked it better when the list bounced a message with a subject of "Re: Contents of applescript-users digest...", or now that it doesn't. I just know I've sent a couple already. I also know that this script I came up with for my wife is cool enough to share. It figures sales tax (at 8.25%; easily changed) and returns the total formatted like the input; with commas or not, with leading zeros or not. Use it from OSA Menu to use it in any application: Copy, run the script, and paste. Tested with Simpletext, Pagemaker, and Finder. works with non-scriptable apps since it's using the clipboard. It seems too big for what it does to me, oh well.
-- begin script
tell current application
set thenum to the clipboard
end tell
try
set commainflag to false
set leadingdecimal to false
if text item 1 of thenum is "0" then
set leadingdecimal to true
end if
set x to offset of "," in thenum
if x is not 0 then
set commainflag to true
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set thenum to text items of thenum
set AppleScript's text item delimiters to oldDelims
end if
set thenum to thenum as string
set thenum to thenum as real
set the rawanswer to (thenum * 0.0825)
set rawanswer to rawanswer as string
set x to offset of "E" in rawanswer
if x is not 0 then
set frontanswer to text 1 thru (x - 1) in rawanswer
set theMult to text (x + 2) thru -1 in rawanswer
set x to offset of "." in frontanswer
set temp to ("" & (text items 1 thru (x - 1) in frontanswer))
set temp2 to ("" & (text items (x + 1) thru -1 in frontanswer))
set frontanswer to "" & temp & temp2
set temp to (text items 1 thru ((x + theMult) - 1) in frontanswer)
set temp2 to ("" & (text items (x + theMult) thru -1 in frontanswer))
set rawanswer to "" & temp & "." & temp2
end if
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set temp to text items of rawanswer
set AppleScript's text item delimiters to oldDelims
set temp1 to first item of temp
set temp1 to temp1 as integer
set leadingzeroflag to false
try
set temp2 to text items of (last item of temp)
try
set temp3 to items 1 thru 2 of temp2
if item 1 of temp3 is "0" then
set leadingzeroflag to true
end if
set temp3 to (temp3 as string) as integer
try
set temp4 to (item 3 of temp2) as integer
if temp4 is greater than 4 then
set temp3 to temp3 + 1
end if
on error
end try
if temp3 = 100 then
set temp1 to temp1 + 1
set temp3 to "00"
end if
on error
set temp3 to "" & (item 1 of temp2) & "0"
end try
on error
set temp3 to "00"
end try
if leadingzeroflag then
set temp3 to "" & "0" & temp3
end if
set the roundanswer to "" & temp1 & "." & temp3
set the roundanswer to the roundanswer as real
set theanswer to (thenum + roundanswer) as string
set x to offset of "E" in theanswer
if x is not 0 then
set frontanswer to text 1 thru (x - 1) in theanswer
set theMult to text (x + 2) thru -1 in theanswer
set x to offset of "." in frontanswer
set temp to ("" & (text items 1 thru (x - 1) in frontanswer))
set temp2 to ("" & (text items (x + 1) thru -1 in frontanswer))
set frontanswer to "" & temp & temp2
set temp to (text items 1 thru ((x + theMult) - 1) in frontanswer)
if commainflag then
set temp3 to items -3 thru -1 of temp
set temp4 to items 1 thru -4 of temp
set temp to "" & temp4 & "," & temp3
end if
set temp2 to ("" & (text items (x + theMult) thru -1 in frontanswer))
set theanswer to "" & temp & "." & temp2
end if
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set temp to text items of theanswer
set AppleScript's text item delimiters to oldDelims
try
set temp to item 2 of temp
try
set temp to text item 2 of temp
on error
set theanswer to "" & theanswer & "0"
end try
on error
set theanswer to "" & theanswer & ".00"
end try
if text item 1 of theanswer is "0" then
if not leadingdecimal then
set theanswer to "" & (text items 2 thru -1 of theanswer)
end if
end if
tell current application
set the clipboard to theanswer
end tell
on error errnum
beep 1
end try