Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 01-14-2011, 10:33 PM   #1
Kha
Prospect
 
Join Date: Jul 2010
Posts: 7
How can you pass an empty variable to optional parameters?

Hey guys,

I am dipping my toes on creating Applescript libraries but I am stuck on a situation. So far, I am trying to create a mock example of an entry using all the parameters available for the "display dialog" command and then selectively pass the information I want to it to give me the desired outcome. This is where I am right now...

Code:
on display_dialog_box(dialog_text, default_answer, hidden_answer, button_1, button_2, button_3, default_button, cancel_button, with_title, with_icon, giving_up_after)

set theResult to display dialog dialog_text ¬
		default answer default_answer ¬
		hidden answer hidden_answer ¬
		buttons {button_1, button_2, button_3} ¬
		default button default_button ¬
		cancel button cancel_button ¬
		with title with_title ¬
		with icon with_icon ¬
		giving up after giving_up_after

end display_dialog_box
This works if I am planning to use all the optional parameters available, but what I am having trouble with is passing an empty value to the parameters I don't want without Applescript throwing me an error. For example, if I don't want a default answer or a hidden answer to be displayed, is there a way I can pass an empty parameter to these that would make Applescript skip the optional parameter and continue on?

I tried using "", null, missing value... but I can't figure it out.

The reason I am trying to do this is to avoid making all sorts of combinations for what is basically the same subroutine.

The knowledge I am getting from this will make me understand how to optimize my code on much more complex subroutines.

Thanks for reading this far!
Kha is offline   Reply With Quote
Old 01-14-2011, 10:56 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 31,938
AppleScript is not designed to be terse.
If you are trying to make your AppleScript as terse as possible, you are probably doing it wrong.
__________________
hayne.net/macosx.html
hayne is online now   Reply With Quote
Old 01-14-2011, 11:13 PM   #3
acme.mail.order
League Commissioner
 
Join Date: Sep 2003
Location: Tokyo
Posts: 6,044
you'll probably have to wrap each parameter in an "if the parameter is not empty..." bit.

But I can't determine what this routine is doing for you - all the action does is repackage the parameters nearly word-for-word. In this case i would just put the display dialog inline and customize as necessary.
acme.mail.order is offline   Reply With Quote
Old 01-14-2011, 11:35 PM   #4
Kha
Prospect
 
Join Date: Jul 2010
Posts: 7
acme.mail.order, I am just trying to use this as an example for learning. I have other programs that passing empty parameters for them in the method i am trying to figure out would make programming them much easier. Adding those "if..." statements would only make programming longer and complex, but I guess going through the long route is the only way to do it.

hayne, I guess you are right about that. I never encountered any script/tutorial that resembles what I am trying to do before, and now I know why. Applescript design limitation...
Kha is offline   Reply With Quote
Old 01-15-2011, 06:58 AM   #5
renaultssoftware
MVP
 
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
It's not really an AS limitation. Basic functionality is implemented and it's up to you to use the classes you want for your purposes.

You could try:
Code:
on doStuff(x,y,z)
if x is missing value then
-- x is nil and specific parts should be used
end if
if y is missing value then
-- y is nil... etc
end if
actuallyDoSomething()
end doStuff

-- and now you can call it like this:
doStuff(missing value, missing value, 3)
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 07:23 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.