Thursday, April 12, 2012

Mac OS X One-Liners

From: epepke@acm.org (Eric Pepke)
Subject: Mac One-Liners January 15, 2003
Date: 15 Jan 2003 10:14:30 -0800

Here is a new version of the Mac One-Liners.  Changes since last version:
Expanded with contributions from more people.  Edited some.
Still looking for more Carbon stuff.  Also, more non-programming (e.g. general
use) One-Liners are sought.
----------

Mac One-Liners, January 10, 2002
Copyright (C) 2002 by Eric Pepke, All Rights Reserved
Permission is granted for non-commercial copying and distribution of this
document in toto, including copyright notice, editor, and all contributors.



Editor: Eric Pepke (epepke@acm.org)
Contributors: Miro Jurisic, Kevin Reid, Peter Ammon, John C. Randolph,
David Stone, Tom Harrington

Introduction
The Macintosh One-Liners is a compact list of lore about using and programming
the Mac.  Each One-Liner consists of 79 or fewer ASCII characters.  The 
purpose of the One-Liners is to encourage and facilitate painless use of
and development for the Mac.  They generally contain either advice or 
statements.  They are kept short so that they are easier to scan with
the eye.  When they do not tell the whole story, they at least give clues
that will help to find more information in other documentation.

I encourage contributions to this list.  If you have experienced a problem
and solved it, consider writing a One-Liner.  I will edit it if needed.
To suggest One-Liners to include here, post to the USENET groups 
comp.sys.mac.misc or comp.sys.mac.programmer.help with a title that
includes "One-Liner."  Your name or nickname will be included in the
document unless you request otherwise.  I prefer that you post rather
than email, because it gives others an opportunity to comment and/or
correct.

The One-Liners

For Windows Users and Programmers
Options are called Preferences on the Mac.
Exit is called Quit on the Mac.
Ctrl-click works the same as the right mouse button.

OS X General
Command+Option+Esc brings up Force Quit.
To change the app that opens a type of file use Get Info/Open With/Change All

OS X Command Line Interface (CLI)
To copy a file with the resource fork use "ditto -rsrcFork" or "ditto -rsrc"
To get and set most application preferences use "defaults"
To edit and inspect HFS flags use "/Developer/Tools/SetFile" and "GetFileInfo"
To build a Project Builder project from the command line use "pbxbuild"
To run AppleScripts from the command line use "osascript"
To open files from the CLI as with a double-click in Finder use "open" 
To see the full path of a file, drag it to a terminal window.
To inspect executables, use "atool"
To map memeory addresses to function names, use "atos"
To copy the contents of a file to the pasteboard use "/usr/bin/pbcopy"
To get the pasteboard, redirect or pipe the output of "/usr/bin/pbpaste"
To capture the screen to a PDF file, use "/usr/sbin/screencapture"
Don't set the root password.  Use "sudo" instead.  "sudo -s" gives a shell.
Get http://sourceforge.net/projects/fink for many free and open-source apps.

Power PC
Converting between integers and floating point accesses memory, so it's slow.
G4 Altivec code can easily be mixed with C; no assembly is required.
Functions typically start with mflr r0 (aka. mfspr r0, lr) and end with blr.

Programming General
To get the name of the default resource fork, use FSGetResourceForkName.
To find memory leaks try MallocDebug and ObjectAlloc.
For OpenGL debugging use OpenGL Profiler (10.2 and later)
The Project Builder suite produces executables for OS X only.
The low-end version of CodeWarrior (Development Studio) also is for OS X only.
Don't assume the initial working directory is the parent of your application.
The default resource fork is named "RESOURCE_FORK" with Unicode characters.
What Apple calls Unicode is really UTF-16.

Programming Cocoa
To put an image in a table column use [column setDataCell:[NSImageCell alloc]].
To draw sharp single-pixel NSBezierCurves add 0.5 to x and y.
To get a Finder icon use [[NSWorkspace sharedWorkspace] iconForFile:path].
To put something at the right of the menu bar use an NSStatusBar.
To scroll programmatically use [[aScrollView contentView] scrollToPoint:p].
[outlineView reloadItem:nil] doesn't work for root; use reloadData instead.
To use Carbon file functions make a file:// URL and use CFURLGetFSRef.
To make a text field scroll, not wrap Use [[textField cell] setScrollable:YES].
To shrink most controls use [[control cell] setControlSize:NSMallControlSize].
NSScroller, NSTabView, and NSProgressIndicator have their own setControlSize.
To do many UNIX-like file features like chdir, chmod, etc. use NSFileManager.
A file ending in ".mm" can contain Objective C and C++ code.
You can use "self" on the left side of an assignment.
Always use the return value of an initializer; it may be a new object or nil.
The GUI API isn't all thread-safe; only the main thread should use it.
An NSQuickDrawView may be much faster than using standard views.
Apple suggests the URL file:// form be preferred over file names.
NSBundle's localizedStringForKey returns the key if the value is not found.
An OpenGL view does not scroll like a normal NSView in an NSScrollView.
An object is responsible for release or autorelease of an object it creates.
If you can't find a feature in a control, look in its cell.
String attributes of nil uses 12 point Helvetica.
Cocoa programmers need to know Core Foundation, even if its docs say "Carbon."

Programming Carbon
To find where your app is use GetProcessBundleLocation(GetCurrentProcess())
To get the user's home directory use FindFolder(kCurrentUserFolderType)

No comments:

Post a Comment