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

Justine Ezarik


Justine Ezarik


iJustine aka Justine Ezarik















Convert DMG image to ISO image

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/hdiutil.1.html

http://songoku.homelinux.com/wordpress/?p=361


How to put OSX Mountain Lion (10.8) on a single-layer dvd

Mac OSX, by Andy.
Hello,
the downloaded Mountain Lion image has a total size of 4.8 GB which is too big for a single-layer dvd, but if you open the image and check the size of all files you only have 4.1 GB. So here is what you have to do to get the files on a normal dvd.
  1. Open the terminal and go to the folder where the installer dmg is located (InstallESD.dmg)
  2. Convert the dmg file to an iso file with
    # hdiutil makehybrid -o MountainLion.iso
  3. Now take the iso file and burn it normally to the single-layer dvd
That should be all, have fun with the new OS 
You can create ISO images from any source disk or data by using the command line in Mac OS X. This isn’t too different than burning themthrough Terminal, and you can use either the hdiutil tool or dd command.
While the command line is generally reserved for advanced users, using it to create ISO’s isn’t too complicated and will save you the hassle of having to download any third party apps. If you’re new to the Terminal, remember that dragging & dropping files into the Terminal window will print their full path, making it easy to point to source files and preventing any navigation through the command line.

Creating an ISO with hdiutil

The most reliable method is uses hdiutil, here is the syntax:
hdiutil makehybrid -iso -joliet -o image.iso /path/to/source
Here’s an example, creating an iso from a Windows 7 installer disc, with the end result showing up on the desktop:
hdiutil makehybrid -iso -joliet -o ~/Desktop/Windows7.iso /Volumes/Windows\ 7\ Install
The -joliet flag is necessary to make the iso fully compatible with Windows and other OS’s, though if your only requirement is to use the iso on a Mac you can leave it off.

Making an ISO with dd

Another approach is by switching around a previously discussed dd command, which makes it go from burning an image to creating an image. This may not be as reliable and it requires additional steps, so use dd only if you have a good reason not to use the primary hdiutil method.
Use the ‘diskutil list’ command to discover the disks identifier that you will need to make an ISO with dd from.
dd if=/dev/dvd of=/destination/path/dvd.iso
dd is often faster than hdiutil, but it’s definitely for more advanced users.


Quickly convert Mountain Lion DMG into an ISO file

From the terminal, after downloading Mountain Lion from the App store:
hdiutil makehybrid -iso -hfs /Applications/Install\ OS\ X\ Mountain\ Lion.app/Contents/SharedSupport/InstallESD.dmg -o OSX-10.8.iso




Mimmo97 Blog Archive