Some unix hacker tips for Mac OS X

I'm a long-time unix hacker and I'm using Mac OS X.

In 1984, a friend of mine commented that Macintosh was "user-friendly but hacker-hostile". I don't think it's hacker-hostile these days, but some digging is sometimes required to make it do what you want. But almost all the Apple stuff has good command-line utilities, with man pages.

Here are some things I do to make Mac OS X more flaps-friendly.

Mac OS X uses sudo extensively and doesn't normally have a root password at all. (You may be familiar with this scheme from linux these days, because linux copied this from Mac OS X (so it seems to me).)
If you want a real root password you want to do "sudo passwd root". (This is a lot easier than all the complicated recipes out there on the net.)
In general, you want to modify /etc/passwd-like information using command-line tools such as chsh, because /etc/passwd isn't used, there's some gnarly database thing instead. "chpass" can do everything.

# nvram boot-args=-v
makes it show the boot process in a normal, non-user-friendly way. (I've done this since Mac OS 10.3.)

Turn off bluetooth unless you need it, and "disable remote control infrared receiver" in the Security pane in System Preferences (as of 10.8 it's under "advanced" under the "general" tab in this pane).
I think that ntp is on by default these days.
You likely want "require password after sleep or screen saver begins".

There are a few things you want to tweak in Preferences in Finder, most notably turning on "show all filename extensions" (in "advanced"). Although I really use the Finder extremely rarely these days, since I can do everything more quickly from the command-line.

Pre 10.7 (or maybe 10.8 -- I haven't ever had a 10.7 machine actually), you want to do "mdutil -a -i off". RTFM. But as of 10.7 or 10.8, this breaks "app store" updates (!), which you need (!), so don't do it any more.
It's not actually all bad -- "mdfind" is like GNU locate on steroids; occasionally very useful. RTFM, and note the "-name" option (as well as the possible lack thereof).

I'm not sure at which OS revision this started working, but anyway in 10.8,
# chmod 600 /System/Library/CoreServices/Search.bundle/Contents/MacOS/Search
removes "spotlight" from the menu bar (for future sessions). This has nothing to do with whether it wastes CPU and i/o time on indexing the disk all the time, which is about "mdutil -i off" (but see previous paragraph).
(To make this take effect right away, do "killall SystemUIServer", which will restart the software which maintains that part of the menu bar. Otherwise it will take effect on next login.)

Removing the "notification manager" from the menu bar is trickier. You need to execute the command "launchctl remove com.apple.notificationcenterui.agent" after logging in, every time. To arrange this, use "LaunchAgents", described in a subsequent section.

Most other icons can be simply removed with command-drag.

open, and AppleScript

You want to know the command "open". This can do all sorts of useful things from aliases or shell scripts (or just the command line). Examples:
open /Applications/Utilities/Disk\ Utility.app
Run the Disk Utility application program.
open -a /Applications/TextEdit.app file.rtf
Run TextEdit on the given file.
open file.ext
Open that file with whatever the standard program to open it would be, just like double-clicking on it.
open /System/Library/Frameworks/ScreenSaver.framework/Versions/Current/Resources/ScreenSaverEngine.app
Start the screen saver, which depending on other settings constitutes locking the console.

I make short shell scripts in my bin directory for all application programs I use, so that I can type (e.g.) "firefox file.html". And a plain "firefox" starts firefox.
/u/ajr/bin/firefox is:

open -a /Applications/Firefox.app "$@"
(This runs just "open -a /Applications/Firefox.app" if there are no command-line arguments, and that command works just as well as without the -a. If there are command-line arguments, the '"$@"' syntax quotes them all properly when passing them to 'open'.)

And you can do all sorts of program-controlling things with "AppleScript". The key command is "osascript", which runs supplied applescript code.

Example: The following shell command puts the computer into sleep mode, so I can do "bigcommand; gosleep" (I call my script "gosleep" because "sleep" is already taken in unix):

osascript -e 'tell application "Finder" to sleep'

And to tell you a fair bit of what you need to know about AppleScript syntax (assuming you know sh syntax), that could equally be rendered as:

osascript -e '
    tell application "Finder"
	sleep
    end tell
'

There's all sorts of example applescript code out there on the web for controlling iTunes. In some cases it might be more convenient to make applescript "applications", e.g. using /Applications/Utilities/AppleScript\ Editor.app. When you do a save, change the type to application, and then in some cases you want to check "stay open". Alternatively, use "osacompile" (e.g. "osacompile -o whatever.app whatever"), and "stay open" is "-s".

File extended attributes:
ls -lOe
Use chflags for inline flags; "chmod -N" to remove ACLs on a separate line at the end.
Or "fsaclctl -p /Volumes/whatever -d" to disable ACLs altogether.

LaunchAgents

LaunchAgents is a facility which makes arbitrary programs (which can be shell scripts if "chmod +x"ed and "#!"ed) run at particular times. Available times include upon login and upon change to a file. I don't know how often the latter is checked but it seems to be pretty immediate, which is a bit worrying, but anyway I use it to run a fix-up script when my networking connection changes (e.g. change DNS settings).

You put new ".plist" files in $HOME/Library/LaunchAgents, and I don't know what you have to do to get it to re-check this directory but logging out and in certainly suffices.

They can be called anything ending with ".plist", so be descriptive, and use a name which makes it clear that it's your personal addition.

And I think that I can tell you the rest of what I know by just giving you two example files, the first of which runs a command upon login, and the second of which runs a command when your networking changes (and DHCP does its thing).

1:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>killnotificationmanager</string>
        <key>Program</key>
                <string>/u/ajr/etc/killnotificationmanager</string>
        <key>RunAtLoad</key>
        <true/>
</dict>
</plist>

2:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>networkchange</string>
        <key>Program</key>
                <string>/u/ajr/etc/networkchange</string>
        <key>WatchPaths</key>
        <array>
                <string>/var/run/resolv.conf</string>
        </array>
</dict>
</plist>

Software

To get a C compiler (gcc), install "Xcode tools". As of 10.8ish, you can install this through the "app store" (for free). In recent versions, you have to "enable command-line tools".

The X server which comes with Mac OS X is fine, and it will read $HOME/.xinitrc if it exists. (If you make an .xinitrc, if you want the standard window manager it's "quartz-wm".)

For whole-disk backup, the Apple "Time Machine" software is ok. For other backups I just use rsync.

For a serial-port terminal I use "ZTerm" (and a USB serial port device).

As of 10.8, "QuickTime Player" seems to suck, but the current version of "MPlayerX" is good.

I use "Seashore" for basic graphics editing (cropping, tweaking individual pixels, etc). GIMP is now Mac-OS-native, and I use that too sometimes, but Seashore is simpler.

This is not MacOS-specific, but the type of person reading this file should definitely know about "wget" if you don't already. Builds fine on Mac OS X.