Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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
            try
                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
42
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
1  
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. –  ؘؘؘؘ Apr 8 '11 at 18:38
    
@Lri Thanks, I used one or two parts of that. –  Daniel Beck Apr 8 '11 at 18:55
1  
@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. –  ؘؘؘؘ Apr 9 '11 at 7:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.