The standard sage distribution now comes with the ability to create a clickable Mac application by running the following (it will take a long time).

SAGE_APP_BUNDLE=yes ./sage -bdist VERSION


This page describes one way of turning the standard sage directory into a clickable Mac Application.

The Platypus program is used to make things easier. Download Platypus, and install it along with its command line tool. Using the Platypus command line program makes this process scriptable.

A Mac application needs an Icon. I made one from the Sage web site logo using the Icon Composer program that is installed with Apple's Xcode tools. Name the icon sage.icns

Next, a small Python program called sageMac.py is useful to set things up. This program will create a .command file so that OS X will start Sage in the Terminal.app program, update the SAGE_ROOT variable to reflect the current location of the program, and finally start the program. Updating SAGE_ROOT automatically allows users to drag the program to install it.

This is the full contents of sageMac.py:

   1 import sys, commands, os
   2 
   3 # path to inside of Sage.app
   4 appname = '/'.join(sys.argv[0].split('/')[:-3])
   5 sageDir = appname + '/Contents/Resources/sage'
   6 sageScriptOrig = sageDir + '/sage'
   7 sageScriptNew = sageDir + '/sageMac.command'
   8 
   9 # copy Sage startup script to end in .command so that OS X will launch it
  10 commands.getoutput('cp -f %s %s' % (sageScriptOrig, sageScriptNew))
  11 
  12 # edit sageScriptNew to reflect current location (sageDir)
  13 f = open(sageScriptNew)
  14 lines = f.read()
  15 f.close()
  16 
  17 splitLines = lines.split('"')
  18 newLines = [splitLines[0], sageDir]
  19 newLines.extend(splitLines[2:])
  20 newLines = '"'.join(newLines)
  21 
  22 f = open(sageScriptNew, 'w')
  23 f.write(newLines)
  24 f.close()
  25 
  26 # run the script
  27 os.execl('/usr/bin/open', '/usr/bin/open', sageScriptNew)

With sage.icns, sageMac.py, and a current sage directory for your platform (e.g., Intel, OS X 10.5), the Mac application can be built with this command:

platypus -t python -i sage.icns -f sage sageMac.py Sage.app

This takes a while to execute, because it has to copy all of Sage, but when it completes there should be a nice clickable Mac application called Sage.app in your folder. You can drag this to your Applications folder to install it, and double click on it to start Sage in a terminal window.

It is good to be able to run Sage from the command line as well. This can be enabled by creating a symbolic link to the sageMac.command script inside of the Mac application, for example (one long line):

sudo ln -s /Applications/Sage.app/Contents/Resources/sage/sageMac.command /usr/local/bin/sage

Be aware that the sageMac.command file will not exist until Sage.app is run for the first time, so don't try to create the link until after Sage has been run once.

Finally, the Sage.app program can be packaged up for distribution in a compressed disk image with a readme.txt file using a procedure like this:

# make a compressed Sage disk image for distribution (specific to this platform):
export platform="10.5intel"
export version=`sage/sage --version | sed s/,// | cut -f 3 -d' '`
hdiutil create -fs HFS+ -size 1500m -volname Sage Sage
hdiutil attach Sage.dmg
cp -R Sage.app /Volumes/Sage
# copy anything else you want in the distribution, such as a 'readme.txt' file
cat > /Volumes/Sage/readme.txt <<README
Drag Sage to your Applications folder

Starting Sage from the command line is also possible if you create
a link to it on your path. For example:

sudo ln -s /Applications/Sage.app/Contents/Resources/sage/sageMac.command /usr/local/bin/sage

(you must double-click Sage once to run it before sageMac.command is created)
README
hdiutil eject /Volumes/Sage
hdiutil convert -format UDZO Sage.dmg -o Sage-${version}-${platform}.dmg
rm -f Sage.dmg

When this completes, you will have a typical Mac program disk image (Sage-2.10.1-10.5intel.dmg, 210 MB) that is ready to distribute.

The sage.icns and sageMac.py files are attached to this page. These are the same files and instructions attached to Ticket #1731 on the Sage Issue tacker.


Another method of creating a Mac Application is to use the freeware (though not currently free as in speech) application Fluid to create a Site Specific Browser (which I call Sage.app). This has the advantage that Sage will be a separate application. Similar things could probably be achieved with Prism, but I wanted a tighter integration with OS X, and I don't really like Firefox.

To do this you must download Fluid and create a new browser pointing to http://localhost:8000/. You can now use your site specific browser to access the sage notebook, but it's still not ideal in the sense that you must start the sage server before opening Sage.app. To work around this you can replace the executable in Sage.app with a script which launches the sage server and then opens the application. For example I used the following simple shell script after having moved Sage.app/Contents/MacOS/FluidInstance to Sage.app/Contents/MacOS/FluidInstance.bin. I made it use an insecure notebook, since that is simpler and probably acceptable on a single user machine (correct me if I am wrong).

# TODO: check here for environment variables that might indicate where
# a user-installed version of sage is

# sage doesn't handle /../ well, so we eliminate them here
PACKAGE_DIR=$(echo $BASH_SOURCE | grep -o '.*\.app')

SAGE_REL=Contents/Resources/sage
SAGE_BIN="$PACKAGE_DIR/$SAGE_REL/sage"
SAGE_LOG="$PACKAGE_DIR/$SAGE_REL/sage.log"
FLUID_BIN=$BASH_SOURCE.bin

export SAGE_BROWSER="$FLUID_BIN"

# TODO: must empty the log or something
curl localhost:8000 > /dev/null 2> /dev/null
if [ $? != 52 -a $? != 0 ]; then
    # This should probably be improved upon some
    "$SAGE_BIN" -c "notebook(open_viewer=False, secure=false)" >/dev/null &
fi

# Stall until the server has started.  If there is a problem this will loop forever...
curl localhost:8000 >/dev/null 2>/dev/null
while [ $? != 52 -a $? != 0 ]; do
    sleep 1
    curl localhost:8000 >/dev/null 2>/dev/null
done

exec $FLUID_BIN

Fluid now has an open sourced counterpart Fluidium that could be used instead.

-IvanAndrus

SageMacApplication (last edited 2010-03-25 14:13:17 by IvanAndrus)