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.


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'.

  • 1
    AFAIK, you cannot easily get the name of a variable. I have written a couple of Applescript commands that possibly will work for this. But will require you using an Applescript Script Libraries. see here: macosxautomation.com/mavericks/libraries/index.html for what they are and let me know if you want to try mine. – markhunte Nov 30 '13 at 22:00
  • This might be just a language issue but are you really interested in the name of the variable or rather in its content/value? If you think it it's the name, what would you do with the name afterwards (maybe add some more code lines to show this)? – nohillside Dec 1 '13 at 9:21
  • 1
    @patrix Each name refers to certain coordinates on the screen. And when I put a window to those particular coordinates I also increment the corresponding variable: I use them as counters. I'm looking for some code to check which variable is lower, or, in other words, which position of my screen has the fewer windows tiled in order to move new or older windows in a balanced way. Eg if I have 12 windows opened and 4 possible position I want to organize them 3 per position. – KingBOB Dec 1 '13 at 9:56
  • Hmm, the "better" way would be to use an array/list of position counts and then use the array index as the position. – nohillside Dec 1 '13 at 13:42

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
           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)

  • I just realized you want to return the NAME of the variable and not the value... bummer. – Alexander - Reinstate Monica Dec 1 '13 at 1:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .