Default variable values in function declarations?

Is there a way to declare a default variable for an applescript function (as in…

function a_function(variable=defaultValue)

end a_function

)
I tried the example above and it doesn’t work (“variable=defaultValue is illegal as a formal parameter”). Is there a way to do this?

Hi,

function doesn’t work either in AppleScript :wink:

The normal syntax for a handler with parameter(s) is

display dialog addition(2, 5) as Unicode text --> 7

on addition(a, b) -- a and b are local variables, which are valid and visible only in this handler
	return a + b -- (the keyword return is actually not necessary in this case, but I like to use it always)
end addition

A synonym for the keyword on is to

Take a look at Getting Started with Handlers (there are 3 articles)

Uh, I knew that. No, really. I’ve been working more with PHP than AppleScript lately. I was doing well not to include a variable that started with a dollar sign. :slight_smile:

I looked for information on how to pass a variable with a default into a handler, and couldn’t find any, which I suspect means it isn’t possible, but I’m in denial about that, so I thought I’d ask.

Whenever I’m writing a handler that has more than 2 or 3 parameters, or for which I want to have optional parameters, I do the following:



someHandler({someRequiredParam:"Hello World!"})

on someHandler(prefs)
   set defaultPrefs to {someParam1:"", otherParam2:false}
   set prefs to prefs & defaultPrefs  
   -- so now the record 'prefs' contains the 
   -- default fields and their values

   -- now, just use 'of prefs' whenever you need to reference a parameter:

   if otherParam2 of prefs then 
      -- do something only if the optional parameter was sent in and was true
   end if
   
   -- ... (rest of code)
end

If you don’t want to have to say ‘of prefs’ every time you use a parameter, then you can have a block of code that does, for example:


set otherParam2 to otherParam2 of prefs

You can also in that block do error-checking in case there is a required parameter. Here’s a more detailed example:



-- Will display msg after saying it in Fred's voice:
msgFeedback({msgRequested:"Hello World!", sayIt:true})

-- Will display msg without saying anything
msgFeedback({msgRequested:"Hello World!"})

-- Will display msg after saying it in Victoria's voice:
msgFeedback({msgRequested:"Hello World!", sayIt:true, chosenVoice:Victoria})


on msgFeedback(prefs)
   set defaultPrefs to {sayIt:false, chosenVoice:"Fred"}
   set prefs to prefs & defaultPrefs

   try
      set msgRequested to msgRequested of prefs
   on error
      error "ERROR in msgFeedback: msgRequested is a required parameter."
   end try

   set msgRequested to msgRequested of prefs
   set sayIt to sayIt of prefs
   set chosenVoice to chosenVoice of prefs


   if sayIt then
      say msgRequested using chosenVoice waiting until completion false
   end

   display dialog msgRequested

   return true

end

If there are any mistakes, I apologize. I’m writing this from a Windows computer, so I couldn’t test my code.