[TxMt] Re: Ruby + UI.dialog question.

Matt Neuburg matt at tidbits.com
Fri Sep 25 18:05:48 UTC 2009


On 9/24/09 6:35 PM, in article C6E16C5A.77B8%dru at summitprojects.com, "Dru
Kepple" <dru at summitprojects.com> wrote:

> Here's some stripped down code that works:
> 
> TextMate::UI.dialog(:nib => nib, :parameters => {}, :center => true) do
> |dialog|
>   dialog.parameters = {'username'   => "moo"}
>   dialog.wait_for_input do |params|
>     puts params["username"]
>   end
> end
> 
> But then I go to try and actually use something with my params hash, like
> putting it into a variable declared earlier:
> 
> username = ""
> TextMate::UI.dialog(:nib => nib, :parameters => {}, :center => true) do
> |dialog|
>   dialog.parameters = {'username'   => "moo"}
>   dialog.wait_for_input do |params|
>     username = params["username"]
>   end
> end
> puts username

Read the docs. :) Okay, there aren't really docs, but there is a very
helpful comment explaining how to use wait_for_input:

          # wait for the user to press a button (with performButtonClick: or
returnArguments: action)
          # or the close box. Returns a dictionary containing the return
argument values.
          # If a block is given, wait_for_input will pass the return
arguments to the block
          # in a continuous loop. The block must return true to continue the
loop, false to break out of it.

So, you're blocking because that's what the block does. It's just doing what
you told it to do. But for your purposes, you don't need a block. Just
capture the result of the call. So, for example (using a built-in nib
example):

require "#{ENV['TM_SUPPORT_PATH']}/lib/ui"
nib = "#{ENV['TM_SUPPORT_PATH']}/nibs/RequestString.nib"
username = ""
TextMate::UI.dialog(:nib => nib, :parameters => {}, :center => true) do
|dialog|
  dialog.parameters = {'string' => "Matt"}
  username = dialog.wait_for_input
end
puts username["returnArgument"]

Hope that gives you enough to move forward... m.

-- 
matt neuburg, phd = matt at tidbits.com, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
http://www.tidbits.com/matt/default.html#applescriptthings






More information about the textmate mailing list