Ever thought of having an application to run it directly instead of multiple shell commands?
And won’t it be cool to just convert your shell script into a Mac application and use it just like any other mac applications?
So let’s learn today to create mac app from a shell script.
Before jumping into the application creation, we will quickly see how an application folder structure in Mac looks like:
The bare minimum folder structure is something like below that you will find for any applications in Mac.
(app-name).app
├── Contents
│ └── MacOS
│ └── (app-name)
└── Icon
So now let’s get started. We need mainly three things for our application:
- An executable shell script which will make your shell script as an application.
- Your actual shell script to make it as an application.
- Add an icon to your application. (Not Mandatory but looks cool like real app)
Let’s walk through these steps one by one:
- An executable shell script which will make your shell script as an application:
#!/usr/bin/env bashAPPNAME=${2:-$(basename "${1}" '.sh')};DIR="${APPNAME}.app/Contents/MacOS";if [ -a "${APPNAME}.app" ]; thenecho "${PWD}/${APPNAME}.app already exists. Provide some unique name if you want to create it anyway. ";exit 1;fi;mkdir -p "${DIR}";cp "${1}" "${DIR}/${APPNAME}";chmod +x "${DIR}/${APPNAME}";echo "${PWD}/$APPNAME.app";#!/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. Provide some unique name if you want to create it anyway. "; exit 1; fi; mkdir -p "${DIR}"; cp "${1}" "${DIR}/${APPNAME}"; chmod +x "${DIR}/${APPNAME}"; echo "${PWD}/$APPNAME.app";#!/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. Provide some unique name if you want to create it anyway. "; exit 1; fi; mkdir -p "${DIR}"; cp "${1}" "${DIR}/${APPNAME}"; chmod +x "${DIR}/${APPNAME}"; echo "${PWD}/$APPNAME.app";
- Create a file with the above script and save it in /usr/local/bin
/usr/local/bin
. - Then make the above file as executable with the command from command line/Terminal: sudo chmod +x /usr/local/bin/appify
sudo chmod +x /usr/local/bin/appify
Note:- You can give it any name and no extension is required for the file.
- For reference, we will give it a name as ‘appify’
- We are saving the file at /usr/local/bin
/usr/local/bin
so that we can directly call ‘appify’ script. If you want to save at any other place like your username directory then you will have to call the script with its full path like ‘/User/your-username/appify’
- Create a file with the above script and save it in
- Your actual shell script to make it as an application:
- Now have your script file with .sh extension ready and run the below command to make you shell script as a Mac application.
appify your-shell-script.sh "Your App Name"appify your-shell-script.sh "Your App Name"
appify your-shell-script.sh "Your App Name"
- Now have your script file with .sh extension ready and run the below command to make you shell script as a Mac application.
- Add an icon to your application:
This one is very simple.- Just copy any image you want as the app icon.
- Right-click your new application select “Get Info”.
- Select the app icon on the top left corner by clicking it just once.
- and then either hit ⌘ + V to paste it or go to ‘Edit’ from menu link and select paste.
- With this, it will overwrite the default icon with your new icon.
Now your application is ready to use just like any other application.
Below is a quick simple demo example to see the above process in action:
I hope this post will help you learn how to create mac app from a shell script.
References:
Hello,
Thank you for this page.
It worked fine for creating the app. But when I double click on the app, I can see on my dock that something is starting to open and then quickly close. My app don’t launch. The only way it works is when I go on terminal and type “open MyApp.app”.
Could you help on that?
Thanks
Can you please try adding #!/bin/bash at the top of your shell script (.sh file), generate the app again, and check? (if not working then try with #!/usr/bin/env bash as well)
It may be due to the mac version.
I got the below error and it worked by adding “#!/bin/bash” on the top of my shell script.
“You can’t open the application “testapp.app” because this application is not supported on this Mac.”
Let me know if this works and I will update the post content. Thanks.
Hi,
Thank you for the suggestion but it still does not work. I have tried changing the shebang, even using “/bin/zsh” or “/usr/bin/env zsh”, given that I am using zsh on iTerm2.
The script executed is a single line: “python /full_path_to_python_app/my_application.py”
If executed from terminal it works fine.
I am using MacBook Pro MacOS Monterey version 12.0.1
Any other suggestion?
I finally found the error: I had to specify the full path to my python installation. Writing just ‘python’ in the script works fine when executed from terminal but not when I click on the app… If someone has an explanation for that…
Thank you!
I think it is because the python path is set in your environment so just ‘python’ worked from the terminal and the app being a standalone application will need a full path for any other apps/software.
Glad that you got it fixed Tom. thanks for the details and updates.