Differences between revisions 5 and 10 (spanning 5 versions)
Revision 5 as of 2008-03-04 06:37:58
Size: 6372
Editor: IvanAndrus
Comment: Alternate Method of App creation
Revision 10 as of 2022-04-05 05:35:56
Size: 0
Editor: mkoeppe
Comment: outdated
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
This page describes one way of turning the standard ''sage'' directory into a clickable Mac Application.

The [http://www.sveinbjorn.org/platypus 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'':

{{{#!python
import sys, commands, string, os

# path to inside of Sage.app
appname = string.join(string.split(sys.argv[0], '/')[:-3], '/')
sageDir = appname + '/Contents/Resources/sage'
sageScriptOrig = sageDir + '/sage'
sageScriptNew = sageDir + '/sageMac.command'

# copy Sage startup script to end in .command so that OS X will launch it
commands.getoutput('cp -f %s %s' % (sageScriptOrig, sageScriptNew))

# edit sageScriptNew to reflect current location (sageDir)
f = open(sageScriptNew)
lines = f.read()
f.close()

splitLines = string.split(lines, '"')
newLines = [splitLines[0], sageDir]
newLines.extend(splitLines[2:])
newLines = string.join(newLines, '"')

f = open(sageScriptNew, 'w')
f.write(newLines)
f.close()

# run the script
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 [http://trac.sagemath.org/sage_trac/ticket/1731 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 [http://fluidapp.com/ Fluid] to create a Site Specific Browser (which I call Sage.app). This has the advantage that Sage will be a separate application.

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).

{{{
#!/bin/sh

# 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
}}}

I am not sure of the legality of distributing a pre-packaged version, since Fluid is not open source and the GPL is very restrictive, but I would like to put up a pre-packaged version after I sort that out.

-IvanAndrus