I’m trying to use Mono to make a portable application that works on Windows, Mac, and Linux, but I want to have a standard DMG file that allows users to simply click and drag my application to their Applications folder on their Mac. In the best answer to this post I found that by creating an executable script beginning with #! in the MyAppName.app/Contents/MacOSX/ folder, it could run whatever I liked.
How do I get a script to actually run?
I made a blank “Hello World” Cocoa App in XCode, and copied the .app file to the Applications directory, and everything worked great. But when I replaced the executable in the MacOSX folder with a script starting with #! it didn’t run the script.
I tried chmod 755 myscript, I tried deleting the old executable file and renaming my script to be the old filename, but all that served to do was stop the app from running at all. The permissions of the script, and the filename, are both correct.
So then I assumed that maybe scripts wouldn’t work, and XCoded a command line utility, again that would simply spout “Hello World!”, but still it didn’t run.
The “.app” extension is formally called an Application Bundle, and they have some pretty good documentation, but none of it seems to tell me why my approach isn’t working.
Here is the .app bundle that I’m working on. Inside the MasOSX directory you’ll find hello (the Hello Word command line utility) and script (a one line script with a #! as the first line) and XCodeTest (a Hello World cocoa app).
Scripts in Unix
Well, first of all, the correct syntax for the first line of a script is
For instance
Using the
opencommandHowever, this won’t help you because the
opencommand does not support shell scripts as launchers. You need to write some C / Objective-C code and compile it into a native executable. Simply usingsystem()orexecv()inside that tool won’t work either, you’d have todlopen()the Mono runtime.Look at monodevelop/mainbuild/MacOSX/monostub.m for an example. This is what MonoDevelop uses to launch MonoMac application.
When you write a Cocoa app with XCode, it will compile your app into native code and put the resulting binary as launcher in the
Contents/MacOSdirectory.The easy solution
Why don’t you simply create a MonoMac application with MonoDevelop? This would automatically create the
.apppackage for you – no need to fiddle around with scripts or custom launchers.