2

I am running OSX, and I am making an app that has a Python program in the Resources folder, the Bash script calls the Python script to run when the app is launched. The script is in the MacOS folder within the app, named the same as the app. I have this Bash script:

#!/bin/bash
clear
python /Applications/Converter.app/Contents/Resources/Converter.py

I thought that the Bash script would execute when the app was launched, thus causing the python script to run through a terminal window. However when I double click the app, nothing happens!

So basically, I want the bash script to open the Python program in a terminal window so the user can interact with it.

What am I missing here?

5
  • How would running in the background help? Sep 3, 2015 at 17:31
  • When I double click the app, nothing happens! I want the bash script to open the python program in a terminal window so the user can interact with it Sep 3, 2015 at 19:14
  • Are you missing an "s" in "/Applications/" in your path in that script?
    – Spiff
    Sep 3, 2015 at 22:22
  • let me check....that may be a typo when I typed in the script to the question. Sep 4, 2015 at 15:02
  • no, the script has a "s", I'll edit the question Sep 4, 2015 at 15:03

3 Answers 3

3

Here's a better solution to your problem if you absolutely need it to be a .app bundle:

In your Converter.app/Contents/MacOS/Converter script, launch your Python script with the "open" command:

open $(dirname "$0")/../Resources/Converter.py

(I did a relative pathname trick above so you're not hard-coding an absolute path that would break if you moved your .app around.)

Make sure your Resources/Converter.py script has a Python shebang at the top:

#!/usr/bin/python

Make sure both MacOS/Converter and Resources/Converter.py are marked executable (use chmod a+x filename on them).

I tested this setup and the open command successfully figured out that a .py file should be opened in Terminal without me doing anything. I don't think I've previously associated .py files with Terminal.app before on this system, so I think these instructions wouldn't need any further steps even on a clean system.

0
3

The easiest way to make a double-clickable script that runs in a new Terminal window is to end the file name with ".command". Then you can double-click on the script file itself, no need for a .app bundle/package.

If you want to execute bash commands before Python, you can put both in the same file but wrap the Python code in a bash exec statement. TCL scripters do it this way a lot. If you just want Python code and no bash code, change the shebang at the top to #!/usr/bin/python (or wherever Python lives on your system).

I personally don't like anything that presents itself as a .app but actually runs in a Terminal window instead of being its own real app. The only time I've seen this done was the Ixia Chariot endpoint for OS X. I haven't looked at it in a few years, but it may still work that way. It's a free download. You might want to grab it and look at how they did it and maybe post an Answer to your own question. I think their solution involved packaging a Terminal settings file in their app. I think it caused a separate instance of Terminal.app to get launched with their weird custom settings, which was annoying in a fresh and exciting way. :)

1
  • I need it to be an app. When I change it to Converter.command, it says that the app can't be launched because it is incomplete or corrupt. Sep 4, 2015 at 18:00
0

Try changing your script to have .command or .tool extension, but possibly it could not work in the recent OS X.


However if you need the script to be an app, you should create one.

The basic structure of a Mac app is:

MyApp.app/
   Contents/
      Info.plist
      MacOS/
      Resources/

Then copy your script into MacOS/ (with the same name as your app), which should contain the application’s standalone executable code. All other resources goes into Resources/.

Please have a look at appify script by Thomas (or Mathias version of it) which basically goes like:

#!/usr/bin/env bash

APPNAME=${2:-$(basename "$1" ".sh")}
DIR="$APPNAME.app/Contents/MacOS"

if [ -a "$APPNAME.app" ]; then
  echo "$PWD/$APPNAME.app already exists :("
  exit 1
fi

mkdir -p "$DIR"
cp "$1" "$DIR/$APPNAME"
chmod +x "$DIR/$APPNAME"

echo "$PWD/$APPNAME.app"

Usage:

appify foo.sh "Bar"

Source: How to create simple Mac apps from shell scripts


Alternatively you can use:

  • DropScript which lets you create new applications (such as scripts written in sh, perl, etc.).
  • Platypus - an app which creates native Mac OS X applications from interpreted scripts such as shell scripts or Perl, Ruby and Python programs.
  • Create it in some IDE (such as Eclipse) which have such options to do it.

Or create it manually by:

  • create the app folder structure,
  • place your script into: MyApp.app/Contents/MacOS/MyApp.app
  • make the file executable

In other words (Gino):

  1. Create your shell script (ex command.sh)
  2. Give it execution permissions (chmod 755 ./command.sh)
  3. Get Info → Open with: → (associate the Terminal app)
  4. Get Info → (put the icon you want on it)

See also:

1
  • I don't think you tested your own instructions to see if they would meet OP's requirements. These instructions would cause the shell script to get executed, but they would not cause it to be executed within a Terminal window like OP wants. DropScript is from 2002 and is PowerPC only. Platypus may be a viable solution, but OP seems to want to figure out how to do it himself.
    – Spiff
    Sep 4, 2015 at 23:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .