Mac OS X Hints - Jaguar Edition logo Pogue Press logo

Instructions: The scripts and code on this page can be cut and pasted directly into either the Terminal or the Script Editor, saving you the trouble of typing them out yourself. You'll find a few of these code snippets are quite wide. They were entered this way on purpose, so as to not introduce any extra line breaks. Here's how to read the various bits of code you'll see on this page...

Commands you type directly into the terminal are shown in monospace green on black text. If the line is an actual Unix command, it's shown with a leading $ symbol to indicate the command prompt:

   $ some terminal command/goes here > example.txt
$ some other terminal/command /goes > here.txt

Do not copy the $ sign, nor the space shown immediately next to it; select all the text beginning with the first character after the space. If there is more than one command, each occurrence of the $ sign indicates a new line, and each line should be copied and pasted one at a time. Scripts that are to be entered in a UNIX text editor, such as vi, pico, or emacs, along with commands to set various preferences, are shown in the same monospace green on black text, but lack the leading $ sign:

   #!bin/bash
# some script
first line of script example
next line of script example

In these cases, simply copy and paste the entire block or line into the text editor of your choice (or Terminal, if it's a preferences command). Finally, AppleScripts are shown in gold with a sidebar set-off, and can be copied and pasted directly into Script Editor in one step:

tell application "some_app"
  do something
  set some_var to 21
end tell

Use the following navigation table to jump directly to the specified (chapter number - hint number) hint:

Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
  • No scripts
Chapter 7
Chapter 8
Chapter 9
  • No scripts
Chapter 10
  • No scripts
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16

--------------------------------------

Chapter One

Hint 1-17: Double Arrows on Both Ends

To place a double-scroll arrow at each end of the scrollbar:

defaults write "Apple Global Domain" AppleScrollBarVariant DoubleBoth

[Return to Navigation Table]

--------------------------------------

Chapter Two

2-17 Nixing Zoom Effects

To remove the zoom rectangles when opening applications:

defaults write com.apple.finder ZoomRects -bool no

To restore the zoom rectangle effect:

defaults write com.apple.finder ZoomRects -bool yes

2-19 A Quit Menu in the Finder

Add a Quit menu to the Finder:

defaults write com.apple.finder QuitMenuItem -bool yes

2-20 Showing Hidden Files

To show all hidden files in the Finder:

defaults write com.apple.finder AppleShowAllFiles -bool yes

To hide all hidden files in the Finder:

defaults write com.apple.finder AppleShowAllFiles -bool no

[Return to Navigation Table]

2-53 A Folder Action Script to Organize the Desktop

A script to move image files from your Desktop to a new "Unfiled" folder in your pictures folder. Paste the following in Script Editor, or just click here to let AppleScript do the work for you.

on adding folder items to this_folder after receiving added_items
  tell application "Finder"
    set homeFolder to (container of (path to desktop folder) as string)
    set folderLocation to homeFolder & "Pictures:Unfiled:"
    repeat with aFile in added_items
      if the name extension of aFile is in {"jpg", "gif", "png", "pict", "tiff", "gif", "psd", "bmp"} then
        tell application "Finder"
          try
            move aFile to folder folderLocation
            display dialog ¬
            "Image files have been moved to the Unfiled" & return & "folder in your Pictures folder."
            on error err
              display dialog err
          end try
        end tell
      end if
    end repeat
  end tel
end adding folder items to

[Return to Navigation Table]

2-54 Open the Finder Selection in the Terminal

Copy and paste the following into Script Editor, or just click here to have AppleScript do the work for you:

(*
Open in Terminal
Copyright 2002 Gregory Ramsperger
*)
property badPathChars : {"\\", "`", "!", "$", "&", "(", ")", "{", "}", "|", "'", "\"", ";", "<", ">", "?", " "}
-- drag-and-drop open
on open theList
  set dir to (item 1 of theList) as text
  my oit(dir)
end open
-- direct run
on run
  try
    tell application "Finder"
      set dir to the target of Finder window 1 as text
    end tell
    my oit(dir)
  on error
    my oit(path to desktop)
  end try
end run
-- run a command in the terminal
on oit(d)
  tell application "Finder"
    if d does not end with ":" then
      set d to (container of item d) as text
    end if
  end tell
  set p to (POSIX path of d)
  repeat with c in badPathChars
    set AppleScript's text item delimiters to c
    set tempitemList to every text item of p
    set AppleScript's text item delimiters to ("\\" & c)
    set p to the tempitemList as string
    set AppleScript's text item delimiters to ""
  end repeat
  tell application "Terminal"
    activate
    do script with command ("cd " & p)
  end tell
end oit

[Return to Navigation Table]

2-60 Screen Saver as the Finder Background

To run the currently selected screensaver as the desktop background:

$ /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background

[Return to Navigation Table]

--------------------------------------

Chapter Three

Unless otherwise noted, you must restart the dock in order to see the effects of any of the following changes.

3-6 The Top-Mounted Dock

To put the dock at the top of the screen, open the Terminal and type:

defaults write com.apple.Dock orientation -string top

3-7 Changing the Minimize Action

To use an undocumented "suck in" action for the minimize effect:

defaults write com.apple.Dock mineffect suck

3-8 Pinning the Dock to a Corner

To place the dock at the far right (bottom if vertical) of the screen:

defaults write com.apple.Dock pinning end

To place the dock at the far left (top if vertical) of the screen:

defaults write com.apple.Dock pinning start

To place the dock back in the middle of the screen:

defaults write com.apple.Dock pinning middle

[Return to Navigation Table]

3-9 Differentiating Hidden Programs

To show hidden programs with a transparent icon:

defaults write com.apple.Dock showhidden -bool yes

To remove hidden programs' transparent icons (dock must be restarted to see the change):

defaults write com.apple.Dock showhidden -bool no

3-17 An AppleScript to Restart the Dock

Paste the following lines in the Script Editor application in the /Applications -> AppleScript folder:

tell application "Dock"
  quit
end tell

Save the script as an Application and check the "Never Show Startup Screen" box to make it easy to use (a simple double-click will restart the dock).

3-22 Killing the Dock

To permanently get rid of the dock (takes effect on next login):

$ cd /System/Library/CoreServices
$ sudo mv Dock.app ~/Documents/Dock.app

To restore the dock (takes effect on next login):

$ cd /System/Library/CoreServices
$ sudo mv ~/Documents/Dock.app /System/Library/CoreServicesDock.app

[Return to Navigation Table]

--------------------------------------

Chapter FOUR

4-8 Fun with the Floating Desktop Blob

To enable a strange, floating blue blob that activates Exposé, type this in the Terminal:

defaults write com.apple.dock wvous-floater -bool true

To disable it again, repeat the above with false instead of true.

4-9 Change Exposé's Show Desktop Behavior

To shrink all windows into a tiny desktop icon when you hit F9, type this in the Terminal:

defaults write com.apple.dock wvous-olddesktop -bool false

To return to the normal Exposé mode, repeat the above with true instead of false. Warning: This hack has been known to cause some system stability issues, and the small window created is a "dead zone" for any mouse clicks -- even when the window isn't in use!

4-24 Speed Up the Display of Sheets

This command will change the sheet draw speed to super-fast:

defaults write NSGlobalDomain NSWindowResizeTime 0.001

To revert to normal, type:

defaults delete NSGlobalDomain NSWindowResizeTime

[Return to Navigation Table]

--------------------------------------

Chapter FIVE

5-9 Renaming the System Preference Categories

Step #1:

$ cd "/Applications/System Preferences.app/Contents/Resources/English.lproj"
$ sudo cp NSPrefPaneGroups.strings NSPrefPaneGroups.bak
$ sudo cp NSPrefPaneGroups.strings ~/Desktop/MyPrefs.strings

Step #5:

$ sudo cp ~/Desktop/MyPrefs.strings NSPrefPaneGroups.strings

Step #6 (restores backup if you're having problems):

$ sudo cp NSPrefPaneGroups.bak NSPrefPaneGroups.strings

[Return to Navigation Table]

--------------------------------------

Chapter SEVEN

7-12 Force Messages into Plain Text Mode

Paste the following into Terminal:

defaults write com.apple.mail PreferPlainText -bool true

[Return to Navigation Table]

--------------------------------------

Chapter EIGHT

8-13 Change the Behavior of the Gray Arrow Links

Paste the following into Terminal:

defaults write com.apple.iTunes invertStoreLinks -bool YES

8-16 Save Videos and Trailers After Viewing

Paste the following into Script Editor, or just click here to let AppleScript do the work for you.

set originalFile to do shell script "lsof -wa -Fn -c iTunes +D /tmp | grep -i QTP | head -1 | cut -c 2-"
if length of originalFile is greater than 0 then
  set newFile to POSIX path of (choose file name default name "iTunes Video.mov")
  if length of newFile is greater than 0 then
    if newFile does not end with ".mov" then set newFile to newFile & ".mov"
    do shell script "/bin/cp -v '" & originalFile & "' '" & newFile & "'"
    do shell script "/Developer/Tools/SetFile -t 'MooV' -c 'TVOD' '" & newFile & "' || true"
  end if
else
  display dialog "No movie found. Please make sure that the movie is
currently visible in iTunes." buttons {"OK"} default button "OK"
end if

[Return to Navigation Table]

8-37 Let iChat Tell the World About Your Musical Tastes

Paste the following into Script Editor, or just click here to have AppleScript do the work for you:

on idle
  tell application "iTunes"
    if player state is playing then
      set a to artist of current track
      if a is "" then
        set a to "mysterious unknown music"
      end if
    else
      set a to "nothing"
    end if
  end tell
  tell application "iChat"
    set status message to "listening to " & a
  end tell
  return 20
end idle

[Return to Navigation Table]

--------------------------------------

Chapter ELEVEN

11-7 To-Do Calls in iCal from Address Book

Enter the following in Script Editor, or just click here to have AppleScript do the work for you:

using terms from application "Address Book"
  on action property
    return "phone"
  end action property
  on action title for p with e
    set theName to (first name of p) & " " & (last name of p)
    return "Schedule call to " & theName
  end action title
  on should enable action for p with e
    return true
  end should enable action
  on perform action for p with e
    set theName to (first name of p) & " " & (last name of p)
    scheduleCall((id of p), theName)
  end perform action
end using terms from
on scheduleCall(id, name)
  tell application "iCal"
    -- New to-dos appear in the first calendar in your list
    -- of calendars. To change where they go, change the '1'
    -- in the next line to another number. Just count down from
    -- the top of the calendar list in iCal to find the number.
    set theItem to (make new todo at end of todo of calendar 1)
    set summary of theItem to "Call " & name
    set url of theItem to "addressbook://" & id
    activate
    show theItem
  end tell
end scheduleCall

[Return to Navigation Table]

11-8 Perform Reverse Phone Number Lookups

Enter the following in Script Editor, or just click here to have AppleScript do the work for you:

using terms from application "Address Book"
  on action property
    return "phone"
  end action property
  on action title for aPerson with aPhone
    return "Look Up Address"
  end action title
  on should enable action for aPerson with aPhone
    return true
  end should enable action
  on perform action for aPerson with aPhone
    set thephone to ((value of aPhone) as string)
    set wpq to 1
    if (label of aPhone) = "work" then
      set wpq to 0
    end if
    set AppleScript's text item delimiters to {"(", ")", " ", "-"}
    set qpa to (first word of thephone)
    set qpx to (second word of thephone)
    set qpp to (third word of thephone)
    set AppleScript's text item delimiters to ""
    open location ("http://ypng.infospace.com/home/yellow-pages/redir.htm?" ¬
    &"qfm=p&searchtype=all&fromform=psearch&QK=10&top=1&" ¬
    & "qcat=reverse&qsubcat=revphone&wqp=" & wpq & "&qpa=" ¬
    & qpa & "&qpx=" & qpx & "&qpp=" & qpp & "&x=0&y=0")
  end perform action
end using terms from

[Return to Navigation Table]

11-9 Reveal a Contact's Age in Years

Enter the following in Script Editor, or just click here to let AppleScript do the work for you.

using terms from application "Address Book"
  on action property
    return "birth date"
  end action property
  on action title for p with e
    set monthnow to (month of (current date) as number)
    set daynow to (day of (current date) as number)
    set monthcheck to ((month of e) as number)
    set daycheck to ((day of e) as number)
    if monthnow < monthcheck then
      return (first name of p) & " is " & (((year of (current date)) - (year of e) - 1) as string) & " years old"
    else
      if monthnow = monthcheck then
        if daynow < daycheck then
          return (first name of p) & " " & (((year of (current date)) - (year of e) - 1) as string) & " years old"
        end if
      end if
      return (first name of p) & " " & (((year of (current date)) - (year of e)) as string) & " years old"
    end if
  end action title
  on should enable action for p with e
    return true
  end should enable action
end using terms from

[Return to Navigation Table]

11-10 Check the Weather in a Contact's Town

Enter the following in Script Editor, or just click here to let AppleScript do the work for you.

using terms from application "Address Book"
  on action property
    return "address"
  end action property
  on action title for per with addr
    set ccode to country code of addr
    if ccode is missing value then
      set ccode to default country code
    end if
    if ccode is equal to "us" then
      return (("Weather Underground forecast at " as Unicode text) & name of per as Unicode text) & "'s " & label of addr
    else
      return ("Weather Underground forecast of " as Unicode text) & city of addr
    end if
  end action title
  on should enable action for per with addr
    if city of addr is missing value then
      return false
    end if
    return true
  end should enable action
  on perform action for per with addr
    set ccode to country code of addr
    if ccode is missing value then
      set ccode to default country code
    end if
    if ccode is equal to "us" then
      set loc to "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=" & zip of addr
    else
      set loc to "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=" & city of addr & "," & country of addr
    end if
    open location loc
    return true
  end perform action
end using terms from

[Return to Navigation Table]

11-19 Speed-Mounting Disk Images

Type the following in Terminal:

defaults write com.apple.frameworks.diskimages skip-verify true

If you want to reverse it at some point, replace true with false.

11-20 Stop Special Treatment of "Internet-Enabled" Disk Images

Type the following in Terminal:

defaults write com.apple.frameworks.diskimages skip-idme true

If you want to reverse it at some point, replace true with false.

[Return to Navigation Table]

--------------------------------------

Chapter TWELVE

12-8 Google Searching in your Browser

The content of the bookmark is:

javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('Keywords...',''))};if(Qr)location.href='http://google.com/search?query='+escape(Qr)+'&num=20'

There is no line break or added space -- just copy and paste as one long line.

12-57 Emailing URLs from Safari

The content of the special bookmark is:

javascript:location.href='mailto:?SUBJECT='+document.title+'&BODY='+escape(location.href)

[Return to Navigation Table]

12-58 Add the Missing Print Button

The content of the special bookmark is:

javascript:window.print( )

12-59 Create Custom Clicked-Link Outlines

The content of the text file is:

:link:active, :visited:active { outline: 3px auto; }

12-60 The Debugging Menu

Type the following in the Terminal:

defaults write com.apple.Safari IncludeDebugMenu –bool true

[Return to Navigation Table]

--------------------------------------

Chapter THIRTEEN

The following two hints both relate to DragThing. Enter each script in Script Editor, or click the associated link, and run them as explained in the book.

13-7 Nix the Fades

Enter into Script Editor, or just click here and let AppleScript do the work:

tell application "DragThing"
  set shouldFade to do not use fading
  set do not use fading to (not shouldFade)
end tell

13-8 Indicate Active Programs

Enter into Script Editor, or just click here and let AppleScript do the work:

tell application "DragThing"
  set useBadge to do not use running app badge
  set do not use running app badge to (not useBadge)
end tell

[Return to Navigation Table]

--------------------------------------

Chapter FOURTEEN

14-17 Replacing the Finder

Type the following command in the Terminal to replace the Finder with Path Finder:

defaults write com.apple.loginwindow Finder /Applications/Path\ Finder.app

To return to the standard Finder:

defaults delete com.apple.loginwindow Finder

[Return to Navigation Table]

--------------------------------------

Chapter FIFTEEN

15-24 Open a New Terminal Window in the Current Directory

Create the following script in your favorite Unix text editor:

#!/bin/sh
osascript<<END
tell app "Terminal"
  do script "cd \"`pwd`\""
end tell

15-90 Renaming the Trash Can

Step #1:

$ cd /System/Library/CoreServices/Dock.app
$ cd Contents/Resources/English.lproj
$ sudo cp InfoPlist.strings InfoPlist.bak
$ sudo cp InfoPlist.strings ~/Desktop/InfoPlist.txt

Step #5:

$ sudo cp ~/Desktop/InfoPlist.txt InfoPlist.strings
$ sudo chown root:wheel InfoPlist.strings

[Return to Navigation Table]

--------------------------------------

Chapter SIXTEEN

16-6 Hiding an Application from the Command Line

Create the following script in your favorite Unix text editor:

#! /bin/sh
# hide an application in the Finder
osascript <<END
tell application "Finder"
  if exists application process "$1" then
    set visible of application process "$1" to false
  end if
end tell

16-8 Fast User Switching from the Command Line

Enter the following into Script Editor, or just click here and let AppleScript do the work:

do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend"

16-17 Finding Files in the Current Directory Tree

Finding duplicate files:

$ find . -size 20 \! -type d -exec cksum {} \; | sort | tee /tmp/f.tmp | cut -f 1,2 -d ' ' | uniq -d | grep -hif - /tmp/f.tmp > ~/Desktop/duplicates.txt

[Return to Navigation Table]

16-24 Customizing Your Shell Prompt

A sample bash shell prompt:

$ PS1='\[\033[43;34m\][\h:\w] \u\$ \[\033[0m\]'

16-25 Removing Space from File Names

#! /bin/sh
for n in *
do
  OldName=$n
  NewName=`echo $n | tr -d " "`
  #NewName=`echo $n | tr -s " " "_"`
  echo $NewName
  mv "$OldName" "$NewName"
done

16-37 Port Scans from the Terminal

$ sudo ln -s "/Applications/Utilities/Network Utility.app/Contents/Resources/ stroke" /usr/local/bin/portscan

[Return to Navigation Table]

16-40 Jazzing Up Your Site: Server-Side Includes

This is the script from step 12 in the instructions:

<HTML>
  <HEAD>
    <TITLE>My SSI Test Page</TITLE>
  </HEAD>
  <BODY>
    <b>Today is:</b><br>
    <!--#echo var="DATE_LOCAL"-->
  </BODY>
</HTML>

16-43 A Windows-Sharing Management Tool: SWAT

Step 2: /etc/xinetd.d swat file:

service swat
{
  port = 901
  socket_type = stream
  wait = no
  only_from = localhost
  groups = yes
  user = root
  server = /usr/sbin/swat
  log_on_failure += USERID
  disable = No
}

16-44 Apache’s Server Statistics

Step 4:

$ sudo cp webalizer /usr/local/bin/webalizer
$ sudo chmod 755 /usr/local/bin/webalizer
$ sudo cp webalizer.1 /usr/local/share/man/man1/webalizer.1
$ rehash

[Return to Navigation Table]