Create mac app from a shell script

mac-app featured image

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:

  1. An executable shell script which will make your shell script as an application.
  2. Your actual shell script to make it as an application.
  3. Add an icon to your application. (Not Mandatory but looks cool like real app)

Let’s walk through these steps one by one:

  1. An executable shell script which will make your shell script as an application:

    #!/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 .
    • Then make the above file as executable with the command from command line/Terminal: 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  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’
  2. 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"
  3. 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:

Mac application creation demo

 

I hope this post will help you learn how to create mac app from a shell script.

References:

About the Author

Sandeep Agrawal
working @ Kalpavruksh Technologies as Software Engineer. I am a simple and straightforward person adaptable to any environment and work culture. Like to work in a creative and challenging environment using cutting edge technologies where I could constantly learn and successfully deliver solutions to problems.

Be the first to comment on "Create mac app from a shell script"

Leave a comment

Your email address will not be published.


*