Kevlesser

Q: 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

Close

Q: help to create simple equation in applescript

  • All replies
  • Helpful answers

  • by VikingOSX,

    VikingOSX VikingOSX Jun 10, 2016 5:48 AM in response to Kevlesser
    Level 7 (20,819 points)
    Mac OS X
    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 errmsg number errnbr

         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 & " ] " & msg as critical giving up after 10

      return

    end error_handler

    tr

  • by SGIII,

    SGIII SGIII Jun 10, 2016 8:26 AM in response to Kevlesser
    Level 6 (10,676 points)
    Mac OS X
    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