Calculate Proportions

To explain what this script does, let me give an example of how you would use it.

Let’s say you have an image, which is 800 x 600 pixels. You want to increase the width to 1000 pixels, but don’t know the height proportional to that width. This is where my script comes in.

The script begins with a dialog where you type in your initial dimensions (800, 600). The script then asks you for a width or height to compare it to, with x as the unknown value (1000, x). From there, the script will fill in the mussing value, and return the full dimensions (1000, 750).

P.S. The script knows if you separate the width and height with a comma (800, 600) or a space (800 600), so you can use either. Even mixing it up throughout the script won’t confuse it.

-- display dialog for initial dimensions
activate
set initialDialog to display dialog "Initial dimensions (separate with comma or space):" with title "Initial Dimensions" buttons {"Cancel", "Continue"} default button 2 cancel button 1 default answer "800, 600"
set theDim to text returned of initialDialog
set dimSep to offset of ", " in theDim
-- get initial width and height
set theWidth to characters beginning through (dimSep - 1) of theDim as text
set theHeight to characters (dimSep + 2) through end of theDim as text
-- if there is no comma
if dimSep = 0 then
	set dimSep to offset of " " in theDim
	-- get initial width and height (without comma)
	set theWidth to characters beginning through (dimSep - 1) of theDim as text
	set theHeight to characters (dimSep + 1) through end of theDim as text
end if
-- set scale factor
set scaleFac to (theWidth / theHeight)

-- display dialog for new dimensions
try
	set newDialog to display dialog "New dimensions (use x as the unknown value):" with title "New Dimensions" buttons {"Cancel", "Calculate"} default button 2 cancel button 1 default answer "1024, x"
	set newDim to text returned of newDialog
	set newSep to offset of ", " in newDim
	-- get new width and height
	set newWidth to characters beginning through (newSep - 1) of newDim as text
	set newHeight to characters (newSep + 2) through end of newDim as text
	-- if there is no comma
	if newSep = 0 then
		set newSep to offset of " " in newDim
		-- get new width and height (without comma)
		set newWidth to characters beginning through (newSep - 1) of newDim as text
		set newHeight to characters (newSep + 1) through end of newDim as text
	end if
	-- if finding the width
	if newWidth = "x" then set newWidth to (newHeight * scaleFac) as integer
	-- if finding the height
	if newHeight = "x" then set newHeight to (newWidth / scaleFac) as integer
	-- return to first dialog if canceled
on error number -128
	tell me to run
end try

-- display full dimensions
set finalDim to "" & newWidth & ", " & newHeight & ""
set finalDialog to display dialog "The final dimensions are:
" & finalDim & "" with title "Final Dimensions" buttons {"Cancel", "Repeat"} default button 2 cancel button 1
-- repeat if chosen
if button returned of finalDialog = "Repeat" then
	tell me to run
end if