Category: Mac

Basic Apple Script

On your desktop, create a folder called test. Inside the test folder, create a folder called tester. Now open a finder window and navigate to a random location, like home, but not one of these folders.

Now open the AppleScript Editor and paste the code below into the AppleScript Editor input window (the top pane).

[text collapse=”False”]
tell application "Finder" to set the target of the front Finder window to folder "tester" of folder "test" of desktop

The finder will automatically open the tester folder in the foremost open window.

A complete tutorial here:

Newline / convert newline shell script

When pasting data to a text file, for use with python and other data editing tools, it may be better to use a code text editor, like BB Edit or Text Wrangler. These apps can save your .txt files with UNIX line returns which is necessary to make some scripts work properly.

Technically the line breaks are invisible, but using the ‘tr’ command, in a UNIX shell, you could display them as any character you like, eg:

In the terminal type:
cat /path/some/file.txt | tr ‘\n’ ‘U’

In the above example, if the input file uses UNIX style line returns, \n ,each line will have a ‘U’ at the end, or nothing if the file is using another type of return.

To change line returns from Mac to Unix in a data file, try the shell script below.



newFile=`basename -s ".txt" $theFile`.UnixLineReturns.txt

theDir=`dirname $theFile`

echo "theFile: $theFile"
echo "newFile: $newFile"
echo "theDir: $theDir"

cat "$theFile" | tr ‘\r’ ‘\n’ > "$theDir/$newFile"

Apple Mac Pro Bluetooth issue solved

If you are using a Mac Pro with wireless Bluetooth devices, particularly a mouse, you may have noticed an annoying lag or stutter in the performance. I bought the Magic Mouse and a wireless keyboard to solve some ergonomic issues. Unfortunately, poor performance made the situation even worse. Now, after having fixed this problem, my wireless devices are working perfectly.

The problem seems to be poor reception from the Bluetooth unit that shipped with my Mac Pro. I solved this by installing a third party Bluetooth usb adapter. In order for the new usb Bluetooth to operate properly, I had to disable the original internal Bluetooth unit. The instructions to disable the Mac Pro’s Bluetooth are below:

Review the procedure for working on your Mac by downloading a manual here.

1. Turn off Mac Pro. Remove power cord. Remove case cover.
2. Remove the processor tray.


Mac Pro Processor Tray

3. Find the Bluetooth unit on the motherboard. It is located near the bottom, on the right hand side as you are looking into the case.


Mac Pro bluetooth unit

4. Remove the connector that connects the Bluetooth to the motherboard.


Mac Pro bluetooth connector

5. Replace processor tray, and close case.


bluetooth USB dongle

6. Install a usb Bluetooth adapter.

Above is the tiny IOGear USB 2.1 Bluetooth Micro Adapter GBU421, which seems to work well with my Mac.

Note: I am using an Intel based MacPro4,1, bought in 2009. If you are using an older Mac Pro, you may want to look at this article and this article, as I believe that the configuration is different.

Caveat: While I am fairly certain that the Mac Pro’s internal Bluetooth could use and antenna redesign, given that so many people are having this same issue, I was not able to completely resolve my issue until I moved the usb Bluetooth adapter up onto my Cinema display, which features usb ports, and is a bit closer to the wireless devices. When I placed the adapter on one of the Mac Pro’s USB ports, on the front of the tower, again the result was poor performance. This leads me to believe that there is some interference from my furniture as well.

Note: If you are using a Magic Mouse, Magic Prefs is a must. It greatly expands the functionality of the Magic Mouse.


Launch Maya through Terminal

If you are using a Macintosh, you can launch Maya by opening a terminal window, typing Maya and hitting the enter key. This will launch one instance of the Maya application, which will close if you quit the terminal window, by the way. This is also useful if you want to open multiple instances of Maya. Just open another terminal window, and repeat the procedure above.

Rsync backup

The scripts, below, allow you to sync directories or drives easily. Great for backups.

Save the scripts to text files with the .sh extension. Make them executable (chmod +x).

#!/bin/sh if [ $# -ne 2 ]; then echo 1>&2 "USAGE: /source/dir /backup/dir/" exit 127 fi rsync --delete -av "$1" "$2"

Usage, in terminal window: path/to/ /source/dir/ /backup/dir/. It is recommended to set up, second script below, to execute and supply the arguments for source directory and target backup directory.
Customize the paths to the script, your source directory and the target backup directory.

~/scripts/ /Volumes/sourceDir/ /Volumes/backupDir

Execute the script in a terminal window (drag and drop and hit enter, or enter the path to the script and hit enter), or set up a cron job to execute this script on a schedule.

Important note!
Seems that sometimes we need to run rsync as superuser, to allow the “–delete” flag to do it’s work deleting or moving files that you have deleted or moved on your source directory. So you can run as superuser by typing:

sudo ~/scripts/ /Volumes/sourceDir/ /Volumes/backupDir

I recommend testing these scripts thoroughly, with temporary directories and files, before using them on valuable data!

vi text editor introduction

vi, text editing program for the Unix/Mac terminal.

To open vi, open a file, or create a file type: vi file_name in a terminal window. Or more specifically vi path/to/filename.ext. For example: vi ~/Desktop/ will open, or create, a file on the Desktop called

To start adding text you need to be in insert mode! hit the i key to get into insert mode.

To delete text, in insert mode, press x for single characters. Press dw for entire words. Press dd to delete entire lines.

To save a file, first hit the ESC key, to enter “command mode”, then type: :w, then hit ENTER (colon+w ENTER)

To exit vi , first hit the ESC key, then type : :q, then hit ENTER (colon+q ENTER)

You can save AND exit by typing :wq, while in command mode.

To move to the beginning of a line, in command mode hit 0 (zero), the end hit $.

Vi cheat sheet

more info here
and here

Shell Scripting Notes – Apple Primer

Shell scripting primer from Apple:



You can use regular expressions to search for strings in a file or a block of text by using the grep command. For example, to look for the word “bar” in the file foo.txt, you might do this:

grep "bar" foo.txt # or cat foo.txt | grep "bar"

Positional anchors allow you to specify the position within a line of text where an expression is allowed to match. There are two positional anchors that are regularly used: caret (^) and dollar ($). When placed at the beginning or end of an expression, these match the beginning and end of a line of text, respectively.

For example:

# Expression: /^Mary/ grep "^Mary" < poem.txt

This matches the word “Mary”, but only when it appears at the beginning of a line.

Shell scripting notes

A shell script is a script written for the shell, or command line interface: Terminal. Typical operations performed by shell scripts include file manipulation, program execution, and printing text.

Examples of uses for shell scripts:

  • Editing large text or data files.
  • Automating tasks, such as backup operations.

You can write a shell script to a text file to perform various tasks on your Mac.


#!/bin/sh if [ -f ~/Desktop/ ]; then say Hello! fi

Save this code to a text file named, on your desktop. It is best to use a program like Text Wrangler, or vi. Open the Terminal (in the Applications/Utilities/ folder) and type: chmod +x ~/Desktop/, to make the file executable. To run the script type: ~/Desktop/ in a terminal window.

#!/bin/sh tells the shell to run the program in sh, a type of shell program.
if and fi are the beginning and end of an “if statement”. Allows execution of certain commands if certain arguments are true.
[ -f ~/Desktop/ ] is the argument. It means: if the file exists on the desktop, do the commands that follow the “then” statement.
say Hello! is the command. This should cause your Mac to say “Hello!”.

You will need learn some basic Unix commands to get around in the command line interface. Read a Mac Terminal tutorial (pdf). The basics can be learned quickly. Below are a few commands that you need to know:

, list the contents of the current directory.
pwd, print the path to the current directory to screen.
cd, change directory. Ex: cd ~/ will change directories to your user home directory.
chmod, changes permissions. Vital, but best to do a tutorial on this one.
mkdir, create a new directory.

Using the Terminal in OSX and Linux is similar.

Utilities/commands useful for shell scripting.

awk A programming language that is designed for processing text-based data. awk video
grep Text search utility originally written for Unix. grep video grep video 2
cat Unix program used to concatenate and display files. cat video
sed is a Unix utility that (a) parses text files and (b) implements a programming language which can apply textual transformations to such files. Sed video

Example grep: this lists (ls) the items in the current directory, and recognizes directories in the list by using the -F option ( you will note the / after list items that are directories). The pipe (|) forwards the result to grep, which filters for directories because we used “/” as the argument.

ls -F | grep /

Example grep and awk: df lists the drives and partitions and disk space on the computer. -h makes it more (h)uman readable. Pipe (|) into grep is filtered for one of the disks, named disk0s2. Pipe (|) awk prints only the 5th column ($5). The result is just the disk space used ex: 39%.

df -h | grep disk0s2 | awk '{print $5}'

or just use awk in this format to get the same result

# more efficient df -h | awk '/disk0s2/ {print $5}' # prints it out in human readable formatting. df -h | awk '/disk0s2/ {print $1 " is " $5 " used. "}'

More info from shell scripting primer from Apple:

Advanced Bash-Scripting Guide

An example of a shell script in action, and a detailed explanation of its functionality.

Extracting raw keyframe data from After Effects keyframe data

You can easily “export” keyframe data from After Effects by copying the key frames in the timeline, then pasting them to a text file.

We want to extract raw key frame data, from After Effects formatted key fame data, via shell script, so that it can be used by Maya or other animation apps.

After Effects keyframe data looks like this:

Adobe After Effects 8.0 Keyframe Data Units Per Second 29.97 Source Width 900 Source Height 506 Source Pixel Aspect Ratio 1 Comp Pixel Aspect Ratio 1 Effects Sound Keys #1 Output 1 #22 Frame 1 0.000261479 2 0.00608461 3 0.0153011 4 0.0274689 5 0.0395869 6 0.0493024 7 0.0562797 8 0.0557284

We need something more like this:

0.000261479 0.00608461 0.0153011 0.0274689 0.0395869 0.0493024 0.0562797 0.0557284

Use this shell script to extract the keys:
Read this for a detailed explanation of the script.
Usage (in the terminal): path/to/ path/to/keyFrameData.txt

Use this shell script to batch process several files:
Usage (in the terminal): path/to/ path/to/directory

Also see:

Here is a primer on shell scripting. You can run a shell script on a Mac or in Linux, through the Terminal, or on a PC with a app like Cygwin.

UPDATE! The NEW! The python script, linked here, will do the same, import After Effects Sound Keys keyframes, from .txt files, to Maya, but it will also take care of the formatting, so there is no need to run the shell script on your After Effects keyframes before importing. Look for the NEW! script.

How to tar and compress files from the Terminal

Tar stands for “tape archive”, and it is a standard archiving method used with Unix systems. Instructions below are for archiving (optionally compressed) and unarchiving tar files from a UNIX shell or the Mac Terminal.

tar video on YouTube

Tar Rant

Lets say you want to back up all of your Users
tar -cvf myTarFile.tar Users
that assumes you are at /
so the User folder will be part of the structure
or you can:
cd /Users
tar -cvf myTarFile.tar *
and that would back up all the users too
tar -cvf myTarFile.tar wolf
would just back up a wolf folder
good so far?
the c flag is for “create” (the tar file)
v is to view all of the files being added
(that is optional)
f is for the name of the tar file being created
you can use a path there, so as to put it where you want it.
if you want to compress the tar file on the fly, add the z flag, and name the tar file so you know that it is compressed
such as:
tar -czvf myTarFile.tar.gz wolf
or if you prefer bzip2:
tar -cjvf myTarFile.tar.bz2 wolf
use j for bzip
the order of the flags does sometimes matter, you will notice that we are creating the tar file first (with the c flag) then compressing it with the z or j flag

Next – we have to learn how to extract a tar file
actually – lets just look at whats inside a tar file first
tar -tvf myTarFile.tar
that will print the contents to the screen
tv is easy to remember – think of that you want to see the contents on your ‘tv”
to view a compressed file:
tar -ztvf myTarFile.tar.gz
the z comes first, because it has to uncompress it before it can print the contents on your “tv”
same but for bzip2:
tar -zjtvf myTarFile.tar.bz2
we just use a j instead of the z
note that if you want to search for a specific file you can use grep such as:
tar -ztvf myTarFile.tar.gz | grep -i someFileName
the -i for grep tells it to ignore the case (capital/lower)
to extract the whole archive, it is the same as viewing, except we just add one flag to extract it
tar -zxvf myTarFile.tar.gz
and instead of the t
that will print all of the files being extracted to the screen, if you want it to not print the files, omit the v flag:
tar -xf myTarFile.tar.gz
oops except that is compressed, so we need the z
tar -zxf myTarFile.tar.gz
if you want to extract an specific file, you just have to specify the file (and the path if any)
so to extract a file called porn.m4v from Documents you woud:
tar -zxvf myTarFile.tar.gz Documents/porn.m4v
and that is pretty much all there is to it!

“Tar Rant” by Jeshua Lacock from OpenOSX