Category Archives: OSX

Let the Mac Speak for Me

The Problem

I often lose my voice temporarily due to allergy. During that time, my main mode of communication is a notepad, or the electronic equivalent: Zen Brush. I was looking for a solution that can convert what I type into spoken words.

The Solution

Since I am using Mac both at home and at work, and Mac has the say command which is useful for this purpose; I decided to roll my own solution. It turned out that the solution is a very simple bash script, which I named speak4me.sh, which I saved in my ~/bin directory:

#!/bin/bash
while read line
do
    say -v Alex $line
done

To make the script executable, I issued the following command:

$ chmod +x ~/bin/speak4me.sh

Using speak4me.sh

To use speak4me.sh, from the terminal, issue the following command:

$ speak4me.sh

After that, start typing your message. As soon as you hit the return key, the script will “say” what you type. You can keep on typing and hitting return. To end the script, just type Ctrl+C.

Discussion

The script is very simple, pragmatic and free from bells and whistles, but it works the way I like it. The -v Alex part of the say command specifies a voice from Alex. OS X comes with a few voices which you can experiment with yourself. To list the voices your system has, issue the following command:

$ say -v ?
Agnes               en_US    # Isn't it nice to have a computer that will talk to you?
Albert              en_US    #  I have a frog in my throat. No, I mean a real frog!
Alex                en_US    # Most people recognize me by my voice.
Bad News            en_US    # The light you see at the end of the tunnel is the headlamp of a fast approaching train.
Bahh                en_US    # Do not pull the wool over my eyes.
Bells               en_US    # Time flies when you are having fun.
Boing               en_US    # Spring has sprung, fall has fell, winter's here and it's colder than usual.
Bruce               en_US    # I sure like being inside this fancy computer
Bubbles             en_US    # Pull the plug! I'm drowning!
Cellos              en_US    # Doo da doo da dum dee dee doodly doo dum dum dum doo da doo da doo da doo da doo da doo da doo
Deranged            en_US    # I need to go on a really long vacation.
Fred                en_US    # I sure like being inside this fancy computer
Good News           en_US    # Congratulations you just won the sweepstakes and you don't have to pay income tax again.
Hysterical          en_US    # Please stop tickling me!
Junior              en_US    # My favorite food is pizza.
Kathy               en_US    # Isn't it nice to have a computer that will talk to you?
Pipe Organ          en_US    # We must rejoice in this morbid voice.
Princess            en_US    # When I grow up I'm going to be a scientist.
Ralph               en_US    # The sum of the squares of the legs of a right triangle is equal to the square of the hypotenuse.
Trinoids            en_US    # We cannot communicate with these carbon units.
Vicki               en_US    # Isn't it nice to have a computer that will talk to you?
Victoria            en_US    # Isn't it nice to have a computer that will talk to you?
Whisper             en_US    # Pssssst, hey you, Yeah you, Who do ya think I'm talking to, the mouse?
Zarvox              en_US    # That looks like a peaceful planet.

Easy Way to Create Colorful Bash Prompt

The Problem

I often want to fiddle with the bash prompt, but don’t want to deal with bash prompt escape sequences. I wish for a utility which simplify setting a bash prompt. I finally wrote that utility myself: mkprompt

Install

Copy mkprompt to a directory in the path.

Using mkprompt

The best way to show mkprompt usage is via a couple of examples.

PS1=$(mkprompt "red workdir" space dollar)  
PS1=$(mkprompt "cyan Workdir" space "green dollar")  
mkprompt # display help

For more information, see my shell_tools page.

What’s Next?

The following are improvements which I plan for mkprompt, depends on my free time:

  • Implement the rest of the prompt escape sequences
  • Improve the help output
  • Implement installation script

The Script

I current host my script as part of my shell_tools collection on GitHub.

Backup My MacBook Pro

My internal hard drive failed recently, but because of my backup plan, I did not loose any work. Because it works well enough, I would like to share my backup plan with you. Please let me (haiv) know if you have any additional questions.

The Plan

mac_backup.png

I back up my MacBook Pro’s internal drive using two methods: clone and time machine backup. My Mac’s drive is 320GB in size, so I purchased a drive that is more than twice its size, 750GB or 1TB will do, but not 640GB. I am going to partition the drive into two separate drives: the first partition is of the same size with my Mac, and the second utilizes the rest of the available space.

Get An External Drive

I choose a 1TB Western Digital drive because it offers plenty of room and its reliability. When choosing a drive, keep in mind that the size must be larger than twice the size of your Mac’s hard drive. Remember that we are doing two kinds of backup: clone back up requires the backup drive to be the same as your original, while Time Machine requires the backup drive to be larger than your original drive. Thus, if your Mac comes with a 320GB drive, you will need something bigger than 640GB.

Partition My Drive

media_1321399243694.png

With the drive insert, I fire up Disk Utility, select my drive (1), choose the partition tab (2) and make two "Mac OS Extended (Journaled)" partitions. The first, called "mac-clone" (3) is the same size of my Mac’s drive, the second, called "mac-timemachine" (4), uses the rest of the space.

Download and install Carbon Copy Cloner

Install Carbon Copy Cloner from http://www.bombich.com

Setup Clone Backup

media_1321399747089.png

Run Carbon Copy Cloner for the first time, select the source (1) and destination (2), then optionally customize your backup (3), then click on the "Schedule this task" button to go to the next step.

Setup Clone Backup Schedule

media_1321399958570.png

Give your backup task a descriptive name (1), click on the schedule tab (2), then customize the backup schedule (3), then click Save (4)

Setup Time Machine Backup

media_1321400307737.png

Launch Time Machine from the Applications folder, then click "Select Disk" (1) to choose your backup drive. Turn on menu bar status by checking the "Show…" box (2). That’s all.

When Your Drive Failed

Replace your drive, then turn on your Mac while holding the "option" key (the alt key). Choose to boot from your clone drive (mac-clone). Then clone from mac-clone back to your internal drive. It’s that simple.

Related Videos

I have created a couple of short 2-minute tutorial videos outline various steps. Please watch, rate, and comment them. Thank you.

    [watch] Partition an External Drive for Backup
    [watch] Time Machine – How To Set Up
    [watch] Time Machine – Restore a File
    [watch] Clone a Drive Using Carbon Copy Cloner
    

 

Squeeze Multiple Blank Lines within vim

The Problem

I want to squeeze multiple blank lines into just one while editing in vim. I often receive source code which the author used multiple blank lines to separate functions. I don’t mind if functions or code block are one or two lines apart, but I have seen sources where functions are four or more blank lines from each other. This is annoying and I want to squeeze many of them into one.

The Solutions

The obvious solution is to use vim’s search-and-replace feature, as discussed in this post. However, my regular expression skill is very basic and I tend to forget the syntax when I need it. Hence, I devised my own solution, one which I have better luck memorizing.

My solution involes the cat command:

:%!cat -s

This short little command applies ‘cat -s’ on my entire file contents, which squeezes multiple blank lines down to one. Note that this solution will only work on Unix-like systems such as BSD, Linux, and Mac OS X. It does not work on Windows. This is one of the shortcoming of this method compare to the previous one.

Upgrade My Mac’s Hard Drive

My 4.5 year-old MacBook is getting slower every day due to its age against the new OS and applications. To give it a new lease in life, I replaced its 500GB hard drive with a 120GB SSD from OWC. This post discusses the upgrade process.

Before the Upgrade

Unwind Applications

This step disconnects the applications from their services. For example, I de-authorized my iTunes account after one last sync with my iPhone. I also disconnected from Dropbox, quit Mail. I quitted all applications which connects to the internet to ensure their data not to change during the back up. I also took note of the Dropbox’s user name and password, along with the password to my 1Password data file. These information are crucial for bringing my system up after the upgrade.

Prepare Applications List

This step is crucial. I made a list of applications I installed on my MacBook. I also hunt down all the license keys to allow me to reinstall later. Since I store my license keys in 1Password, this step is relatively simple. I then prioritize my applications list: priority means I must have them. 1Password and Backblaze back up falls into this category. Priority 2 means I should have them for my every day tasks: Aperture, Path Finder, and Alfred are examples in this category. Applications that do not fall into these two categories will be installed only when absolutely needed.

Back up Data

Next, I disconnected my internet connection to ensure no data change during the back up. I then created a bootable clone of my hard drive using SuperDuper. This is the last snapshot of my old hard drive. This step also means that after upgrade, I will have two copies of my old hard drive. I will keep one of those copies in case I ever need any old data from it.

Upgrade and Post Upgrade

I will not discuss the actual replacement of the drives as there are several how-to videos showing this procedure–just search for them.

Install the Operating System and Updates

After installing the new drive, I installed the operating system from the disc. Then connect to the internet and run software update. This step might spans several minutes.

Restore Dropbox and 1Password

Since I stored my 1Password’s data in Dropbox, I connected to the internet, downloaded Dropbox, installed it, copied the Dropbox data from the old drive to the new, and brought Dropbox online. Next, I downloaded and installed 1Password. Amazingly, the first time I launched 1Password, the application found its data from the Dropbox folder and asked me if I want to use it. After saying yes, I got all my 1Password back as if the upgrade never occurred. I have the 1Password team to thank for that.

Restore Other Applications and Data

Most applications store their data in ~/Library/Application Support, so after re-installing the application and before the first launch, I copied the data from the old drive over to the new drive at the same location and applications will pick up where they left off. Many people suggested to use Apple’s application migrations, but I don’t want to use it because I need the absolute control of which application to move. Besides, I am more comfortable with this level of details. If you are not familiar with the file structure of Mac OS X, you should use application migration.

Lessons Learned

  • Backup, backup, backup. My drive were backed up by three means: The Backblaze online service, SuperDuper and Time Machine. Over the years, I had lost many valuable family photos and videos due to the lack of backing up. I made that mistake twice (yes, its dumb.) With the new SSD, I keep all three back up methods. The Backblaze backup is especially useful because it is offsite.
  • Preparation ensures smooth transition. By taking notes of the essential services’s user names and passwords, I was able to pick up quickly. By learning the data location of my applications, I avoided headaches of not having my data migrated.
  • Only installed application when absolutely necessary. This time around, I not install applications just because it is cool. This will save me from bloating my hard drive and operating system, slowing down my already-aging MacBook.
  • 1Password is a life-saver, especially for storing my applications’ license. Some applications, such as Hazel, store their keys in a file, which 1Password can also handle via the attachment feature. Go 1Password!

Open Windows Share

As a Mac user in a Windows world, I can get along just fine. I can share files with my colleagues, collaborate documents, or communicate. However, one of the annoyances I face has to do with how Windows and Mac references SMB shares. Windows uses the \\server\share notation while Mac uses smb://server/share. I often receive emails such as one below:

Hello Hai,
Please review the latest document is at \\community\docs\readme.txt and let me know what you think.
– Da boss

I wish I can just highlight the share and open it in the Finder, but it is in a different language. Parlez-vous Anglais? After putting up with this problem for too long, I finally had enough and decided to do something about it. My solution involes a little automation, add to it some sed shell script and mix well. Here is how.

Create a New Service

media_1276487315003.png

Start Automator, select Service and click Choose.

Add a Run Shell Script Action

media_1276487436039.png

Make sure that the top said, "Service receives selected text in any application" (see picture above). From the Library column on the left, select "Utilities", then select "Run Shell Script" in the next column. Next, type the following script:

open $( echo "$1" | sed ‘s:\\:/:g;s/^/smb:/’ )

Save this automation action as "Open Windows Share" or any name you choose.

Try It Out

media_1276487890700.png

Select the text containing the Windows-notation, then select the application menu (the name of the application at the top left corner), Services, Open Windows Share. The document you selected should be open.

Discussion

media_1276488775456.png

This automator action employs the sed command to transform the selected text based on the following two rules: a) replace all back slashes "\" with forward ones "/", and b) slap a "smb:" in front. The effect of this transformation turns a Windows-notation to Mac (or Unix, for that matter). The automation action resides at ~/Library/Services directory.

Add Color Coding to Mac OS X’s Quick Look for Source Codes

The Problem

The recent releases of Mac OS X has a wonderful feature: quick look. By selecting a file in the Finder and hit spacebar, the user can quickly view the contents of that file, be it text, audio, video, graphics, and many other file types. I often want to quick look the contents of my source codes and encountered two problems: First, As shipped, quick look does not support many types of source code, specifically Tcl. Second, the default quickly is pretty bland without any color code at all.

The Solution

My search took me to a wonderful piece of software called qlcolorcode and it did what I want. Now instead of seeing the above boring quick look, I see something like:

If you like what you see and decide to use it, head over the developer’s site and grab it. The installation is easy, so just jump right in. When you are there, please read the ReadMe for customization such as adding line numbers or changing the font.