Thursday, August 13, 2009

Bash script to Mount/Unmount .ISO from Nautilus

I was googling to find a software for Ubuntu which helped me to mount and unmount .ISO files, you know the same job DaemonTools does so flawlessly on Windows.
Well I found a four years old post in the Tutorials&Tips section of Ubuntu Forums where Sonny explains how to create a script which will enable you to to the job directly from Nautilus.
I know that you can already do it on the newer version of Ubuntu but I find sometimes nice to hack here and around obtaining the same effect with some home brewed script.
I tried it and it perfectly worked on my Jaunty version so I thought it would have done good to spread the word around.
Here's the recipe.
First thing to do is create the folder where the files are going to be mounted.  Open a terminal and type:
sudo mkdir /media/iso
You can create whichever folder you like but the Sonny's way will place the folder next to the cdrom and other media in the system
Now we must create the script itself so always in the terminal type:
gedit ~/.gnome2/nautilus-scripts/Mount\ Iso
When the editor opens up paste the following code inside, save and exit
#!/bin/bash
for uri in $NAUTILUS_SCRIPT_SELECTED_URIS; do
gnome-terminal -x sudo mount -o loop -t iso9660 "$1" /media/iso
done
Now get back to the terminal because we need to make the script executable, so type:
chmod +x ~/.gnome2/nautilus-scripts/Mount\ Iso
That's all folks! Now, next time you encounter a .ISO file just right click on it and from the context menu select script and then Mount Iso. Type the user password and you will find the .ISO mounted and ready to be used directly on your desktop.
The next part is to create the script to unmount the .ISO. It doesn't really work for me this but I suppose it's worth give it a try and let me know if it works for you.
open a terminal and type:
gedit ~/.gnome2/nautilus-scripts/Unmount\ Iso
When the text editor showup put this code:
#!/bin/bash
for uri in $NAUTILUS_SCRIPT_SELECTED_URIS; do
gnome-terminal -x sudo umount "$1"
done
Make executable by typing in the terminal:
chmod +x ~/.gnome2/nautilus-scripts/Unmount\ Iso
And now you can simply mount and unmount the .ISO images directly from Nautilus and with just two clicks. According to Sonny it's possible to mount also .cue and .nrg images by just changing the parameter in the script. You could then have one script for each type of image you need to mount/unmount. Again, if you try to do so drop me a line to let me know if it worked or not.

No comments:

Post a Comment