12

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?

6
  • Add a shebang line on top of the script, like: #!/bin/bash and make the script executable using chmod +x script.sh. That's it. Commented Jun 15, 2015 at 20:55
  • I know my script works perfectly within the terminal when I run it. I am looking for the functionality of being able to double click on the file..have it run and receive input. If I kept the file as a script, users would have to run it from terminal or change file association to launch the script. Commented Jun 15, 2015 at 21:34
  • Hmm, on Linux it is possible to double click an executable shell script to execute it from the desktop. If you want something more sophisticated (icon, description, etc) you need to create a .desktop file (at least in Gnome they are called .desktop). I guess in OSX is something similar. Commented Jun 15, 2015 at 21:48
  • Btw, why don't you use Google before posting a question? I found this without problems: stackoverflow.com/questions/5125907/… Commented Jun 15, 2015 at 21:51
  • 1
    I do owe you an apology though. That link (which I read previously) did have an answer I was looking for as well. which was making it a .command ext. I just didn't happen to read that far down because it wasn't marked as solution. Thank you. Commented Jun 16, 2015 at 16:24

3 Answers 3

22

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It works perfectly when the script is associated to open with terminal. But default is to open in a text editor which could cause issues for me.
Okay, if you leave off ".sh" -- so you have an executable file with no extension -- it opens properly with terminal without fiddling with os associated app settings.
The problem is if I deploy it and someone downloads the script , the permissions are stripped and the user has to chmod on their end after downloading it so they could run it.
Yes, you'd want to package it as a zip in order to keep it executable.
FYI, in OS 10.11+ an error will likely popup saying "Script from unidentified developer". The workaround is to tell your users to (1) Right-Click the file, (2) Hold Shift while the menu is still showing (3) Choose Open, still holding Shift (4) Click Open on the resulting dialog. It will never ask you this again for this file.
3

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

9 Comments

I will give this a try, it seems like I will have to tweak my script because I have my first name and last name variables declared in it..So i will prob take those variables out of the script itself..I'll update when i can try that out.
This solution works with the workflow I need. I am having issues with getting my file to be named based off initials using : initial="$(echo $first_name | head -c 1)" touch ~/Desktop/$initial$last_name but that might just need me tweaking a bit. Thank you to all who helped.
Please note: That making the script executable and making it a .command ext seems to have also done the trick for me.
@SpacemanTrip yes, if for you is OK launch the terminal the .command extension to any script will launch the terminal.app and will run the script inside. (but, no GUI). :)
Actually I abandoned Automator because it wasn't handling variables right and i couldn't get my output file to name itself with my first and last name...
|
2

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.

3 Comments

Applescript was what i felt was best suited for this task as well when I started digging around after creating my script. I will give this a try as well and keep you posted.
It looks like I came back to AppleScript because it has the deployment I want..just I cant get my BASH script to work in it. I am going nuts.
If you are having problems I guessing you may be getting stuck on text returned of the result just remember every question you have should be laid out in that three line structure. (Question -> Data -> Function)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.