Sunday, November 4, 2012

Mac OS X shell scripts / Automating Terminal Commands in Mac OS X with Bash

http://jonlandrum.com/2012/01/28/automating-terminal-commands-in-mac-os-x-with-bash/


Coming from the Linux world, Mac was somewhat familiar to me, yet somewhat foreign. One such example is the system hides most of the files and directories I was accustomed to seeing and using as a Linux user. Also, the terminal commands to show and re-hide these directories don’t follow standard Unix terminal syntax. So I had to do some researching to find what the commands even are, and then I could put them in a Bashscript that could be run with a single click. But first, the commands:
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder
killall Terminal
The first line is the business end, the second one restarts Finder with the new setting, and the last makes sure Terminal closes afterward. And you can type these in a Terminal window right now and get the desired results. But what about automating this? Instead of typing all this out — and possibly misspelling it a time or two (ask me how I know) — let’s make a shell script that contains the commands, and keep it in our user directory for easy access:
1
2
3
4
#!/bin/bash
defaults write com.apple.finder AppleShowAllFiles TRUE
killall Finder
killall Terminal
The only difference this time is the new first line. That specifies which shell we’re using, in this case Bash. It’s called the shebang line, and it tells Terminal which program to use to interpret the commands. Copy and paste those three lines into TextEdit or your favorite text editor, and save it as show.sh or something similar. Then change the “TRUE” to “FALSE” at the end of the second line, like this:
1
2
3
4
#!/bin/bash
defaults write com.apple.finder AppleShowAllFiles FALSE
killall Finder
killall Terminal
and save that as hide.sh or something similar. Put these two files in your user root directory, in the same place where your Documents, Desktop, Music, and Pictures folders are.
An example directory layout
An example directory layout. Put show.sh and hide.sh in your user root like this.
Next we have to make them executable. Open Terminal (it will already be cd’d to your user root directory) and enter this command:
$ chmod +x show.sh && chmod +x hide.sh
This will make our two files executable. Now give them a whirl:
$ ./show.sh
$ ./hide.sh
The ./ tells Terminal to run that program. This should open your Finder window with hidden files and directories showing, but faded to show you the difference. And when you run hide.sh Finder will close and reopen, with those files and directories hidden. If that’s what you got, then continue to the final step to make them clickable. If not, go back through this tutorial and start from scratch. Make sure you’ve typed everything correctly.
The last step is open a Finder window and select show.sh, then type Command-i to bring up the information menu. Under the “Open with:” dropdown, select “Other…”, and in the window that pops up, scroll to the bottom to the “Utilities” directory, open that, and then scroll to Terminal and select it.
You may have to change the “Enable:” menu from “Recommended Applications” to “All Applications” in order to select it. Tick the “Always Open With” checkbox, and click “Add”. Repeat this last step for hide.sh.
Double-click show.sh and confirm it works, then do the same for hide.sh. If everything works as expected, you’re all set! You can access these programs from the command line or from Finder the way you’ve done just now.
If you have other commands you’d like to automate, you can simply replace what’s in either of these scripts with that command, save it as a new .sh file, make it executable, set it to open with Terminal, and you’re golden. For timed automation, you can even have these programs run with cron so that they’re executed for you on a regular schedule. How’s that for automation?
~Jonathan

No comments:

Mimmo97 Blog Archive