• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Run Handler-Name from Text?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Run Handler-Name from Text?


  • Subject: Re: Run Handler-Name from Text?
  • From: Christopher Nebel <email@hidden>
  • Date: Wed, 18 Apr 2012 21:56:26 -0700

While it's not possible to convert a string to handler (though you can invoke one using a string using "run script", as others have explained), it *is* possible to get references to handlers and then invoke them.  Consider:

on handler_f()
	return "f!"
end handler_f

on handler_g()
	return "g!"
end handler_g

on handler_h()
	return "h!"
end handler_h

on handler_for(x)
	set lookup to {{"f", handler_f}, {"g", handler_g}, {"h", handler_h}} -- notice, no parentheses after the handler name.
	repeat with i in lookup
		if item 1 of i is x then
			return item 2 of i
		end if
	end repeat
end handler_for

set fp to handler_for("g")
fp() --> "g!"

The syntactic rules for invoking a function this way are somewhat obscure.  For instance, the "()" cannot be tied to an arbitrary expression, so 'handler_for("g")()' will not compile.  I was also unable to combine the lookup and call parts into a single handler, because it insisted on interpreting "fp" as a handler name, not a variable -- I'm not sure why that is without digging a lot deeper.  The technique may be of some use to you, though.


--Chris Nebel
AppleScript Engineering
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Run Handler-Name from Text?
      • From: Christopher Stone <email@hidden>
References: 
 >Run Handler-Name from Text? (From: Christopher Stone <email@hidden>)
 >Re: Run Handler-Name from Text? (From: Tom Robinson <email@hidden>)
 >Re: Run Handler-Name from Text? (From: Christopher Stone <email@hidden>)
 >Re: Run Handler-Name from Text? (From: Stan Cleveland <email@hidden>)

  • Prev by Date: Re: [ANN]Scripting Helper App
  • Next by Date: Re: Run Handler-Name from Text?
  • Previous by thread: Re: Temperature sensors
  • Next by thread: Re: Run Handler-Name from Text?
  • Index(es):
    • Date
    • Thread