Can't return value from Display Dialog

I got this to do what I want with 1 exception. I want to display the Tax from item1 in a display dialog and I’m not sure how to go about it. Ive tried a few things without success. A proper nudge in right direction would be most welcome.

--Added to library on 09/13/2024
--This script Adds items in list.
--Paste URL here. https://www.macscripter.net/t/adding-numbers-in-a-list-pretty-simple-youd-think/55543/5



--enter prices in list below
set theList to {2.99, 3.99}

set TheNetPrice to 0.0

repeat with x in theList
	set thePrice to x
	set TheNetPrice to TheNetPrice + x as string -- <-- making AS work for me!
end repeat


set TheNetPrice to TheNetPrice as number

display dialog TheNetPrice with title "Net Price"

--Enter the gross price here
--For testing use 7.54 as Gross Price
display dialog "GrossPrice" default answer "" with title "GrossPrice"
set GrossPrice to text returned of result as number


set TaxPaid to GrossPrice - TheNetPrice as number

display dialog TaxPaid with title "Tax Paid"

set TaxRate to TaxPaid / TheNetPrice as number
display dialog TaxRate with title "Tax Rate"


set tax to TaxRate * ((item 1) of theList) + (item 1 of theList) as number

--why won't this display? I want the tax, not item 1 of the list? Tax is 3.22, not 2.99 
display dialog (item 1 of theList)


--Once I fix 1st tax calc, I will be able to fix this.
set tax2 to TaxRate * ((item 2) of theList) + (item 2 of theList) as number
--display dialog "Tax2"
--why won't this display? I want the tax, not item 2 of the list? Tax is 4.31, not 3.99 

The main issue is that you do the math with numbers but display dialog expects text.
You have to coerce the numbers as text in a display dialog line.

By the way all the other coercions back and forth are pretty confusing.
This script doesn’t need any further coercions except after receiving GrossPrice which is also a string

--enter prices in list below
set theList to {2.99, 3.99}

set TheNetPrice to 0.0

repeat with x in theList
	set thePrice to x
	set TheNetPrice to TheNetPrice + x
end repeat


display dialog (TheNetPrice as text) with title "Net Price"

--Enter the gross price here
--For testing use 7.54 as Gross Price
display dialog "GrossPrice" default answer "" with title "GrossPrice"
set GrossPrice to text returned of result as real


set TaxPaid to GrossPrice - TheNetPrice

display dialog (TaxPaid as text) with title "Tax Paid"

set TaxRate to TaxPaid / TheNetPrice
display dialog (TaxRate as text) with title "Tax Rate"


set tax to TaxRate * (item 1 of theList) + (item 1 of theList)

display dialog (item 1 of theList) as text

set tax2 to TaxRate * (item 2 of theList) + (item 2 of theList)
display dialog (tax2 as text)

Thanks for you suggestion. I will try and incorporate into my future scripts.

I tried the fixes you suggested and there’s one issue with the display of the tax in item 1.

This code

set tax to TaxRate * (item 1 of theList) + (item 1 of theList)
display dialog (item 1 of theList) as text

Returns the original value of 2.99 instead of the Tax added value of 3.22 .

How do I display the value of 3.22?

I just renamed the first tax calc to tax 1 and then displayed as you did for tax2

set tax1 to TaxRate * (item 1 of theList) + (item 1 of theList)
display dialog (tax1 as text)

I have tweaked a little more but I ran into something beyond my expertise. It does what i want but I’m struggling with how to get the rest of the display dialog values for “With Tax” & “Tax Paid” to return only 2 digits. This gives me what I’m looking for but I’m looking to enhance/streamline the code. Suggestions welcome.

--Added to library on 09/13/2024
--This script calculates items in theList and then gets tax rate and calculates tax for each item in the list
--Paste URL here. https://www.macscripter.net/t/cant-return-value-from-display-dialog/76281




--enter prices in list below
set theList to {2.99, 3.99, 5.99}

set TheNetPrice to 0.0

repeat with x in theList
	set thePrice to x
	set TheNetPrice to TheNetPrice + x
end repeat


display dialog (TheNetPrice as text) with title "Net Price"

--Enter the gross price here
--For testing use 14.01 as Gross Price
display dialog "GrossPrice" default answer "" with title "Gross Price"
set GrossPrice to text returned of result as real

--calculate how much the tax was...
set TaxPaid to GrossPrice - TheNetPrice

--display how much tax was paid...
display dialog (TaxPaid as text) with title "Total Taxes Paid"

--calculate the tax rate...
set TaxRate to TaxPaid / TheNetPrice

--Display the calculated tax rate
display dialog ((TaxRate as text) & " %") with title "Tax Rate"


--Display Item 1 before tax calc
display dialog (item 1 of theList as text) with title "Item 1 Before Taxes"

--Calcualte tax for item 1
set tax1 to TaxRate * (item 1 of theList as text) + (item 1 of theList as text)

--display total with tax for item 1
--??? How to I get the value to return 2 digits only?
display dialog (tax1 as text) with title "Item 1 With Taxes"


--Calcualte tax1a for item 1
--??? How to I get the value to return 2 digits only?
set tax1a to TaxRate * (item 1 of theList as text)

--display total with tax for item 1
--??? How to I get the value to return 2 digits only?
display dialog (tax1a as text) with title "Item 1 Tax Paid"


--Display Item 2 before tax calc
display dialog (item 2 of theList as text) with title "Item 2 Before Taxes"

--Calcualte tax for item 2
set tax2 to TaxRate * (item 2 of theList as text) + (item 2 of theList as text)

--display total with tax for item 2
display dialog (tax2 as text) with title "Item 2 With Taxes"


--Calcualte tax2a for item 2
set tax2a to TaxRate * (item 2 of theList as text)

--display total with tax for item 2
display dialog (tax2a as text) with title "Item 2 Tax Paid"


--Display Item 3 before tax calc
display dialog (item 3 of theList as text) with title "Item 3 Before Taxes"

--Calcualte tax for item 3
set tax3 to TaxRate * (item 3 of theList) + (item 3 of theList as text)

--display total with tax for item 3
display dialog (tax3 as text) with title "Item 3 With Taxes"

--Calcualte tax3a for item 3
set tax3a to TaxRate * (item 3 of theList as text)

--display total with tax for item 3
display dialog (tax3a as text) with title "Item 3 Tax Paid"

Please stop the pointless coercions. For example in the line

set tax3a to TaxRate * (item 3 of theList as text)

TaxRate is a floating point number, item 3 of theList is also a number. You coerce it to text and AppleScript coerces it silently back to a number.

To display the numbers formatted you need a number formatter. There is none in vanilla AppleScript but there is one in the Foundation framework.

I added a handler and removed all useless coertions.
The use lines at the beginning are mandatory.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

--enter prices in list below
set theList to {2.99, 3.99, 5.99}

set TheNetPrice to 0.0

repeat with x in theList
	set thePrice to x
	set TheNetPrice to TheNetPrice + x
end repeat


display dialog (TheNetPrice as text) with title "Net Price"

--Enter the gross price here
--For testing use 14.01 as Gross Price
display dialog "GrossPrice" default answer "" with title "Gross Price"
set GrossPrice to text returned of result as real

--calculate how much the tax was...
set TaxPaid to GrossPrice - TheNetPrice

--display how much tax was paid...
display dialog formatNumber(TaxPaid) with title "Total Taxes Paid"

--calculate the tax rate...
set TaxRate to TaxPaid / TheNetPrice

--Display the calculated tax rate
display dialog (formatNumber(TaxRate) & " %") with title "Tax Rate"


--Display Item 1 before tax calc
display dialog formatNumber(item 1 of theList) with title "Item 1 Before Taxes"

--Calcualte tax for item 1
set tax1 to TaxRate * (item 1 of theList) + (item 1 of theList)

--display total with tax for item 1
display dialog formatNumber(tax1) with title "Item 1 With Taxes"

--Calcualte tax1a for item 1
set tax1a to TaxRate * (item 1 of theList)

--display total with tax for item 1

display dialog formatNumber(tax1a) with title "Item 1 Tax Paid"


--Display Item 2 before tax calc
display dialog formatNumber(item 2 of theList) with title "Item 2 Before Taxes"

--Calcualte tax for item 2
set tax2 to TaxRate * (item 2 of theList) + (item 2 of theList)

--display total with tax for item 2
display dialog formatNumber(tax2) with title "Item 2 With Taxes"


--Calcualte tax2a for item 2
set tax2a to TaxRate * (item 2 of theList)

--display total with tax for item 2
display dialog formatNumber(tax2a) with title "Item 2 Tax Paid"


--Display Item 3 before tax calc
display dialog formatNumber(item 3 of theList) with title "Item 3 Before Taxes"

--Calcualte tax for item 3
set tax3 to TaxRate * (item 3 of theList) + (item 3 of theList)

--display total with tax for item 3
display dialog formatNumber(tax3) with title "Item 3 With Taxes"

--Calcualte tax3a for item 3
set tax3a to TaxRate * (item 3 of theList)

--display total with tax for item 3
display dialog formatNumber(tax3a) with title "Item 3 Tax Paid"

on formatNumber(theNumber)
	set theFormatter to current application's NSNumberFormatter's new()
	theFormatter's setFormat:"#,##0.00"
	return (theFormatter's stringFromNumber:theNumber) as text
end formatNumber
1 Like

I normally use ASObjC to format numbers, as suggested by Stefan. Another solution is to use the printf shell command:

set theNumber to 31.444
set roundedNumber to do shell script "printf '%.2f' " & theNumber -->"31.44

set theNumber to 31.445
set roundedNumber to do shell script "printf '%.2f' " & theNumber -->"31.45

Another way to get 2 digits would be to multiply by 100, round the result, then divide by 100.

You also have a bit of duplicate code when displaying the summary. Usually this can be reduced by using a handler or repeat statement. In this case, a repeat statement can be used to go through each item, assembling the summary text for that item. For example:

--enter prices in list below
set theList to {2.99, 3.99, 5.99}

set theNetPrice to 0.0
repeat with thePrice in theList
   set theNetPrice to theNetPrice + thePrice
end repeat

--Enter the gross price here
set test to "14.01" -- set to empty string after testing
display dialog "Net Price is " & theNetPrice & return & "Enter Gross Price: " default answer test with title "Gross Price"
set grossPrice to text returned of result as real
set taxPaid to grossPrice - theNetPrice
set taxRate to taxPaid / theNetPrice

--display the taxes paid...
display dialog ¬
   "Tax Rate is: " & format(taxPaid / theNetPrice) & return & ¬
   "Total Taxes Paid: " & format(taxPaid) with title "Total Taxes Paid"
repeat with indx from 1 to (count theList)
   set theItem to item indx of theList
   set dialogText to ¬
      "Before Taxes: " & format(theItem) & return & ¬
      "With Taxes:    " & format(taxRate * theItem + theItem) & return & ¬
      "Tax Paid:        " & format(taxRate * theItem)
   display dialog dialogText with title "Summary for item " & indx
end repeat

to format(theNumber) -- round to 2 places
   (round (theNumber * 100) rounding as taught in school) / 100
end format

Thanks for your guidance. I have learned a few things from this post.

This is very cool. Thanks

I will add this to my list of examples. Thanks

I have a follow up ? regarding this code. What does this symbol do in relation to how the script operates? ¬
I know how to make it appear, but don’t know why it’s there. Can you please explain its significance.

Option + l ¬ Not Sign Math

It’s called the Continuation Character

A simple AppleScript statement must normally be entered on a single line. You can extend a statement to the next line by ending it with the continuation character , ¬. With a U.S. keyboard, you can enter this character by typing Option-l (lower-case L). In Script Editor, you can type Option-Return, which inserts the continuation character and moves the insertion point to the next line.

1 Like

As mentioned, it is the line continuation character, and is used to continue the statement on a new line. It can be helpful for formatting the script or strings so that long lines don’t go off the screen or get wrapped where you don’t want (although the editor sometimes still insists on doing its own thing). In my example, the summary dialog contains multiple lines, so I used line continuation to put each line on its own to more easily see how it was built.

1 Like

Thanks to both of you for the explanation. I learned something today!