Mac OSX - Allow for user input in shell script via GUI or Prompt

From what I understand I would recommend you look in to Applescript as this will allow you to have a GUI Interface as well as executing 'SHELL' commands.

First of all I would open 'Script Editor' program that comes preinstalled on Mac's

This is an example script which asks for the user's name then says it via executing a shell command "say name"

display dialog "What is you name? " default answer "" buttons {"Say It"} default button 1
text returned of the result
do shell script "say " & result

You may also append with administrator privileges to the do command which will make it run with administrator privileges (Ask for administrators username and password)

Example:

display dialog "What is you name? " default answer "" buttons {"Say It"} default button 1
text returned of the result
do shell script "say " & result with administrator privileges

Hope this helped.


OS X has (mostly) all batteries included, just use it. :)

For this, you could use the Automator.app. With the Automator, you could create executable application (e.g. your.app) what will contains your shell script and also asks for the user inputs.

Example, asking for two inputs: "name" and "year" you should do the following:

  • Launch Automator
  • Choose "Application"
  • Click the "Library" button on the toolbar, if the Library is hidden
  • Drag the following actions from the Library to the workflow
    • "Ask for text"
      • Enter the question: "First and Last name:"
      • click "Require an answer"
    • "Set value of variable"
      • Create new variable "name"
    • "Ask for text"
      • "Enter the second question, e.g. "Enter year:"
      • Add the default e.g. 2015 (if want)
      • click the "Require an answer" checkbox
      • check the "Ignore this actions input" in the "Options"
    • "Get value of variable
      • Select "name"
    • "Run Shell script"
      • select "Pass inputs" -> "as arguments"
      • copy & paste your shell-script into the window, like:
year="$1"
name="$2"
echo "Report for name: $name year: $year"
  • also, you can add the final action "copy to clipboard", e.g. the output from the shell script will go into the clipboard.

Save the script (you will get an name.app - standard OS X .app application), just add it into .dmg or create a .zip from it and you could deploy it.

Everything is much faster to do, as read this answer. ;)


For the record, I tested (on OS X Yosemite) the following script (namescript), which uses the read command to accept user input. After chmod +x namescript, double clicking from Finder properly launched the script and accepted user input.

    #! /bin/bash

    echo "Please enter your name"
    read name
    echo "Your name is $name"

It is important to choose a name for the script with either no extension (as in namescript) or a .command extension (namescript.command). By default, using .sh (namescript.sh) causes double clicking to open the script in a text editor, as noted in the OP.

Tags:

Macos

Shell

Bash