MacOS – How to set up a shell script as a wrapper to the main executable of an app

applescriptapplicationsemacsmacos

I have an application that I want to launch via a shell script that does some setup and then launches the app itself with some specific options. Ideally, this script would be run whenever the application would normally be launched, i.e. when I click on it in the dock, double-click in the Applications folder, launch from QS, or even click on a file that opens in this app.

Right now I have an applescript wrapper "App" for my script, but the downside is that it appears as a separate app from the main app that sits in my dock, so I have to remember to not click on the main app's icon, but launch this alternate app instead.

So is this something that is possible to do? One idea I had is to rename the main executable in the app to something else, and then replace it with my script. Is that a reasonable thing to do, or is there a better solution?

For those who are curious, I am trying to set up Emacs.app to launch a daemon and then open using emacsclient. Basically, I want to have this script launch instead of emacs (and I'm going to modify the script to take file arguments and open them as well).

Best Answer

I would start with a tool like Platypus to make your script have all the accoutrements of an App and test to see that things work as expected.

Automator also has a nice GUI for deciding how to handle arguments to a script - and we have several questions here that explore how to pass arguments into and out of an Automator action / applet.

Without more details on how your daemon is designed and how you launch it, I can only pick away at some of the elements to your solution, but since anyone can edit this answer to provide more details (or answer a stand-alone answer) - perhaps this can get the ball rolling to get you a solution.