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
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Running PHP on Mac
Publish Date:2025/04/12 Views:183 Category:PHP
-
In this article, we'll show you how to run PHP on your Mac. php -S Run PHP on Mac using command PHP is a server-side language. It runs on a server. Therefore, it requires a web server to run. There are different web servers like Apache HTTP
Run Shell Scripts and Open Shell Files in PHP
Publish Date:2025/04/12 Views:88 Category:PHP
-
PHP allows us to use shell_exec(); the shell function to process files. However, if your operating system is Windows, you should consider using the popen() and pclose() functions, because pipes are executed in text mode, which usually preve
How to use clion to debug redis source code on mac system
Publish Date:2025/04/09 Views:172 Category:Redis
-
clion mainly uses cmake + make for compilation. So for redis4, the main thing is to write the CMakeLists.txt file first. CmakeLists.txt file redis4/CMakeLists.txt cmake_minimum_required (VERSION 3.15 ) project (redis4) set (CMAKE_BUILD_TYPE
Mac system uses clion to remotely debug redis4 source code
Publish Date:2025/04/09 Views:129 Category:Redis
-
The remote host uses the Linux system. The first step is definitely to establish a code synchronization mechanism on the local and remote hosts - sftp is the first choice. The second step is to write the CMakeLists.txt file of redis4. There
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Solve the problem of gdb debugging on Mac system
Publish Date:2025/04/07 Views:93 Category:OPERATING SYSTEM
-
On a Mac, you may encounter the following error when using gdb for the first time: (gdb) run Starting program: /usr/ local /bin/order_monitor Unable to find Mach task port for process-id 26551: (os/kern) failure (0x5). (please check gdb is
Mac Modify the host name and prompt in terminal and iterm
Publish Date:2025/04/07 Views:178 Category:OPERATING SYSTEM
-
Commonly used terminals on Mac are terminal and iterm. In many cases, we need to modify the host name displayed in these terminals. As shown in the figure above, we use the command hostname to view the current host name From the results we