help to create simple equation in applescript

Hi All


Can someone help me create a script for a simple math equation.


(A) = variable figure with a prompt 'total sheet height' before entering figure


then


SUBTRACT (B) variable figure with a prompt 'total job height' before entering figure


then


result divided by 2.


then


prompt to say 'Your result is'


in essence (A-B)/2


thanks

Mac Pro

Posted on Jun 10, 2016 5:08 AM

Reply
2 replies

Jun 10, 2016 5:48 AM in response to Kevlesser

Launch Script Editor (Launchpad : Other : Script Editor). Copy and paste the following AppleScript into it. Click the hammer icon (compile), and then the ▸ button to run it. Save your script as File format: Text, and then option+File menu : Save As to any of Format (script, script bundle, or Application) to your Desktop. A double-click will run it.


try

display dialog "Total sheet height" default answer ""

set A to text returned of result

display dialog "Total job height" default answer ""

set B to text returned of result

on error errmsgnumbererrnbr

my error_handler(errnbr, errmsg)

return

end try


ignoring numeric strings

set answer to (A - B) / 2

end ignoring

display dialog "Your result is " & answer as text

return


on error_handler(nbr, msg)


display alert "[ " & nbr & " ] " & msgascriticalgiving up after 10

return

end error_handler

tr

Jun 10, 2016 8:26 AM in response to Kevlesser

A succinct approach if you don't need error handling is:


set sh to text returned of (display dialog "Total sheet height: " default answer "")

set jh to text returned of (display dialog "Total job height: " default answer "")

set theResult to (sh - jh) / 2

display dialog "Your Result is: " & theResult buttons "OK"



Or with some error handling:


set sh to text returned of (display dialog "Total sheet height: " default answer "")

set jh to text returned of (display dialog "Total job height: " default answer "")

try

if sh * jh > 0 then

set theResult to (sh - jh) / 2

display dialog "Your Result is: " & theResult buttons "OK"

else

error

end if

on error

display dialog "Enter values > 0" buttons "OK"

end try



SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

help to create simple equation in applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.