10

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?

Michael
  • 8,362
  • 6
  • 61
  • 88
SpacemanTrip
  • 103
  • 1
  • 1
  • 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. – hek2mgl Jun 15 '15 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. – SpacemanTrip Jun 15 '15 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. – hek2mgl Jun 15 '15 at 21:48
  • Btw, why don't you use Google before posting a question? I found this without problems: http://stackoverflow.com/questions/5125907/how-to-run-a-shell-script-in-os-x-by-double-clicking – hek2mgl Jun 15 '15 at 21:51
  • I know how to make it executable. The issue wasn't that..I need to deploy it and even making it executable, if ppl don't have .sh associated with terminal it will open in text editor. I needed to make this as simple as possible for non-technical users. – SpacemanTrip Jun 16 '15 at 16:05
  • 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. – SpacemanTrip Jun 16 '15 at 16:24

3 Answers3

19

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.

scottgwald
  • 579
  • 4
  • 9
  • 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. – SpacemanTrip Jun 16 '15 at 16:26
  • 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. – scottgwald Jun 17 '15 at 02:29
  • 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. – SpacemanTrip Jun 17 '15 at 22:00
  • Yes, you'd want to package it as a zip in order to keep it executable. – scottgwald Jun 18 '15 at 04:48
  • 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. – Demis May 31 '16 at 23:34
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. ;)

clt60
  • 62,119
  • 17
  • 107
  • 194
  • 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. – SpacemanTrip Jun 15 '15 at 21:38
  • 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. – SpacemanTrip Jun 16 '15 at 15:59
  • Please note: That making the script executable and making it a .command ext seems to have also done the trick for me. – SpacemanTrip Jun 16 '15 at 16:27
  • @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). :) – clt60 Jun 17 '15 at 10:26
  • 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... – SpacemanTrip Jun 17 '15 at 21:53
  • See, in the automator you can run apple script, perl/bash scripts and it handles variables right (just need to know how - it has own logic) :). Maybe, if you post an more precise question could help more precisely. – clt60 Jun 18 '15 at 05:59
  • It's not working for me -- the bash step runs and just produces a system beep, and the code itself does not seem to execute. – Ben Wheeler Aug 04 '23 at 17:42
  • Seems like this is not actually setting the variables as desired. – Ben Wheeler Aug 04 '23 at 17:45
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.

Ashley Primo
  • 161
  • 6
  • 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. – SpacemanTrip Jun 15 '15 at 21:39
  • 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. – SpacemanTrip Jun 17 '15 at 22:02
  • 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) – Ashley Primo Jun 19 '15 at 03:30