AppleScript’s prepositional parameters

You can either love or hate AppleScript — in fact you are quite likely to either love or hate AppleScript — but you have to admit:

AppleScript is a programming linguist’s wet dream.

Its english-likeness and its crufty, underutilized features give it a peculiar flavor. AppleScript is goofy, quirky… even downright silly. Yet at the same time, it’s thoroughly fascinating.

By adopting a certain style, the AppleScripter can compose programs that read like sentences and boldly defy description as a “computer program”.

For the record… I LOVE it.

My personal favorite kooky AppleScript feature (which I learnt by reading Matt Neuberg’s AppleScript: The Definitive Guide) is Prepositional Parameters.

Turns out that with AppleScript you can parameterize function calls in one of three ways (two of which will sound familiar):

  • By index — similar to many modern structured and oo languages (like Java and C), you can send parameters to a function in a specific, defined order. The first parameter to a function call becomes the value of the first argument in the function body and so forth.
  • By name — similar to Python, you can give individual names to the parameters and they will match up with like-named arguments in the function body when called.
  • And finally, by using prepositional parameters — which require a little more explanation.

Instead of labeling function parameters with names, you can label them with prepositional phrases from a predefined set:

  • above
  • against
  • apart from
  • around
  • aside from
  • at
  • below
  • beneath
  • beside
  • between
  • by
  • for
  • from
  • instead of
  • into
  • on
  • onto
  • out of
  • over
  • thru
  • under

So for example, you might define a function (a handler in AppleScript terms) like so:


on quotient of dividend over divisor
	return dividend / divisor
end quotient

quotient of 8 over 2
4.0

AppleScript handlers (again, functions) start with the keyword on much like JavaScript functions start with function or Python functions start with def. quotient is the name of the handler, and dividend and divisor are the two arguments.

Crazy, huh?

So Matt Neuburg provides an interesting example of using prepositional parameters… the following is a valid handler name in AppleScript:


on stopping by woods on aSnowyEvening
	return woods & aSnowyEvening
end stopping

Here’s one I came up with that addresses some Einstein detractors who have claimed his first wife may have actually devised special relativity:


on theElectrodynamics of movingBodies by AlbertEinstein with MicheleBesso without MilevaMaric

end theElectrodynamics

The hard part is deciding what this function would do


About this entry