Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to make a display box that has a text box that the user can enter a number into that will later be assigned to a variable. How would I do this?

share|improve this question
The low-tech solution, of course, is dialog, which you can install e.g. using Homebrew or MacPorts. – Daniel Beck Apr 8 '11 at 18:00
up vote 3 down vote accepted

Open AppleScript Editor, enter the following and save as script:

tell application "Terminal"
    repeat while true
        set input to display dialog "Enter a number:" default answer ""
        if button returned of input is equal to "OK" then
                return (text returned of input) as number
            end try
        end if
    end repeat
end tell

(we need to tell application, because otherwise osascript doesn't allow user interaction)

Then run like this:

$ osascript path/to/script.scpt

Output of the program is the number the user entered.

Store in bash variable like this:

$ foo=$( osascript path/to/script.scpt )
$ echo $foo
share|improve this answer
Store in bash? What does that mean and why do I have to do it? – JShoe Apr 8 '11 at 18:25
@JShoe Well, what do you want to do with the value the user entered? Probably pass it to a shell script. That's why I showed you how to store it in a variable named foo. Also, it's "bash variable" -- the additional space around bash comes from monotype formatting. – Daniel Beck Apr 8 '11 at 18:26
You could just tell app "Terminal"; repeat while true; display dialog "" default answer ""; try; return (text returned of result) as number; end try; end tell. – user495470 Apr 8 '11 at 18:38
@Lri Thanks, I used one or two parts of that. – Daniel Beck Apr 8 '11 at 18:55
@JShoe Just replace ; with newlines, and maybe compile it in ASE to fix the indentation. $ indicates the command prompt, you're not meant to include it. – user495470 Apr 9 '11 at 7:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .