Radius Corner Calculator

So I’m writing a script to calculate the difference between radius corner boxes so that when they are positioned in InDesign the arc if the corner is parallel.

I’ve cracked the script more or less but I’ve messed up the repeat bit at the end and I can’t work it out (my brain must be mush!)

Can anyone help please?

set r to return
set welcomeResult to button returned of (display dialog “Radius Corner Nesting Box Calculator” & r & r & “A small application to quickly and easily calculate a correct radius for a nested radius corner box within another to ensure that the radius angle matches and is parallel” & r & r & “The formulas for these calculations are:” & r & “Outer radius – gap distance = Inner radius” & r & “Inner Radius + Gap Distance = Outer radius” buttons {“Cancel”, “Let’s work”} default button “Let’s work”)
if welcomeResult is “Cancel” then
error number -128 – User canceled
else
my mainFunction()
end if
– Function to calculate outer radius based on inner radius and gap distance
on calculateOuterRadius(innerRadius, gapDistance)
return innerRadius + gapDistance
end calculateOuterRadius
– Function to calculate inner radius based on outer radius and gap distance
on calculateInnerRadius(outerRadius, gapDistance)
return outerRadius - gapDistance
end calculateInnerRadius
– Main function to execute the script
on mainFunction()
– Prompt user for calculation choice
set calculationChoice to choose from list {“Calculate the radius of the inner box”, “Calculate the radius of the outer box”} with prompt “Choose the calculation:” default items {“Calculate the radius of the inner box”} without multiple selections allowed and empty selection allowed
if calculationChoice is false then
display dialog “Thanks for using the app - have a great day!” buttons {“Quit”} default button “Quit”
return
end if
– Set radius dialog box text based on the calculation choice
set radiusTypePrompt to “”
if item 1 of calculationChoice is “Calculate the radius of the outer box” then
set radiusTypePrompt to “Enter the radius of the inner box:”
else
set radiusTypePrompt to “Enter the radius of the outer box:”
end if
– Prompt user for inner or outer radius and gap distance
set radius to text returned of (display dialog radiusTypePrompt default answer “”)
set gapDistance to text returned of (display dialog “Enter the gap distance between the two boxes:” default answer “”)
try
– Convert text input to numbers
set radius to radius as number
set gapDistance to gapDistance as number
on error
display dialog “Script error: Please note that you should only enter numeric values into the calculation boxes. Decimal points are allowed, however.” with icon caution buttons {“Quit”, “Try again”} default button “Try again”
if button returned of the result is “Quit” then
error number -128 – User canceled
else
my mainFunction()
end if
end try
– Perform selected calculation
if item 1 of calculationChoice is “Calculate the radius of the outer box” then
set resultRadius to calculateOuterRadius(radius, gapDistance)
set resultType to “The outer box radius should be”
else
set resultRadius to calculateInnerRadius(radius, gapDistance)
set resultType to “The inner box radius should be”
end if
– Copy the result to the clipboard
do shell script “echo " & (resultRadius as text) & " | pbcopy”
– Display the result
display dialog resultType & ": " & resultRadius buttons {“Copy result & quit”, “Copy Cost to Clipboard & Calculate Again”} default button “Copy result & quit”
set nextAction to button returned of (display dialog buttons {“Copy result & quit”, “Copy Cost to Clipboard & Calculate Again”} default button “Copy Cost to Clipboard & Calculate Again”)
set the clipboard to resultRadius
if (nextAction is “Copy result & quit”) then exit repeat
end mainFunction

Cloudwalker_3. When posting a script, you have place three backtick characters on the lines before and after the script. Otherwise, the formatting of some characters in the script are changed. I made the necessary changes, and the following compiled in Script Editor. I don’t have the InDesign app and can’t help with that.

set r to return
set welcomeResult to button returned of (display dialog "Radius Corner Nesting Box Calculator" & r & r & "A small application to quickly and easily calculate a correct radius for a nested radius corner box within another to ensure that the radius angle matches and is parallel" & r & r & "The formulas for these calculations are:" & r & "Outer radius -- gap distance = Inner radius" & r & "Inner Radius + Gap Distance = Outer radius" buttons {"Cancel", "Let's work"} default button "Let's work")
if welcomeResult is "Cancel" then
	error number -128 -- User canceled
else
	my mainFunction()
end if
-- Function to calculate outer radius based on inner radius and gap distance
on calculateOuterRadius(innerRadius, gapDistance)
	return innerRadius + gapDistance
end calculateOuterRadius
-- Function to calculate inner radius based on outer radius and gap distance
on calculateInnerRadius(outerRadius, gapDistance)
	return outerRadius - gapDistance
end calculateInnerRadius
-- Main function to execute the script
on mainFunction()
	-- Prompt user for calculation choice
	set calculationChoice to choose from list {"Calculate the radius of the inner box", "Calculate the radius of the outer box"} with prompt "Choose the calculation:" default items {"Calculate the radius of the inner box"} without multiple selections allowed and empty selection allowed
	if calculationChoice is false then
		display dialog "Thanks for using the app - have a great day!" buttons {"Quit"} default button "Quit"
		return
	end if
	-- Set radius dialog box text based on the calculation choice
	set radiusTypePrompt to ""
	if item 1 of calculationChoice is "Calculate the radius of the outer box" then
		set radiusTypePrompt to "Enter the radius of the inner box:"
	else
		set radiusTypePrompt to "Enter the radius of the outer box:"
	end if
	-- Prompt user for inner or outer radius and gap distance
	set radius to text returned of (display dialog radiusTypePrompt default answer "")
	set gapDistance to text returned of (display dialog "Enter the gap distance between the two boxes:" default answer "")
	try
		-- Convert text input to numbers
		set radius to radius as number
		set gapDistance to gapDistance as number
	on error
		display dialog "Script error: Please note that you should only enter numeric values into the calculation boxes. Decimal points are allowed, however." with icon caution buttons {"Quit", "Try again"} default button "Try again"
		if button returned of the result is "Quit" then
			error number -128 -- User canceled
		else
			my mainFunction()
		end if
	end try
	-- Perform selected calculation
	if item 1 of calculationChoice is "Calculate the radius of the outer box" then
		set resultRadius to calculateOuterRadius(radius, gapDistance)
		set resultType to "The outer box radius should be"
	else
		set resultRadius to calculateInnerRadius(radius, gapDistance)
		set resultType to "The inner box radius should be"
	end if
	-- Copy the result to the clipboard
	do shell script "echo " & (resultRadius as text) & " | pbcopy"
	-- Display the result
	display dialog resultType & ": " & resultRadius buttons {"Copy result & quit", "Copy Cost to Clipboard & Calculate Again"} default button "Copy result & quit"
	set nextAction to button returned of (display dialog buttons {"Copy result & quit", "Copy Cost to Clipboard & Calculate Again"} default button "Copy Cost to Clipboard & Calculate Again")
	set the clipboard to resultRadius
	if (nextAction is "Copy result & quit") then exit repeat
end mainFunction
1 Like

Cloudwalker_3. I looked at your script just to see how it worked, and I had two comments. The following code in your script returns an error, because the display dialog command doesn’t have the required text before the buttons parameter.

set nextAction to button returned of (display dialog buttons {"Copy result & quit", "Copy Cost to Clipboard & Calculate Again"} default button "Copy Cost to Clipboard & Calculate Again")

Your script doesn’t have a repeat loop, so there’s nothing to exit. I inserted a repeat loop which may do what you want. I also deleted a second display dialog command at the end of the mainFunction handler–it didn’t appear to be necessary, and it caused the error noted above. After making these changes, the script appears to work:

set r to return
set welcomeResult to button returned of (display dialog "Radius Corner Nesting Box Calculator" & r & r & "A small application to quickly and easily calculate a correct radius for a nested radius corner box within another to ensure that the radius angle matches and is parallel" & r & r & "The formulas for these calculations are:" & r & "Outer radius -- gap distance = Inner radius" & r & "Inner Radius + Gap Distance = Outer radius" buttons {"Cancel", "Let's work"} default button "Let's work")
repeat -- added repeat loop
	mainFunction()
end repeat

-- Function to calculate outer radius based on inner radius and gap distance
on calculateOuterRadius(innerRadius, gapDistance)
	return innerRadius + gapDistance
end calculateOuterRadius

-- Function to calculate inner radius based on outer radius and gap distance
on calculateInnerRadius(outerRadius, gapDistance)
	return outerRadius - gapDistance
end calculateInnerRadius

-- Main function to execute the script
on mainFunction()
	-- Prompt user for calculation choice
	set calculationChoice to choose from list {"Calculate the radius of the inner box", "Calculate the radius of the outer box"} with prompt "Choose the calculation:" default items {"Calculate the radius of the inner box"} without multiple selections allowed and empty selection allowed
	if calculationChoice is false then
		display dialog "Thanks for using the app - have a great day!" buttons {"Quit"} default button "Quit" cancel button "Quit"
	end if
	-- Set radius dialog box text based on the calculation choice
	set radiusTypePrompt to ""
	if item 1 of calculationChoice is "Calculate the radius of the outer box" then
		set radiusTypePrompt to "Enter the radius of the inner box:"
	else
		set radiusTypePrompt to "Enter the radius of the outer box:"
	end if
	-- Prompt user for inner or outer radius and gap distance
	set radius to text returned of (display dialog radiusTypePrompt default answer "")
	set gapDistance to text returned of (display dialog "Enter the gap distance between the two boxes:" default answer "")
	try
		-- Convert text input to numbers
		set radius to radius as number
		set gapDistance to gapDistance as number
	on error
		display dialog "Script error: Please note that you should only enter numeric values into the calculation boxes. Decimal points are allowed, however." with icon caution buttons {"Quit", "Try again"} default button "Try again"
		if button returned of the result is "Quit" then
			error number -128 -- User canceled
		else
			return
		end if
	end try
	-- Perform selected calculation
	if item 1 of calculationChoice is "Calculate the radius of the outer box" then
		set resultRadius to calculateOuterRadius(radius, gapDistance)
		set resultType to "The outer box radius should be"
	else
		set resultRadius to calculateInnerRadius(radius, gapDistance)
		set resultType to "The inner box radius should be"
	end if
	-- Copy the result to the clipboard
	do shell script "echo " & (resultRadius as text) & " | pbcopy"
	-- Display the result
	set nextAction to button returned of (display dialog resultType & ": " & resultRadius buttons {"Copy result & quit", "Copy Cost to Clipboard & Calculate Again"} default button "Copy result & quit")
	set the clipboard to resultRadius
	if (nextAction is "Copy result & quit") then error number -128
end mainFunction
1 Like

Thanks @peavine you’re a total star!

1 Like