VAT/Mark-up Script Looping

A little while ago I wrote a script for calculating the total price of an item including VAT and a mark-up. Two people on this list, StefanK and Nigel Garvey gave me a lot of help in finishing it off, (thanks guys!) and it works perfectly.

The only thing is that I’d like it to loop so that I could do another calculation, after copying the result to the clipboard for the result I’ve just calculated. Ideally I’d like a button at the end to copy the result to the clipboard and then do another calculation plus one to copy the result to the clipboard and then quit.

I’ve tried various code to get this to work but so far no joy. I’m sure it’s something really obvious but at the moment it’s eluding me. All and any help would be appreciated!

Here’s the script that I have at the moment:

-- VAT + Mark-Up Cost Script

-- A script that calculates a total price from a base price + VAT + a suitable mark-up to the client then provides the result and copies it to the clipboard for later use.

set r to return as text
display dialog "VAT + Mark-up Costing Tool" & r & r & "A small Applescript to help calculate the total print production costs chargeable to the client." & r & r & "© 2011 Creative19" with icon note buttons {"Let's Work", "Not now"} default button {"Let's Work"}

-- this part of the code shows a dialog box with the name of the script, an explanation of what it does and the copyright information
-- the 'Let's Work' button starts the script running
-- the 'Not Now' button quits the script

set opr to the button returned of the result
if opr is "Let's Work" then
	
	display dialog "Production Costs" & r & r & "Enter the total print and finishing" & r & "production costs (Excluding any VAT)" default answer "" buttons {"Next", "Cancel"} default button {"Next"}
	set num1 to the text returned of the result
	
	-- this part of the code displays a dialog box asking for the base production cost for the work
	-- pressing the 'Next' button takes the script to the next stage
	-- pressing the 'Cancel' button quits the script
	
	display dialog "Value Added Tax" & r & r & "Enter the current UK VAT rate as a % figure" default answer "" buttons {"Next", "Cancel"} default button {"Next"}
	set num2 to the text returned of the result
	
	-- this part of the code displays a dialog box asking for the current rate of VAT
	-- pressing the 'Next' button takes the script to the next stage
	-- pressing the 'Cancel' button quits the script
	
	display dialog "Creative19 Mark-up" & r & r & "Enter the markup to the client as a % figure" default answer "" buttons {"Calculate", "Cancel"} default button {"Calculate"}
	set num3 to the text returned of the result
	
	-- this part of the code displays a dialog box asking for the mark-up percentage
	-- pressing the 'Next' button takes the script to the next stage
	-- pressing the 'Cancel' button quits the script
	
	tell num1 * (100 + num2) * (100 + num3) / 100 to ¬
		tell (it div 0.5 - it div 1) to ¬
			tell it div 100 & "." & text 2 thru 3 of (100 + it mod 100 as text) to set {totalCostString, totalCost} to {"Total Cost to Client: £" & it, "" & it}
	
	-- this part of the code calculates the final result using the following equation:
	-- (Production Costs X (100 + VAT) ÷ 100 X (100 + Creative19 Mark-up) ÷ 100) X 100 = Production Costs X (100 + VAT) X (100 + Creative19 Mark-up) ÷ 100
	-- it then rounds up the total to the nearest integer (.5 away from 0)
	
	display dialog totalCostString buttons {"Copy Cost & Quit", "Calculate Another"} default button {"Copy Cost & Quit"}
	set the clipboard to totalCost
	
	-- this part of the code displays a dialog box giving the total chargeable cost, to two decimal places
	-- clicking the 'Copy Cost & Quit' button both closes the script and copies the result to the clipboard
	-- clicking the 'Calculate Another' button both closes the script and copies the result to the clipboard
	
	set opr to the button returned of the result
	if opr is "Calculate Another" then display dialog "VAT + Mark-up Costing Tool" & r & r & "A small Applescript to help calculate the total print production costs chargeable to the client." & r & r & "© 2011 Creative19" with icon note buttons {"Let's Work", "Not now"} default button {"Let's Work"}
	
	set opr to the button returned of the result
	if opr is "Let's Work" then
		
		display dialog "Production Costs" & r & r & "Enter the total print and finishing" & r & "production costs (Excluding any VAT)" default answer "" buttons {"Next", "Cancel"} default button {"Next"}
		set num1 to the text returned of the result
		
		display dialog "Value Added Tax" & r & r & "Enter the current UK VAT rate as a % figure" default answer "" buttons {"Next", "Cancel"} default button {"Next"}
		set num2 to the text returned of the result
		
		display dialog "Creative19 Mark-up" & r & r & "Enter the markup to the client as a % figure" default answer "" buttons {"Calculate", "Cancel"} default button {"Calculate"}
		set num3 to the text returned of the result
		
		tell num1 * (100 + num2) * (100 + num3) / 100 to ¬
			tell (it div 0.5 - it div 1) to ¬
				tell it div 100 & "." & text 2 thru 3 of (100 + it mod 100 as text) to set {totalCostString, totalCost} to {"Total Cost to Client: £" & it, "" & it}
		
		display dialog totalCostString buttons {"Copy Cost & Quit", "Calculate Another"} default button {"Copy Cost & Quit"}
		set the clipboard to totalCost
	end if
end if


That’s easily doable. But do you want the results on the clipboard overwritten each time?

Nigel…

Thanks again for being the first to respond.

The simple answer is yes to your question because what I’d probablly do is calculate a price then move over to another application, paste the result into this and then move back to the script application to do another one.

Thanks.

Michael

I think you want something like this. Your idiosyncratic placing of comments below the code to which they refer could be confusing for others. I’ve left them in place, but for the sake of other forum readers, I’ve doctored them slightly to make it clear where to look.

-- VAT + Mark-Up Cost Script

-- A script that calculates a total price from a base price + VAT + a suitable mark-up to the client then provides the result and copies it to the clipboard for later use.

set r to return
display dialog "VAT + Mark-up Costing Tool" & r & r & "A small Applescript to help calculate the total print production costs chargeable to the client." & r & r & "© 2011 Creative19" with icon note buttons {"Let's Work", "Not now"} default button "Let's Work"
if (button returned of the result is "Not now") then error number -128 -- "User canceled" [sic]

-- the above part of the code shows a dialog box with the name of the script, an explanation of what it does and the copyright information
-- the 'Let's Work' button starts the script running
-- the 'Not Now' button quits the script

repeat
	display dialog "Production Costs" & r & r & "Enter the total print and finishing" & r & "production costs (Excluding any VAT)" default answer "" buttons {"Next", "Cancel"} default button "Next"
	set num1 to the text returned of the result
	
	-- the above part of the code displays a dialog box asking for the base production cost for the work
	-- pressing the 'Next' button takes the script to the next stage
	-- pressing the 'Cancel' button quits the script
	
	display dialog "Value Added Tax" & r & r & "Enter the current UK VAT rate as a % figure" default answer "" buttons {"Next", "Cancel"} default button "Next"
	set num2 to the text returned of the result
	
	-- the above part of the code displays a dialog box asking for the current rate of VAT
	-- pressing the 'Next' button takes the script to the next stage
	-- pressing the 'Cancel' button quits the script
	
	display dialog "Creative19 Mark-up" & r & r & "Enter the markup to the client as a % figure" default answer "" buttons {"Calculate", "Cancel"} default button "Calculate"
	set num3 to the text returned of the result
	
	-- the above part of the code displays a dialog box asking for the mark-up percentage
	-- pressing the 'Next' button takes the script to the next stage
	-- pressing the 'Cancel' button quits the script
	
	tell num1 * (100 + num2) * (100 + num3) / 100 to ¬
		tell (it div 0.5 - it div 1) to ¬
			tell (it div 100 as text) & "." & text 2 thru 3 of (100 + it mod 100 as text) to set {totalCostString, totalCost} to {"Total Cost to Client: £" & it, it}
	
	-- the above part of the code calculates the final result to two decimal places (.005 away from 0) using the following equation:
	-- (Production Costs X (100 + VAT) ÷ 100 X (100 + Creative19 Mark-up) ÷ 100) X 100 = Production Costs X (100 + VAT) X (100 + Creative19 Mark-up) ÷ 100
	
	set nextAction to button returned of (display dialog totalCostString buttons {"Copy Cost & Quit", "Copy Cost & Calculate Another"} default button "Copy Cost & Quit")
	set the clipboard to totalCost
	if (nextAction is "Copy Cost & Quit") then exit repeat
	
	-- the above part of the code displays a dialog box giving the total chargeable cost, to two decimal places
	-- clicking the 'Copy Cost & Quit' button both copies the result to the clipboard and exits the repeat, ending the script
	-- clicking the 'Calculate Another' button copies the result to the clipboard and allows another iteration of the input process
end repeat

Nigel…

Thank you so much for the help. It works just the way I want it to now.

Michael Preston