How to create simple Mac applications from shell scripts
Basically, a Mac application has a .app
./file extension, but it's not really a file - it's a package. We can view the contents of an application by navigating to it in the Finder, right-clicking it, and then selecting " Show Package Contents ".
The internal folder structure may vary from application to application, but we can be sure that every Mac application will have a Directory folder that contains a MacOS subfolder. Within the MacOS directory, there is an extensionless file with the exact same name as the application itself. This file can really be anything, but in its simplest form is a shell script. It turns out that this folder/file structure is all you need to create a functional application!
Enter appify
Following this discovery, Thomas Aylott came up with a clever "appify" script that allows us to easily create Mac applications from shell scripts. The code is as follows:
#!/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";
If you're used to UNIX, it's appify
pretty simple to install and use. (I'm not, so I had to figure this out.) Here's how to install it:
- Save the script to a directory in your PATH and name it appify (no extension). I chose to put it in /usr/local/bin. You will need root privileges.
-
Launch Terminal.app and type
sudo chmod +x /usr/local/bin/appify
Make appify executable without root permissions.
After that, we can create an application based on any shell script by simply launching Terminal.app and typing:
$ appify your-shell-script.sh "Your App Name"
Obviously, this will create a standalone application called Your App Name.appyour-shell-script.sh
which will execute the script.
After that we can add a custom icon to the application very easily if needed.
Add a custom application icon
.icns
Create a .txt file or 512x512 PNG image with the icon we want and copy it to the clipboard⌘ + C
. (Alternatively, copy it from an existing application as described in steps 2 and 3.)-
Right-click the file whose icon you want to change
.app
and choose Get Info (or select the file and press⌘ + I
). - Select the app icon in the top left corner by clicking it once. If you did it right, it will get a subtle blue outline.
-
Now click
⌘ + V
(Paste) to overwrite the default icon with the new one.
请注意
, this works for any file or folder, not just .app files.
Example
Chrome/Chromium Bootloader
I like to run with some command line switches or flags enabled Chrome/Chromium
. On Windows, we can create a shortcut and set the parameters we want in its properties; on Mac, we need to launch it from the command line every time. Well, not anymore :)
#!/usr/bin/env bash
/Applications/Chromium.app/Contents/MacOS/Chromium --enable-benchmarking --enable-extension-timeline-api&
The final &
is not a typo; it is used to ensure that Chromium is launched in a separate thread. If not &
, Chromium will exit immediately when we exit Terminal.app.
Start a local web server from a directory
Let's say we are working on a project and want to debug it from a web server. The following shell script will use Python to start a local web server from a specific directory and open the index page in the default browser of our choice. Once applied, you don't even need to open the terminal anymore.
#!/usr/bin/env bash
cd ~/Projects/Foo/
python -m SimpleHTTPServer 8080 &> /dev/null &
open http://localhost:8080/
Needless to say, the possibilities are endless. As another example, you could easily create an application that minifies all JavaScript and CSS files in a specific folder. Got any great ideas? Let us know by leaving a comment!
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
PBKDF2+HMAC Hash Conflict
Publish Date:2025/04/06 Views:55 Category:OPERATING SYSTEM
-
Cryptocurrency enthusiast Christian 'CodesInChaos' Winnerlein once wrote: plnlrtfpijpuhqylxbgqiiyipieyxvfsavzgxbbcfusqkozwpngsyejqlmjsytrmd and eBkXQTfuBqp'cTcarg* have the same PBKDF2-HMAC-SHA1 hash. This intrigued me, so I decided to find
Differences between Login and Non-Login Shells
Publish Date:2025/04/05 Views:179 Category:OPERATING SYSTEM
-
This article explains the difference between login shells and non-login shells in UNIX-based systems. What is Shell in UNIX based systems The shell in UNIX based systems is the interface between the user and the kernel of the operating syst
Purpose of Shell Script Headers
Publish Date:2025/04/05 Views:63 Category:OPERATING SYSTEM
-
In programming, a shebang is a series of tags and exclamation marks #! at the beginning of a file. It then provides the entire path to the interpreter that will execute the file's code /bin/bash . After that, comes the code itself. The inte
Open Git Bash on Mac
Publish Date:2025/03/31 Views:97 Category:Git
-
Managing Version Numbers in GitGit is the most popular and well-known free version control system used by developers to help them work on various programs safely and efficiently in a team. They can easily keep track of their own projects wi
Updating Git on Mac
Publish Date:2025/03/28 Views:182 Category:Git
-
When working on Git, you should stay updated with the latest version to get its latest features. This article will discuss how to install and update the latest version of Homebrew and Git on your personal computer. Homebrew on Mac Homebrew
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:99 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Increasing variable value in Shell programming
Publish Date:2025/03/23 Views:96 Category:OPERATING SYSTEM
-
In this article, we will explain how to increment a variable in bash. We will also learn about different types of increment operators used in bash scripting in Linux. Let's start with shell programming in Linux and learn how to increment a
Open Emacs in Bash
Publish Date:2025/03/21 Views:143 Category:OPERATING SYSTEM
-
This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit