PDA

View Full Version : Script to eject Volumes


jerrykrinock
Mar 26, 2004, 08:45 AM
I have an Applescript which ejects my Firewire disk (with 2 volume/partitions) after my daily backup. It "tells application Finder" to do this, but when running Path Finder, I find that, even though the disk has disappeared in the Finder, Path Finder hangs on to it, so in order to completely eject the disk I have to "tell application Path Finder" to eject it also.

It works, but it seems like a kludge. It seems to me that they system should be in charge of what volumes are mounted, and Finder and Path Finder should simply reflect what the system says. Is there a better way to do this, maybe with a shell script?

Jerry

Here is my Applescript:

tell application "Finder"
try
eject disk "Firelite10GB"
eject disk "Firelite19GBBoot"
end try
--quit
end tell
tell application "Path Finder"
try
eject disk "Firelite10GB"
eject disk "Firelite19GBBoot"
end try
--quit
end tell
do shell script "sleep 8"
display dialog "You may now pull the plug on the Firelite HD. It's been 8 seconds. Both partitions should have been ejected."

ecammit
Mar 29, 2004, 09:53 PM
you could write a shell script that calls 'umount' on the appropriate folder in /Volumes and then rmdir the folder. I believe this removes the volume from both pathfinder and finder

jerrykrinock
Mar 30, 2004, 11:04 AM
I found out today that my script does not always work, because Path Finder returns the followin error when I tell it to unmount a disk (without telling the Finder to do same):

Applescript Error. Path Finder got an error: NSReceiversCantHandleCommandScriptError

That reads as though "eject" is not implemented, even though it is in Path Finder's dictionary.

Meanwhile, I'm trying to implement the shell script suggested by ecammit. There is apparently some trick in getting "umount" to work. I'm looking for a tutorial on disk mounting, /Volumes, umount, Firewire, etc.

Can Cocoatech please confirm if the Applescript "eject" command is implemented? My Applescript works fine with the Finder.

ecammit
Mar 30, 2004, 11:29 AM
Sometimes you have to be the superuser to use umount (depending upon how you mounted it. For example to remove a volume called 'Music', I type:

sudo umount /Volumes/Music
sudo rmdir /Volumes/Music

If you aren't familiar with sudo, if you are in the admin group, you are in the file /etc/sudoerrs. If not, you can add yourself. It asks for your password. Then for the next couple of minutes any command you type beginning with sudo gets executed as root.

If you would like to create an automatic script that doesn't require you to enter a password, I recommend using a setuid executable. Make a file with the following:

#!/bin/sh
umount /Volumes/Music
rmdir /Volumes/Music

Save the file as something like 'unmount_drive'
Make the file executable 'chmod 775 unmount_drive'
Give root ownership 'sudo chown root unmount_drive'
Set the SETUID bit of the file 'sudo chmod u+s unmount_drive'

The setUID bit allows any executable to run as that file's owner. The caveats for security are that the setuid bit can only be set by that file's owner (remember, sudo runs as root), and any modification to the file in any way will clear the bit. So as long as you don't put a security hole in your script, you aren't giving unfettered root access.

Everytime your type './unmount_drive' ('unmount_drive' if . is in your path), you are running the 2 commands in the file as root and there is no reason to sudo.

After this, you can use applescript to tell the Terminal application to execute your script. I hope this helps.