Show/Hide Invisible Files on Mac & Other Secrets

Showing hidden files can be somewhat of a pain in Mac OS X. I’ve come up with a number of ways to make it easier for myself.

The simplest, most direct way of doing this is a command in the Terminal. One way is to navigate to the appropriate directory in Terminal and type ls -lha. Alternatively, you can type ls -lha <path/to/directory>. This will give you a list of all the hidden files for that directory. But maybe you aren’t proficient with Terminal commands and/or you need to do some file manipulation and/or you don’t want to use the CLI.

Much like my post Hide Desktop Icons on Mac, I’ve created a little script I call hidden that automatically shows and hides hidden files.


# checks file visibility and stores value in a variable
isVisible="$(defaults read AppleShowAllFiles)"

# toggle file visibility based on variable
if [ "$isVisible" = 1 ]; then
	defaults write AppleShowAllFiles -bool false
	defaults write AppleShowAllFiles -bool true

# force changes by restarting Finder
killall Finder

Paste that code into a text editor, save it to a directory in your $PATH and make it executable (chmod 755 <filename>).

HideSwitch ScreenshotTwo other alternatives I’ve stumbled upon are HideSwitch and Secrets Prefpane (at the time of this post the site was down. Get it at MacUpdate instead.) HideSwitch is just a simple mini-app with two buttons to hide and show hidden files. Secrets Prefpane is a just that: a button that shows up as a system preference and turns into a prefpane once clicked. However Secrets is quite powerful and can do a lot more than just show and hide files. Secrets includes a variety of features, such as:

  • Selecting the format and destination folder of saved screenshots
  • Changing the login window desktop picture
  • Changing Dock effects
  • Seeing the contents of folders when QuickLooking (I don’t think this works on Lion)
  • Enabling the debug menu in iCal

Those are just a few that I’ve found useful and interesting. Since it’s free, you may want to download it and check it out. Might have a feature you’ve been dying to have. Secrets also taps into many of the preferences of your other programs such as Adium, iTunes, Cyberduck, Skype, Preview, Transmission, etc.

Secrets Prefpane Screenshot

Hide Desktop Icons on Mac

Ever wanted to hide your desktop icons briefly and easily on Mac OS X for a presentation, screencast or just to hide everyday clutter? Here’s a simple Bash script you can use.


# checks visibility and stores value in a variable
isVisible="$(defaults read CreateDesktop)"

# toggle desktop icon visibility based on variable
if [ "$isVisible" = 1 ]; then
	defaults write CreateDesktop -bool false
	defaults write CreateDesktop -bool true

# force changes by restarting Finder
killall Finder

Paste that into a text editor and save it without a suffix/filetype as something like desktop. Then execute the following command while in the folder where you saved the script (preferably in your personal bin directory): chmod 755 desktop. You should be good to go as long as the directory you saved it in is mapped to your $PATH (If you aren’t sure what that means, read the 4th and 5th paragraphs of Clock In, Clock Out).

You can find some simple apps to do this if you are repelled by the Terminal or love extra menubar buttons; just search Google. Some of them cost money (CamouFlage – $1.99), though I don’t know who would pay for something so simple. There are some other free alternatives.

Bash, Ruby & Time Calculations

As a follow up to Clock In, Clock Out, I thought I would discuss the methods I used for calculating the amount of hours worked in my clock script, since working with time mathematically is always a bit tricky.

First off, Bash shell scripts are really great for text and file manipulation (among other powerful aspects). However, once you start getting into more complex functionality like math and anything object-oriented, you really want to switch over to another language or environment. That’s why I called upon Ruby to do my “heavy lifting” as I put it.

So let’s jump into the first timestamp the script records. Pretty basic.

TIME_IN=`date "+%Y/%m/%d %H:%M:%S"`

The backticks (`) allow me to store the result of the date command in the variable TIME_IN. (Actually, the backticks are sorta deprecated. The new convention is to use $(<command>) instead. Bad habit I suppose.) I store it in a variable so that I can use it later on to print the timestamp to Terminal (below).

echo -e "\033[1;32mStatus - Clocked in\033[0m"
echo -e "\033[0;37m$TIME_IN\033[0m"

Those funky bits of code (i.e. \033[0;37m$TIME_IN\033[0m ) are used to adjust the text color when printed in Terminal. In this case, the first line is green and the second line gray.

Now when you clock out, clock grabs the last line in the timecard, stores the new timestamp and feeds both into the timediff.rb script as follows:

TIME_IN=`tail -n 1 $TIMECARD`
TIME_OUT="Out - `date "+%Y/%m/%d %H:%M:%S"`"
HOURS="     Session Length - `timediff.rb "$TIME_IN" "$TIME_OUT"`"

You might have noticed that the TIME_OUT variable not only includes the timestamp from date but also some extra text, specifically “Out – “. Later on, clock uses the TIME_OUT variable to print to the console. timediff.rb is robust enough that this extra text is parseable and disregarded in the time difference calculation. Let’s take a look at what happens in timediff.rb

First I implement the required gems.

require 'rubygems'
require 'time'
require 'time_diff'

Next, timediff.rb uses the parse method to create a Time object with the appropriate date and time.

t1 = Time.parse(ARGV[0])
t2 = Time.parse(ARGV[1])

The ARGV parameter is an inherent array in Ruby that grabs arguments that are passed into the script when it is run, in this case $TIME_IN and $TIME_OUT from clock. Lastly, the script uses the diff method added to Time from the time_diff gem to find the difference between the two timestamps, which are subsequently sent back to clock with puts.

puts Time.diff(t1, t2, '%h:%m:%s')[:diff]

Since the output of diff() is actually a hash (see hashes in Ruby), the [:diff] call at the end of the line tells Ruby to only pass back the :diff portion of the hash with the timestamp formatted as ‘%h:%m:%s’.

Now let’s talk about timeadd.rb. In essence, clock grabs the hours for each recorded session and pumps each one into timeadd.rbas an argument.

TOTAL=`grep "Session Length -" $TIMECARD | cut -c23-30`
echo "Total Hours - `timeadd.rb $TOTAL | cut -d . -f 1`" >> $TIMECARD

First, timeadd.rb creates a base timestamp of the current day at 00:00:00 and a totalTimeInSecondsvariable. The rest of that portion is explained in the comments of the code below.

baseTime = Time.parse('00:00:00')
totalTimeInSeconds = 0.0

ARGV.each do|arg|

   # Parses the hours into a Time for today
   time = Time.parse(arg)

   # Calculates the time difference in seconds using the base time 00:00:00
   timeInSeconds = time - baseTime

   # Sums up the time in seconds
   totalTimeInSeconds += timeInSeconds


To calculate the total time worked for that period, I implemented a nice little gem called ChronicDuration. You can read more about it in a blog post by Everyday Rails. In short, ChronicDuration takes a time in seconds and converts it to a given format, which in this case is day:hours:minutes:seconds as indicated by :chrono. After that I pass it back to clock.

totalTime = ChronicDuration::output(totalTimeInSeconds, :format => :chrono)
puts "#{totalTime}"

Finally, clock does some final formatting on the timecard as explained in the previous post (download the files there too).

While Bash can handle basic math by converting time into seconds, I chose to take the Ruby route to learn some Ruby and keep the code in clock a bit more simple. Arguably, abstracting to external Ruby scripts is just as complex. However, it did simplify much of the work and I didn’t have to do a lot of tedious math and formatting in Bash.

App of the Day: Console

ConsoleI currently work as a Quality Engineer at Palantir Technologies. I do a lot of feature and product testing. As a result, one of the most common tools for testing is the POSIX tool tail.

Linux/Unix/Mac users may be familiar with tail. In layman terms, tail allows you to grab a number of lines at the end of a text based file. In testing, we use tail -f <filename> a lot because the “f” option immediately and automatically updates the CLI with the most recently written lines of text in the file you are tailing. Testers love this because they like to see the stack traces printed on their screen the moment something errors in a program or system. Many development environments have consoles built in for errors and system printing and logging (think Eclipse, NetBeans, etc.). Java based programs also have the option of having the Java console automatically open when you run a Java based program.

On my Windows box at work, I use Cygwin to run the tail command. Cygwin is a Linux-like environment for Windows that allows users to port software running on POSIX systems (such as Linux, BSD, and Unix systems) to Windows. On my Mac, I just use Terminal. However, more recently I’ve discovered an even greater tool called Console. Console actually comes as a pre-installed utility with the Mac OS X operating system. I’ve found that the Utilities folder is full of great (whadoyaknow!) utility apps. I suggest you take a gander through that folder if you haven’t already. I’ve used the Grapher app in my ECON 110 class this semester a couple times (back when I used to take notes on my computer; I’ve since switched to paper since we do more graphing than anything else. Which reminds me about a great note taking app for iPad called Notes Plus. Alas, I digress. I will save that discussion for another post.).

The reason I love Console most, is because you can tell it to bounce in the dock when stack traces print to it. You don’t have to have it open on a second monitor so it’s always visible, or worse yet peaking out on the side of the screen behind the program you are testing. Better yet, you can actually choose to have it come to the forefront for a limited amount of time (say 5 seconds) and then disappear in the background again. I’ve searched for something like this on Windows and haven’t seen anything like it. Probably because there are IDE’s and stuff, but regardless, it’s a beaut. It has other great functions for console type stuff too. Check it out if you’re a tester or programmer. It might be pretty handy.

Console for Mac OS X