MacOS – Getting the lower variable

applescriptmacossort

In applescript I have 4 variables, each one with its number.
I'd like to have a function that tells me which variable is set to the lower number.

EG.

set one to 12
set two to 55
set three to 2
set four to 1244

function(which one is the lower) - returns three.

I'm new to applescript, in PHP I would do this thanks to associative arrays, but in applescript we have simple lists.

It would be awesome if it could return the first variable name if two or more variables are set to the same number. Eg if 'one' and 'four' are set to 0 it should return 'one'.

Best Answer

From what I've found by Googling, I haven't found any built in Min/Max functionality, however you can easily write your own:

    to min(n1,n2)
       if n1 < n2 then
--> change "<" to ">" to make a max handler
           return n1
       else
           return n2
    end min

After you've written that function (called a handler in AS), whenever you need to set a variable to the lower of two values, you can:

set smallerNumber to min(numberOne, numberTwo)