MacOS X Tips & Tricks


Now that Apple ships a Unix system with its computers, this environment has become of considerable interest to scientists and computer geeks. This site gives some hints for the uninitiated.

Contents


Terminal and Keyboard

The terminal application dwells in /Applications/Utilities (in German: /Programme/Dienstprogramme). To enable 8-bit characters (e.g. German Umlauts) and the Delete key it is necessary to make some settings in ~/.inputrc, which is the configuration file for the readline library.

On the German Apple keyboard a number of characters are missing. These characters require the following key combinations:

Character Key Combination
~ Alt-N + Space
@ Alt-L
| Alt-7
\ Shift-Alt-7
[ Alt-5
] Alt-6
{ Alt-8
} Alt-9

The Apple documentation calls the ALT key "option key" and the Apple key (aka as "cauliflower key") "command key". On iBooks there is an additional "fn" key which is necessary to modify the arrow keys and the backspace key (FN + Backspace = Delete).

Alternatives to Terminal.app are xterm or iTerm. xterm is an X11 application and thus reads its configuration from "Xresources" stored in ~/.Xdefaults. The advantage of xterm is that it allows to customize the word separating chars with the xterm*charClass Xresource. This makes it possible to select paths and filenames with a double click. In Terminal.app this does not work because word boundary characters are hard wired into Cocoa (the OSX GUI programming library).

The keybindings in Cocoa however can be customized as described by Llew Mason. This in particular useful as the Cocoa default bindings of the HOME and END key are different from every other OS or GUI environment (I have used a lot and none maps HOME to "beginning of document" except Cocoa).


Directories and the Finder

Directories

In some cases the display names of directories in the Finder are not the actual names. This depends on the existence/contents of the file .localized in the directory. In particular the following system folders are renamed:

German Finder name Real Name
/Programme /Applications
Schreibtisch ~/Desktop
/Benutzer /Users
/Programme/Dienstprogramme /Applications/Utilities

A number of directories/files are invisible in the Finder. Nevertheless they can be opened in the Finder from the command line with open .... You can change the visibility of directories/files with SetFile (Developer-Tools).

Beside the MacOS X specific "invisibility flag", the Finder also hides files with a beginning dot (these are "hidden files" in the Unix world). There is no way to make these files visible in the Finder which means that they are only accessible from the command line.

Finder Configuration

Unfortunately there is no built in way to configure Finder context menus. There is however the free third party software OnMyCommandCM which allows to add custom menu items like "Open bash here"; see http://free.abracode.com/cmworkshop/.

Additionally you need OMCEdit to configure the context menus in an easy way. To start for instance an xterm in the current directory you can add the following command in OMCedit:

osascript -e 'tell application "X11" to activate'; cd __OBJ_PATH__; DISPLAY=:0; export DISPLAY; /usr/X11R6/bin/xterm &

The final ampersand (&) is essential because otherwise xterm would block all other applications. Note that this command explicitly sets the DISPLAY variable which fails in a multi user environment. See GUI and X11 for a better solution.

If you want to use Terminal.app instead of xterm, you can invoke it with AppleScript. The problem with Terminal.app however is, that it opens an additional window when it is not already running, so it is necessary to figure out whether Terminal.app is already running. The following command for OMCEdit does the trick:

osascript -e 'tell application "System Events"
  if (count(processes whose name is "Terminal")) is 0 then
    tell application "Terminal"
      activate
      do script "cd " & quoted form of "__OBJ_PATH__" in window 1
    end tell
  else
    tell application "Terminal"
      activate
      do script "cd " & quoted form of "__OBJ_PATH__"
    end tell
  end if
end tell'

The Finder offers no key or menu entry for updating a folder view. The following AppleScript does this:

try
    tell application "Finder"
        set the_Folder to (the target of the front window)
        update every file in the_Folder
        update every file in the folder of the_Folder
    end tell
end try

You can save this script as "Program Bundle" in AppleScript Editor and drag and drop it on the Finder toolbar. In order to prevent the running script from appearing in the dock, add the key LSBackgroundOnly with value "1" in the file Info.plist in the application bundle (for a full list of possible keys see the Apple Developer Documentation). I have prepared a ready to use application bundle as UpdateFinder.zip

Finding Files

Despite its name, the Finder is particularly poor at actually finding files with a simple search. One problem is that it requires to index a folder before it finds anything. Another problem is that it does not allow common searches like looking for file patterns and combining them with certain content (e.g. looking for the occurrence of the variable "bla" in all "*.py" files). For this purpose the freeware Easy Find can be used.

File Type Associations

The information which file types are associated with which applications is stored in the LaunchServices database. You can view its content with

/System/Library/Frameworks/ApplicationServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump

Moreover you can view your user specific associations in ~/Library/Preferences/com.apple.LaunchServices.plist. Applications are identified by the parameter CFBundleIdentifier from the info.plist file in the application bundle.

I have found no other way to change the associations than over the "information" context menu in the Finder (also accessible with Apple-I). When this does not work, there are two things you can try:


How to Start Programs

There are three ways to start an application: There are subtle differences between the start from a shell or the Finder, in particular the environment is different in both situations (you can verify this with my little program listenv): X11 programs can only be run from the command line, preferably from an xterm because otherwise the DISPLAY variable needs to be set. With a little effort it is however possible to start X11 applications from the Finder or the Dock:


Software Development

Scripting Languages

MacOS X 10.3 ships with Shells, Perl and Python, MacOS X 10.4 also includes Tcl/Tk. For shell scripting, CocoaDialog is a simple way to add a GUI.

The interactive Python interpreter shipped with MacOS X does not support command line editing by default. You can enable it on OSX 10.3 with

  sudo python `python -c "import pimp; print pimp.__file__"` -i readline

On OSX 10.4 this does not work. Here is however a standalone python readline module made by Bill Bumgarner that you can install with distutils (see the included README.txt for details) on top of the python shipped with OSX 10.4.

Compiler

The developer tools ("Xcode Tools") contain different gcc versions: gcc2, gcc3, gcc3.3 etc. To set the default version e.g. to 3.3, use sudo gcc_select 3.3. According to the documentation, older versions are only necessary for downwards compatibility to earlier MacOS X versions. If that is not an issue, the latest version is recommended. gcc -v prints the current default version.

Moreover the Xcode tools contain an IDE for the development of Aqua programs and some other useful tools like an icon editor or FileMerge (look at /Developer/Applications/Utilities), a graphical diff utility which supports drag&drop of files from the Finder. Note that FileMerge can also be invoked from the commandline with opendiff.

Programming Interfaces

MacOS X ships with the following APIs (Application Programming Interfaces): The ideal API for MacOS X is Cocoa, which is however not available for any other platform. Possible alternatives are:

Applications

gcc created programs can only be started from the shell. For command line programs this is ok, but GUI applications should be launchable from the Finder/Dock. There are two possible ways to achieve this:

These Resource-Forks have the ugly side effect that they get lost when you pack an archive with zip or tar. Thus you must build your archive in a different way:

Shared and Dynamically Loaded Libraries

Unlike Linux, MacOS X distinguishes between shared libraries and dynamically loaded libraries (called Bundles in Apple lingo).

Shared libraries are loaded by the system at program start; on Linux they have the extension *.so and on MacOS X the extension *.dylib. To check the required shared libraries for a program use ldd on Linux and otool -L on MacOS X.

Dynamically loaded libraries or loadable modules are loaded by the program with special library functions (dlopen on Linux, LoadLibrary on Win32). On Linux they have the extension *.so, on MacOS X *.bundle. The use of loadable modules with dlopen is described in the Program-Library-HOWTO. Thanks to the dlcompat library these instructions also work on MacOS X.


Packages and Fink

Native Packages

MacOS X has two on board means for software installation: Unfortunately MacOS X does not ship with an uninstaller for Installer Packages. It is however possible to uninstall packages by hand, because some information on installed packages is kept in /Library/Receipts. Most notably there is a *.bom file ("bill of material") whose contents can be listed with lsbom (see man lsbom for details). Thus I have written a shell script bomremove that removes all files listed by lsbom; bomremove -? gives a usage message.

Packages can be built with PackageMaker (in /Developer/Applications/Utilities), but this is a nontrivial process. Easier is the use of ESP Package Manage, which has the nice side effect that it also supports the creation of RPM, DEB etc. packages without any additional effort. Make sure that you call epm with sudo (e.g. sudo epm -v -f osx ...) to avoid wrong file ownerships which can cause trouble on the target system!

The lack of a decent built in package management system is one of the most serious drawbacks of MacOS X. It has resulted in a plethora of third party solutions for this problem (Fink, DarwinPorts, iInstaller, OSXPM, ...), which are incompatible with each other.

Fink

Fink is a free software repository with an own package management system. To avoid conflicts with the rest of system, all Fink files are installed in /sw. Before you can use any Fink software, you must source /sw/bin/init.sh (e.g. in ~/.profile).

Fink uses the Debian package management system, which means that you have to use dpkg and apt, e.g. sudo apt-get install .... The downloaded deb-packages are stored in /sw/var/cache/apt/.

Beside apt/dpkg it is also possible to install from the sources with sudo fink install .... The source packages are stored in /sw/src, the compiled deb-packages in /sw/fink/debs. Make sure that the latest gcc3.3 shipped with Xcode is the default compiler before calling fink install. Some packages (e.g. Emacs on Panther) only work when build from the sources.

Some packages are placeholder packages for software that has been installed on a different way beside Fink (e.g. Apple's X-server instead of Finks). These packages are necessary to formally fulfil Fink's internal dependency checks. Some examples:

Dummy-Package Description possible external Source
system-xfree Xserver ships with MacOS X 10.3
system-java.. Java ships with MacOS X 10.3
system-perl Perl ships with MacOS X 10.3
system-tetex LaTeX //www.rna.nl/ii.html


GUI and X11

The MacOS X user interface is called Aqua. X11-applications like xfig, gimp or OpenOffice require additionally a running X-server.

X11

X11 is shipped with MacOS X 10.4 ("Tiger"), but is not installed by default. It is somewhat hidden on the install DVD: look for packages starting with "X11".

If you use X11 applications regularly you should add X11 to your startup items (in "Systemeinstellungen/Benutzer"). The automatically started X11 applications are configured in ~/.xinitrc (see the default configuration /etc/X11/xinit/xinitrc for an example); useful is e.g. the addition of xset b off to suppress the beeping. For security reasons it is a good idea to disable network access in the X11 preferences dialog.

Starting X11 applications on MacOS X prior to 10.5 can be a little tricky because they need the environment variable DISPLAY. When only one X-server is running, it typically has DISPLAY=:0.0. In a multi user environment however, this is no longer true and using a constantly preset value for DISPLAY will fail. There are two possible workarounds for this problem:

X11 applications happily coexist with Aqua applications on the same screen. If you need a fullscreen X11 (eg. for presentations with OpenOffice, xpdf or MagicPoint), switch it in the X11 preferences. Note that you will only get beck to your Aqua desktop with Apple-ALT-A.

A common problem with X11 is that client programs can't connect to the display server because they use bogus authentication data. In that case you will get the error

  Xlib: connection to ":0.0" refused by server
  Xlib: Client is not authorized to connect to Server

Solution:
  rm $HOME/.Xauthority; touch $HOME/.Xauthority

Some GUI Tricks

Apple-TAB switches between applications. If an application has more than one window open (e.g. X11), you will need to press instead F9 or F10 and move to the wanted window with the arrow keys, or you can switch between application windows directly with Apple-Arrow.

Screenshots are possible with Apple-Shift-3 (entire screen) or Apple-Shift-4 (region). This saves a PDF file on the desktop. Another option is /Applications/Utilites/Grab.app (saves as tiff).


How to burn CD-Roms

The Finder offers a simple way to create data CD-Roms: This method is easy to use, but does not offer any options like multi session CDs.

A more flexible method is the use of mkisofs and cdrecord, which are part of Fink. I have written a small script write2cd to simplify the use of mkisofs/cdrecord. When using this script it is important that the CD is not mounted:

For more details see http://homepage.mac.com/machiavel/Text/cdrecord.html


Some useful little Tools

The in the Linux world well known "virtual desktops" are also possible under MacOS X with Desktop Manager on OSX 10.3 or VirtueDesktops on OSX 10.4.

A nice graphical man page viewer is ManOpen.

Occasionally it is necessary to convert a file from Windows to Unix line breaks and vice versa. With the aid of flip, Platypus and CocoaDialog I have built a small application on which you can drop files and which asks you how to convert these files. The application is downloadable for free as ConvertNewlines (universal binary). To learn more about how it works, have a look at the file Contents/Resources/script within the application bundle.


Configuration Issues

In the OSX default configuration the access to samba servers is painfully slow. This issue can be solved by creating a file /etc/sysctl.conf with the contents

  net.inet.tcp.delayed_ack=0

This will have effect after a reboot. You can test this setting for the current session with the command sudo sysctl -w net.inet.tcp.delayed_ack=0.


Back to Homepage Christoph Dalitz, 2009-07-07