Snippets code from my daily experience

July 12, 2007

How to detect and run MacOSX executable files with XUL

Filed under: macosx,xul — dafi @ 8:22 pm

XUL allows to detect if a file is executable (Windows batch, Unix script, regular executable binary file) simply using the nsIFile::isExecutable method.

All seems high portable and platform independent but isExecutable doesn’t work on MacOSX as reported in Bug 307463 and Bug 322865.

I’ve found a workaround in ViewSourceWith since 0.0.8.2 version.

The solution requires a brief technical explanation.

MacOSX uses the so called CFBundle files, these files are stored inside a directory that resides under a standard path:

/LEAD_PATH/APP_NAME.app/Contents/MacOS/Info.plist

where LEAD_PATH should be for example /Application and APP_NAME should be for example Firefox

So the Firefox bundle can be found in /Application/Firefox.app/Contents/MacOS/Info.plist

Info.plist is a standard XML file containing many attributes, the CFBundleExecutable points to the executable file name.

The example below shows the content for Firefox Info.plist

<key>CFBundleExecutable</key>
<string>firefox-bin</string>

So the real executable file is /Application/Firefox.app/Contents/MacOS/firefox-bin

ViewSourceWith contains a function that digs inside MacOSX to find the real executale file.

Initially I wrote the code treating Info.plist like a DOM document but at end I used a regular expression as shown below:

var pListText = ... // contains the Info.plist text
var re = /\<key\>CFBundleExecutable\<\/key\>[ \t\n\v\r]*\<string\>[ \t\n\v\r]*(.*)[ \t\n\v\r]*\<\/string\>/

var m = pListText.match(re);
var the_real_exe = m[1];

Apple has many API to access to properties without open manually the Info.plist but to use them I need

– a C compiler for Mac

– write native code to use inside firefox extension

Instead, accessing directly to xml file I simplified the development.
You can find the code written for ViewSourceWith in SVN repository.

Other details about this problem can be found on ViewSourceWith FAQ page.

Blog at WordPress.com.