Can a handler return multiple values? Using properties perhaps?

Something like this does:

display dialog "Example" default answer "text to return" buttons {"ok", "whatever"} default button 1
copy the result as list to {text_returned, button_pressed}

Currently I have a handler that looks something like this:


set Valueset1 to callHandler(1)
set Valueset2 to callHandler(2)

on callHandler(type)
---Stuff here

if type is "1" then
 return Valueset1
else 
 return Valueset2
end if

end callHandler

is it possible to do something like:

set Valueset1 to Valueset1 of callHandler(1)
set Valueset2 to Valueset2 of callHandler(2)

on callHandler(type)
	---Stuff here
	set Valueset1 to "blah"
	set Valueset2 to "Blah"
	if type is "1" then
		return Valueset1
	else
		return Valueset2
	end if
	
end callHandler

I am pretty sure it has something to do with properties, but I’m just not wrapping my head around it. Ideally for my purposes the return would be two lists within a list or something to that effect.
Result:
{{“valueset1”},{“valueset2”}}

Seems simple and really is there to eliminate calling the handler twice. Still, I’d like to know how.

The solution I am using is to create the list and just return it within the handler.


set Valueset to callHandler()


on callHandler()
	---Stuff here
---Valuesets are really lists themselves, but for the example:
	set Valueset1 to "blah"
	set Valueset2 to "Blah"
	
set Returnlist to {}
copy Valueset1 to end of Returnlist
copy Valueset2 to end of Returnlist
return Returnlist
end callHandler

I’m just curious to learn if there is a better way of going about it all.

on callHandler
	
	-- do stuff here
	
	return {{1, 2, 3}, {4, 5, 6}}
end callHandler

set {valueSet1, valueSet2} to callHandler
-- valueSet1 --> {1, 2, 3}
-- valueSet2 --> {4, 5, 6}

Is this what you are asking about?

Hope this helps,
Brad

Yes, that is exactly what I was trying to get. Simple and effective.

Thanks!

:smiley:

Bear in mind that the handler needs parentheses: call handler()


on callHandler()
	
	-- do stuff here
	
	return {{1, 2, 3}, {4, 5, 6}}
end callHandler

set {valueSet1, valueSet2} to callHandler()
-- valueSet1 --> {1, 2, 3}
-- valueSet2 --> {4, 5, 6}


Opps! That’s what I get for doing it off the top of my head. Thanks for the correction Adam.

Brad

The answer works if all elements of the lists are the same data type.
Is there some way I can get a handler to return a string text value and a Boolean?

No problem to do that.

on callHandler()
	
	set aString to "blahblahblah"
	set aBoolean to true
	
	return {aString, aBoolean}
end callHandler

set {someString, someBoolean} to callHandler()


--> {"blahblahblah", true}

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 1 août 2019 20:11:46