SpacemanTrip
SpacemanTrip

Reputation: 123

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

I created a shell script which requires a person to input their name and then generates a report. The script works as needed when chmoded into an executable script and run from terminal. But, I would like to deploy it and have it be a double click kind of solution instead of instructing people to run it from terminal.

I tried wrapping the script in Platypus, which makes it easy to launch. But it doesn't allow for input from the user, which is critical.

I just found cocoaDialog, but my main concern is whether it will provide the functionality I need and do so without having everyone else install it.

Has anyone ever been in this situation or can offer any pointers?

Upvotes: 12

Views: 39321

Answers (3)

scottgwald
scottgwald

Reputation: 609

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.

Upvotes: 22

clt60
clt60

Reputation: 63972

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. ;)

Upvotes: 3

Ashley Primo
Ashley Primo

Reputation: 161

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.

Upvotes: 2

Related Questions