Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Faster, better disk image mounting via a script System

Mounting a disk image with Disk Copy is so lame, especially for an encrypted disk image. You have to wait for Disk Copy to launch, then you have to wait for Disk Copy Helper to launch. If you click Cancel in the password dialog, it gives you an error alert. If you click OK and the volume was already mounted, it also gives you an alert.

You can put Apple to shame by coding your own disk mounting application, which will run 35% faster, prevent stupid alerts, and allow you to launch an application (such as Mail) after the volume mounts. It will take you only five minutes.

Just create a file, called Mount.sh containing the lines:

#!/bin/csh -f
/usr/bin/hdid "$1"
if ($status != 0) exit
/usr/bin/open -a "Mail"

Omit the last line if you don't want to launch the Mail program. If you use a text editor, such as BBEdit, make sure the file has Unix, not Mac, line endings and make sure that the # is the very first character in the file. You can also just type cd;cat > Mount.sh in the Terminal, type or paste in the lines, and end with a control-D. Drag your file onto the freeware program DropScript 0.5, which you can download from Apple's OS X download page. It will create an application called DropMount.

Select your disk image file (extension .dmg or .sparseimage for a sparse disk image) and type command-I to Get Info. Reveal the Open with section, select Other... from the pull-down menu, and select your DropMount application in the file browser. Close the Get Info window.

Double-clicking on your disk image will now use your little application to mount the volume. You can open the Console to watch the mount occurring, if you're interested. If the volume is not mounted and you double-click on an alias to a folder within it, the Finder will mount the volume using a unique mechanism, bypassing both your application and Disk Copy. Because your application doesn't run, Mail will not be launched. It's a Finder feature, not a bug!

    •    
  • Currently 2.40 / 5
  You rated: 1 / 5 (5 votes cast)
 
[22,098 views]  

Faster, better disk image mounting via a script | 25 comments | Create New Account
Click here to return to the 'Faster, better disk image mounting via a script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Faster, better disk image mounting via a script
Authored by: jzsimon on Mar 10, '03 11:04:14AM

Works for me.

You can also then set your new DropMount application to be the default app for all .dmg files. The price you'll pay for speed is that you'll lose the standard .dmg icon.



[ Reply to This | # ]
How to set the new dropmount app as default?
Authored by: jasonxz on Mar 10, '03 09:21:19PM

This seems simple, and I'm probably overlooking some basic pref...



[ Reply to This | # ]
Click "Change All" in the Get Info window
Authored by: noworryz on Mar 11, '03 12:12:57PM

When you have the "Open With" section of the Get Info window revealed, notice that it says "Use this application to open all documents like this" and that there is a "Change All..." button.



[ Reply to This | # ]
Why do people always write csh scripts?
Authored by: a1291762 on Mar 10, '03 06:18:26PM

csh is evil. Stop using it!

#!/bin/sh
/usr/bin/hdid "$1"
[$? -ne 0] && exit
/usr/bin/open -a "Mail"



[ Reply to This | # ]
We do it just to taunt you...
Authored by: noworryz on Mar 10, '03 06:48:44PM

That and because:

  1. The standard OS X shell for users is /bin/tcsh.
  2. /bin/sh has no "if" statement, so you have to replace if ($status != 0) with [$? -ne 0] && -- not a very obvious construct for non-programmers.
  3. The csh man page is less geeky than the sh man page.


[ Reply to This | # ]
We do it just to taunt you...
Authored by: thrig on Mar 10, '03 08:28:18PM
  1. A leading #!/bin/sh on a script in no way involves csh or tcsh. That Apple makes a C-ish shell the default shell is unfortunate.
  2. /bin/sh on OS X does have an if statement. And I strongly doubt csh is any better for "non-programmers" than a Bourne-based shell.
  3. C-ish shells complicate matters as admins have to document and maintain two different syntaxes, and figure out which one the user is using when doing support.
  4. Furthermore, csh and tcsh offer no benefits over the rich family of Bourne-based shells (ksh, bash, zsh), and plenty of drawbacks, which are well documented in "CSH Programming Considered Harmful."

    http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/



[ Reply to This | # ]
Migration guide?
Authored by: hayne on Mar 10, '03 09:40:22PM

As has already been remarked above, the main reason why people write scripts in 'csh' is that they are used to 'csh' from using it interactively (in Terminal). Someone should write a migration guide for people (like me) whose default shell has been 'csh' (or 'tcsh') for many years and who therefore have a lot of inertia that works against changing shells. Such a guide would show the equivalents of such 'csh' things as aliases, foreach loops, etc, and point out the "gotchas" (e.g. my experience is that whitespace matters more in 'sh' than it does in 'csh' but I'm not sure where & how.)



[ Reply to This | # ]
We do it just to taunt you...
Authored by: mervTormel on Jul 09, '03 10:42:22AM

worry...

$ man sh
...
Reserved Words

Reserved words are words that have special meaning to the
shell and are recognized at the beginning of a line and
after a control operator. The following are reserved
words:

! elif fi while case
else for then { }
do done until if esac
...



[ Reply to This | # ]
We do it just to taunt you...
Authored by: almage on Mar 26, '04 09:59:03AM

Actually, in Panther, the default shell for a new user is bash. This is a change from Jaguar, where tcsh was the default shell for a new user.

My two-cents: I use tcsh as my login shell, and do ALL my shell scripting in Bourne shell (sh). I have little difficulty with the sh syntax, and have found the case structure of sh to be superior to that of csh.



[ Reply to This | # ]
Problem
Authored by: ultimanine on Mar 10, '03 09:54:37PM

Problem here...

When I drag a .dmg file onto DropMount, it opens and mounts the disk image almost immediately.

When I tell all .dmg files to open with DropMount, then double click on a .dmg file, DropMount simply hangs with the beach ball cursor.

Any ideas? Running 10.2.4.

---
Macs and music:
saviors of society



[ Reply to This | # ]
Try opening the console...
Authored by: noworryz on Mar 11, '03 12:08:50PM

Open the console before you double-click and see if there is any information.

It's always worth trying the following: boot from the OS X Install CD, select "Open Disk Utility..." from the Installer menu, click on "Repair Disk Permissions" in the First Aid tab, and wait a half-hour or so. This often fixes system flakiness.

Also, if you double-click on an alias to a file/folder on an unmounted volume, the Finder often hangs. This has nothing to do with your application -- it is a system bug that has been around for a long time. The March security update seems to have reduced, but not eliminated, the problem.

[ Reply to This | # ]

Try opening the console...
Authored by: mnewman on Jul 07, '03 11:38:47PM

Sorry to be posting so late on this hint and sorry that I'm not cleverer than I am, but:

This script did not work for me as written. It works OK if I remove the escaped quotes in the second line. It also works OK from the command line or as a drag-and-drop application if I leave the quotes unescaped. Here's what the console output looks like either way when I try to double click on a .dmg file:

/usr/bin/hdid: "/"/Users/mnewman/Desktop/PTHClock.app.dmg"" unrecognized: No such file or directory


---
Mike Newman
Saipan and Narita Layover Pages:
http://net.saipan.com/personal/mike_newman/



[ Reply to This | # ]
Another possibility...
Authored by: noworryz on Mar 12, '03 09:37:11PM

Another possibility is that you've left other applications, created with DropScript, sitting around on your disk, which your system has confused with your DropMount application. Use the following steps to make your application unique, as far as LaunchServices are concerned.

Control-click on your application, select "Show package contents," and double-click on the Contents folder. Discard the PkgInfo file. Select the Info.plist file and do a Command-I to Get Info. Reveal the "Ownership & Permissions" section and chose "Read & Write" for owner access. Close the Get Info Window.

Double click on the Info.plist file to run Property List Editor. Reveal the "Root" list and make the values for CFBundleIdentifier and CFBundleSignature something unique and unusual. Close and save.

Go to the terminal and delete these caches:


sudo rm /Library/Caches/*
sudo rm /System/Library/Caches/*

Restart your computer. Do a "Get Info" on a .dmg file, reveal the "Open with" section, select Other... from the pull-down menu, and select your DropMount application in the file browser. Click on the "use this application to open all documents like this" button. Close the Get Info window. Now try double-clicking on a .dmg file.

[ Reply to This | # ]

Faster, better disk image mounting via a script
Authored by: mm2270 on Mar 10, '03 10:35:02PM

Yeah, it's easy. Just select any .dmg file and choose get info... on the file. Then in the "Open with:" turn down menu, choose your new script app as the application to open the .dmg, but make sure to click on the "Change all" button. When you do that, OS X will know to open all future .dmg's with the script. Occasionally, you need to do it more than once for it to stick.



[ Reply to This | # ]
Faster, better disk image mounting via a script
Authored by: mm2270 on Mar 10, '03 10:37:12PM

Oops, I was trying to reply to the question above: "How to set the new dropmount app as default?" but somehow posted a new message. Sorry.



[ Reply to This | # ]
Faster, better disk image mounting via a script
Authored by: readparse on Mar 11, '03 12:26:11AM

Maybe I'm missing something here, but this is how I mount image files:

hdiutil mount file.dmg

Easy. hdiutil rocks in many, many ways. Check it out.

Oh, by the way, that command will (in addition to mounting the volume and showing the icon on the desktop) output a few lines that indicate the device that it's using for the mount. You can use that later to unmount...

Example... when you mount, you might see this:

/dev/disk1 Apple_partition_scheme
/dev/disk1s1 Apple_partition_map
/dev/disk1s2 Apple_HFS /Volumes/Camino

To eject, you use...

hdiutil eject disk1

Beautiful.



[ Reply to This | # ]
"hdiutil mount" actually runs hdid
Authored by: noworryz on Mar 11, '03 12:21:13PM

The hint is, of course, a way to mount images using the GUI interface, not the Terminal. "Simple" is not an adjective most users would use to describe interacting with the system using the Terminal.

By the way, when you type "hdiutil mount" in the Terminal, the program actually runs "hdid" to mount the image. That is why the little script uses "hdid."



[ Reply to This | # ]
A Problem
Authored by: jasonxz on Mar 11, '03 12:27:00AM

Doesn't seem to work with images that pop-up an agree/disagree dialog when the image is mounted via DiskCopy. Anyone know of a workaround?



[ Reply to This | # ]
Here's the fix...
Authored by: noworryz on Mar 11, '03 12:43:10PM

The hidid program is waiting for a "Yes" to agree to the license. Here's the fixed script, which also works for image files that are not asking you to agree:


#!/bin/csh -f
echo "Y" | /usr/bin/hdid "$1"
if ($status != 0) exit
/usr/bin/open -a "Mail"

Just delete or comment out the last line (with a leading "#") if you don't want to launch an application. Do not delete the first line; it is a special comment.

[ Reply to This | # ]

Thanks!
Authored by: jasonxz on Mar 11, '03 06:25:29PM

Works perfectly now.



[ Reply to This | # ]
Faster, better disk image mounting via a script
Authored by: bluehz on Mar 11, '03 12:38:18AM

There is also a GUI app released last week called "Mount" that claimed faster disk mounting. I was skeptical, but it works as advertised. Also has option to auto-accepts aggrement screens. Great stuff!

http://mount.houchin.us/



[ Reply to This | # ]
Just a few bugs in V1.0
Authored by: noworryz on Mar 11, '03 01:21:44PM

Fast and easy. However, Mount v1.0 doesn't properly quit after mounting an image.

The preferences dialog is unclear: it has two checkboxes, "Stay open after mounting image" and "Hide application after mounting image." None of the four combinations (only three of which are valid) seem to get it to quit.

The author also didn't test it with encrypted images -- it generates exceptions if the "Cancel" button is pressed in the password dialog. Maybe version 1.1 will fix these problems.

[ Reply to This | # ]

Just a few bugs in V1.0
Authored by: houchin on Mar 12, '03 01:22:24AM

Hi, This is the guy who wrote Mount.app. 1.0.1 is due out any day. I'm just waiting for the two guys that helped with the French and German localization to double check the final version.

1.0.1 fixes the quitting bug (which was a really stupid error caused by a rewrite of a portion of the license display code), and it also fixed it so that if you cancel the password request, you don't get an error. There are several other improvements and tweaks as well.



[ Reply to This | # ]
Faster, better disk image mounting via a script
Authored by: houchin on Mar 12, '03 01:33:03AM

Mount.app is really just yet another GUI to /usr/bin/hdid. The reasons I wrote it were:

- I wanted something that would stay open (I'm too impatient to wait for an app to start every time I want to mount an image
- I wanted something that handled license agreements in such a way that I was presented the agreement
- I wanted to learn Cocoa

(In some ways I regret adding the capability to automatically accept license agreements. Despite the warnings, I fear that someday someone is going to get in trouble because there were terms in the agreement that they don't like, but agreed to anyway without seeing them)

(1.0.1 is due out any day that fixes the couple bugs that have been mentioned)



[ Reply to This | # ]
Faster, better disk image mounting via a script
Authored by: iGav on Mar 26, '04 11:28:55AM

I don't know if this has always worked, but I've found a quick way to mount DMG files from the Dock.

The only thing is to access the shortcut, you must have launched the image whichever way so that you have the image icon mounted on the desktop.

If you drag the disk icon from the desktop onto the dock, it mounts virtually instantly even on my clunky G3 iMac. It seems to bypass Disk Utility completely



[ Reply to This | # ]