Making my own native Mac OSX app without knowing Swift
from python cli tool to MacOS app in 10 minutes
I am a Python developer and a data scientist. I never thought that I would create an desktop app for myself one day. However, this day came out of my expectation and my first desktop app appeared within 10 minutes. It could be yours too!
All you need is a .app folder, and a cute icon!
I’m going to use my little success as an example here. I wrapped a side project product — a pure python command-line pomodoro time management tool , ctimer— into a MacOSX desktop app. (You’re welcome to try it out as well!)
Step 1: Create a folder called ctimer.app (at any location)
mkdir ./ctimer.app/
(we will drap it to Application folder after we finished creating it.)
Step 2: Create a folder called Contents in ctimer.app
See the tree structure below. Create (1) Contents folder, in this folder, create (2) Info.plist (a file), (3) MacOS (a folder), and (4) Resources (a folder).
ctimer.app/
└── Contents
├── Info.plist
├── MacOS
│ └── ctimer_wrapper
└── Resources
└── ctimer.icns3 directories, 3 files
Step 3: In MacOS create a bash file to launch Python script
Here, I called it ctimer_wrapper, but you can use any name. The name will be filled in the Info.plist file (in step 5). Use your favorite text editor tool (mine is vi) to create a file.
vi ./Contents/MacOS/ctimer_wrapper
Copy paste the following into your file. (Or, for your own python cli, change it to path to your python script.)
#!/bin/sh
##########################################
exec /Users/lchen/miniconda/bin/ctimer;
Make sure your file is executable by chmod
chmod +x ctimer_wrapper
Step 4: Find / Make an .icns App icon
In MacOSX, the suffix of icons is .icns. There are many icons for free download online. Always check the license before distributing it to others. Here is an example link of where you can find (GNU General Public License v3.0) icons.
To make your own icon, you can convert a (good enough quality) png file into an “iconset,” then convert it to icns. Here is a python code to do this. You could read more here. It worked for me.
Step 5: Create Info.plist file
Now we have gathered everything! To put them all together, we create a Info.plist file under Contents, and collect the names of the wrapper/launcher & the name of the icon in this file.
vi ./Contents/Info.plist
Copy paste the following into your file. You can observe that:
- Between first <string> contains your bash file name (in MacOS folder)
- Between second <string> contains your icon name (in Resources folder)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>ctimer_wrapper</string>
<key>CFBundleIconFile</key>
<string>ctimer.icns</string>
</dict>
</plist>
Step 6: All done, drag it to dock!
Move it (ctimer.app) to /Applications or ~/Applications (in my case, I have only user but no admin permission on my work computer. As a result, I made a Applications folder available only to myself.)
You will see the icon popping up as soon as you dragged it to another folder. After that, you could also drag it to the dock!
P.s. If you decided to switch the icon after you first dragged it, you need to provide the icon in another way: Open the Applications folder → Right click on the said application → Get Info → Drag the icon to top-left position, where the original icon is (a green (+) will show). The dock icon will change upon re-launching (from dock).
Other available wrapper tools — Platypus
What we just did is not unique, and there are tools to do so as well. Platypus is one (https://sveinbjorn.org/platypus). In my case, since I don’t have admin rights, Platypus ask for it at one point therefore I cannot use it.