Mac OSX - Allow user input to shell script via GUI or prompt

first post, so please let me know how I could improve ..

I created a wrapper script that requires a person to enter their name and then generate a report. The script runs as needed, chmod'd into the script executable and run from the terminal. But I would like to expand it and do it with a double click, instead of instructing people to run it from the terminal.

I tried wrapping the script in Platypus, which makes it easier to run, but it doesn't allow user input, which is critical.

I just found cocoaDialog but my main problem is that it will provide the functionality I need, and if it does, if everyone else doesn't install it ...

Has anyone ever been in this situation or could suggest and pointers.

Thank.

+3


source to share


3 answers


For the record, I tested (on OS X Yosemite) the following script ( namescript

), which uses a command read

to accept user input. After chmod +x namescript

double clicking on Finder, the script ran correctly 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 without an extension (as in namescript

) or extension .command

( namescript.command

). By default, using .sh

( namescript.sh

) triggers a double click to open the script in a text editor, as the OP pointed out.

+9


source


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

For this you can use Automator.app

. With Automator, you can create an executable application (for example your.app

) that will contain your shell script and also request user inputs.

For example, ask for two inputs: "name" and "year", you should do the following:

  • Launching Automator
  • Select "Application"
  • Click the Library button on the toolbar if the library is hidden.
  • Drag the following actions from the Library into the workflow
    • "Request text"
      • Enter the question: "First and last name:"
      • click "Require response"
    • "Set variable value"
      • Create a new variable "name"
    • "Request text"
      • "Enter the second question, for example" Enter the year: "
      • Add the default, eg. 2015 (if you like)
      • check the box "Require response"
      • check the box "Ignore input actions" in "Options"
    • "Get the value of a variable
      • Select "name"
    • "Run shell script"
      • select "Pass input" β†’ "as arguments"
      • copy and paste your shell script into the window, for example:


year="$1"
name="$2"
echo "Report for name: $name year: $year"

      

  • Alternatively, you can add a final copy to clipboard action, for example. the output from the shell script will go into the clipboard.

Save the script (you will get a standard OS X app .app

name.app

), just add it to .dmg

or build .zip

it and you can deploy it.

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

+2


source


From what I understand, I would recommend that you turn to Applescript as it will allow you to have a GUI interface as well as execute "SHELL" commands.

First of all, I would open the Script Editor program, which is pre-installed on the Mac

This is an example script that asks for a username, then says so by executing the "say name" shell command

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 can also add with administrator privileges

to the do command which will make it run with admin privileges (ask for admin 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 it helped.

+1


source







All Articles