This discussion is locked
Mark92691

Q: How to make a shell script double-clickable

I just installed a program that currently must be launched from a shell script, which, in turn, must be run from Terminal. Is there an easy way to create an alias with an icon that, when double-clicked, runs the script to launch the program? This would be trivial in Windows with a shortcut to a batch file, but I am having the darndest time figuring out how to do it in OS X. Google searches turned up a number of suggestions, all of which appear to be obsolete or accompanied by several follow-up posts to the effect that "I tried this and it didn't work." I tried a few of them, myself (such as changing .sh to .command), and they didn't work for me, either.

Any thoughts?

TIA,

Mark

MacBook Pro, Mac OS X (10.6.4)

Posted on Sep 30, 2010 10:21 PM

Close

Q: How to make a shell script double-clickable

  • All replies
  • Helpful answers

  • by red_menace,Helpful

    red_menace red_menace Sep 30, 2010 10:55 PM in response to Mark92691
    Level 6 (15,526 points)
    Desktops
    Sep 30, 2010 10:55 PM in response to Mark92691
    If you give your script a .command extension and perform a chmod 777 on it (do make it executable), it will open and run in the Terminal when double-clicked.
  • by MrHoffman,Helpful

    MrHoffman MrHoffman Oct 1, 2010 6:15 AM in response to red_menace
    Level 6 (15,627 points)
    Mac OS X
    Oct 1, 2010 6:15 AM in response to red_menace
    The following will work, but also opens the file up for write access to everybody. That write access may or may not be a desirable side-effect here.

    chmod 777 file


    With the "newer" chmod syntax, you can enable just execute (x) access for all (user, group, other) modes with this:

    $ chmod ugo+x file


    Or yes, sort out the individual bits in the mask and set those (rather than the 777 Big Hammer) with a numeric-syntax command.
  • by BobHarris,

    BobHarris BobHarris Oct 1, 2010 6:18 AM in response to Mark92691
    Level 6 (19,405 points)
    Mac OS X
    Oct 1, 2010 6:18 AM in response to Mark92691
    As red_menace points out, there is the .command suffix.

    You can also download Platypus (free download)
    <http://www.macupdate.com/info.php/id/12046/platypus>
    which can turn any script or Unix command line program into a double-clickable app.

    You can use Applications -> Automator -> Run Shell Script, and save the workflow as an application.

    I kind-of-like Platypus as it lets you control how you display any output from your script. The .command approach will launch Applications -> Utilities -> Terminal, and leave the window sitting on your screen. If you do not need the Terminal session, you have the chore of closing Terminal after running your script.
  • by Mark92691,

    Mark92691 Mark92691 Oct 5, 2010 11:25 PM in response to BobHarris
    Level 1 (5 points)
    Oct 5, 2010 11:25 PM in response to BobHarris
    O.K., I can now run the script by double-clicking, but it fails. If run from Terminal, it succeeds. The purpose of the script is to launch a vendor (Oracle) program. Here's the script:

    #!/bin/bash
    cd "`dirname $0`"/bin && bash datamodeler.sh $*

    It's run from the main vendor directory, in which sits a "bin" sub-directory, in which sits a shell script named "datamodeler.sh". I have (also) made it executable.

    Here's the result of launching the outer script with a double-click:

    ==========

    Last login: Tue Oct 5 23:19:02 on ttys001
    /Applications/Oracle\ Data\ Modeler\ 2.0/datamodeler.sh.command ; exit;
    Macintosh-6:~ mbwallace$ /Applications/Oracle\ Data\ Modeler\ 2.0/datamodeler.sh.command ; exit;
    usage: dirname path
    bash: datamodeler.sh: No such file or directory
    logout

    [Process completed]

    ==========

    Again, if the outer script is run from Terminal, the end result is that the vendor program is launched.

    I'm guessing that this is a relatively simple (but not to me) problem with the interaction between the Unix environment and the shell script.

    TIA for any ideas,

    Mark
  • by BobHarris,Solvedanswer

    BobHarris BobHarris Oct 6, 2010 5:59 AM in response to Mark92691
    Level 6 (19,405 points)
    Mac OS X
    Oct 6, 2010 5:59 AM in response to Mark92691
    Yours:

    #!/bin/bash
    cd "`dirname $0`"/bin && bash datamodeler.sh $*

    Change the above to

    #!/bin/bash
    cd "$(dirname "$0")/bin" && bash datamodeler.sh "$@"

    You have spaces in your path so the dirname is failing.

    Also I've replaced the $* with "$@" which as magical properties that preserve quoted call arguments.

    Message was edited by: BobHarris
  • by Mark92691,

    Mark92691 Mark92691 Oct 7, 2010 12:02 AM in response to BobHarris
    Level 1 (5 points)
    Oct 7, 2010 12:02 AM in response to BobHarris
    Working. Thank you.