PDA

View Full Version : Eject Disc Script not working, help!




TheNightPhoenix
Oct 3, 2009, 02:42 PM
I'm trying to write a script that will be made a start up item.
This script would auto eject all my hard drives apart form the boot disk.

I've written this

tell application "Finder"
eject disk "BOOTCAMP"
end tell

but nothing happens, in the script editor I get this under events,
tell application "Finder"
eject disk "BOOTCAMP"
--> error number 0
end tell

What could be wrong. Sorry if this seems a complete noob question, cause it is!



kainjow
Oct 3, 2009, 06:28 PM
Try throwing it a try block:

tell application "Finder"
try
eject disk "BOOTCAMP"
end try
end tell

mysterytramp
Oct 3, 2009, 08:59 PM
Maybe this is a clue ...

Running SL, I tried to eject a partition on my external and had no problems.

Then I tried to eject another partition that had an app running. I got an "error 0" but I also got a dialog box telling me the app that was running.

mt

TheNightPhoenix
Oct 4, 2009, 03:07 AM
Try throwing it a try block:

tell application "Finder"
try
eject disk "BOOTCAMP"
end try
end tell

:( Sadly It's the same issue. "--> error number 0"

Maybe this is a clue ...

Running SL, I tried to eject a partition on my external and had no problems.

Then I tried to eject another partition that had an app running. I got an "error 0" but I also got a dialog box telling me the app that was running.

mt

Nothings running on the Volume. In fact one the volumes I've tried it with, is a freshly formatted hard drive, nothing to even run on it!

mysterytramp
Oct 4, 2009, 04:58 PM
Nothings running on the Volume. In fact one the volumes I've tried it with, is a freshly formatted hard drive, nothing to even run on it!

I'm going to be stubborn and suggest you run one more test:

tell application "Finder"
repeat with pr in the processes
try
set theAlias to the application file of pr
on error
set theName to the name of pr
end try
set theDisk to (the disk of theAlias) as string
if theDisk is "BOOTCAMP:" then
set theName to the name of pr
display dialog "The offending process is: " & theName
end if
end repeat
end tell

The try block is because I have one process, "mdworker", which if you call its properties, you get a lot of missing values, which then screws up theAlias variable. Luckily it's the last process.

mt

TheNightPhoenix
Oct 5, 2009, 10:05 AM
I'm going to be stubborn and suggest you run one more test:

tell application "Finder"
repeat with pr in the processes
try
set theAlias to the application file of pr
on error
set theName to the name of pr
end try
set theDisk to (the disk of theAlias) as string
if theDisk is "BOOTCAMP:" then
set theName to the name of pr
display dialog "The offending process is: " & theName
end if
end repeat
end tell

The try block is because I have one process, "mdworker", which if you call its properties, you get a lot of missing values, which then screws up theAlias variable. Luckily it's the last process.

mt

Result:
error "The variable theAlias is not defined." number -2753 from "theAlias" :confused:

mysterytramp
Oct 5, 2009, 06:49 PM
Result:
error "The variable theAlias is not defined." number -2753 from "theAlias" :confused:

Sorry ... it was a quick and dirty effort on my part to do something that should have required a little more finesse ... What's happening is that there's a process where the Finder doesn't have values for its properties. The try block was an attempt to try to catch that. It worked on my machine but after I posted it, I realized it might not be generalized enough.

OK ... Let's take a different tack ... What happens when you try to eject Bootcamp from the Finder?

mt

TheNightPhoenix
Oct 6, 2009, 04:10 AM
Sorry ... it was a quick and dirty effort on my part to do something that should have required a little more finesse ... What's happening is that there's a process where the Finder doesn't have values for its properties. The try block was an attempt to try to catch that. It worked on my machine but after I posted it, I realized it might not be generalized enough.

OK ... Let's take a different tack ... What happens when you try to eject Bootcamp from the Finder?

mt

From the finer it ejects no problem.

I've this script with a series of different drives, and I've also tried it on the leopard machine at work, but none of them work.
Perhaps there is something fundamentally wrong with the commands I'm trying?

Edit:
I just tried to something a little different I changed it to eject a USB drive, and it worked, perhaps the commands do not work for internal drives?

mysterytramp
Oct 6, 2009, 06:12 AM
It's an internal disk? Hmmm.

Try this:

tell application "Finder"
ejectable of disk "BOOTCAMP"
end tell

If it says "false" in the Result window we know we need to do something else.

mt

fredthefool
Oct 6, 2009, 10:39 AM
try this:


tell application "Finder"
repeat with myEntry in list disks
if (get startup of disk myEntry is false) then
try
eject disk myEntry
on error
try
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & myEntry)
end try
end try
end if
end repeat
end tell

TheNightPhoenix
Oct 6, 2009, 03:56 PM
It's an internal disk? Hmmm.

Try this:

tell application "Finder"
ejectable of disk "BOOTCAMP"
end tell

If it says "false" in the Result window we know we need to do something else.

mt

false!

TheNightPhoenix
Oct 6, 2009, 03:57 PM
try this:


tell application "Finder"
repeat with myEntry in list disks
if (get startup of disk myEntry is false) then
try
eject disk myEntry
on error
try
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & myEntry)
end try
end try
end if
end repeat
end tell


tell application "Finder"
list disks
--> error number -10004
end tell
tell current application
list disks
--> {"Macintosh HD", "Junk", "Video HD", "iTunes", "BOOTCAMP"}
end tell
tell application "Finder"
get startup of disk "Macintosh HD"
--> true
get startup of disk "Junk"
--> false
eject disk "Junk"
--> error number 0
get startup of disk "Video HD"
--> false
eject disk "Video HD"
--> error number 0
get startup of disk "iTunes"
--> false
eject disk "iTunes"
--> error number 0
get startup of disk "BOOTCAMP"
--> false
eject disk "BOOTCAMP"
--> error number 0
end tell



The really annoying thing is, if I drag them all to the trash they all eject bar Macintosh HD

jeremy.king
Oct 6, 2009, 04:05 PM
If your goal is to simply hide the drive(s) from Finder - no need for a login script - just use the SetFile utility to set the visibility attribute

http://www.macosxhints.com/article.php?story=20080124184544791


SetFile -a V /Volumes/BOOTCAMP
sudo killall Finder

TheNightPhoenix
Oct 7, 2009, 01:30 AM
If your goal is to simply hide the drive(s) from Finder - no need for a login script - just use the SetFile utility to set the visibility attribute

http://www.macosxhints.com/article.php?story=20080124184544791


SetFile -a V /Volumes/BOOTCAMP
sudo killall Finder


If I do this, how do I go about un-hiding them so I can use them. Also does simply hiding them leave the powered up and spinning?

My main aim is not to have 3 hard discs spinning 24/7 when they are only used on occasions.

mysterytramp
Oct 7, 2009, 05:27 AM
It's curious that trying to eject a disk that is not ejectable doesn't report an error. That's possibly worth a bug report to Apple. I think the solution might be in tweaking what fredthefool suggested:

tell application "Finder"
repeat with myEntry in list disks
if startup of disk myEntry is false then
if ejectable of disk myEntry is false then
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & myEntry)
else
eject disk myEntry
end if
end if
end repeat
end tell

I ran this on my system. I have a single external drive and its partitions are ejectable, so I'm not the best test subject.

Hope this works.

mt

TheNightPhoenix
Oct 8, 2009, 04:22 AM
It's curious that trying to eject a disk that is not ejectable doesn't report an error. That's possibly worth a bug report to Apple. I think the solution might be in tweaking what fredthefool suggested:

tell application "Finder"
repeat with myEntry in list disks
if startup of disk myEntry is false then
if ejectable of disk myEntry is false then
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & myEntry)
else
eject disk myEntry
end if
end if
end repeat
end tell

I ran this on my system. I have a single external drive and its partitions are ejectable, so I'm not the best test subject.

Hope this works.

mt

We might have a winner here, Script ejected all my drives. I made it a start up and it worked perfectly.

However I then switched on iDisk syncing, this now causes an error and none of the discs eject. So I've switched the iDisk off. Any ideas how to get around the iDisk issue?

Otherwise Thank You for all your help!!!:D

mysterytramp
Oct 8, 2009, 05:39 AM
However I then switched on iDisk syncing, this now causes an error and none of the discs eject. So I've switched the iDisk off. Any ideas how to get around the iDisk issue?

What's the error message?

This script works on my setup. What I can get to distinguish my iDisk from my other mounted volumes is that the iDisk is the only disk that has NFS formatting, so I trap that.

If you have other disks with NFS formatting and you want them ejected, the only thing I can suggest is hardcoding the names of your drives in the script.

tell application "Finder"
repeat with myEntry in list disks
if startup of disk myEntry is false then
if ejectable of disk myEntry is false then
tell application "System Events" to set theFormat to format of disk myEntry
if theFormat is not NFS format then
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & myEntry)
end if
else
eject disk myEntry
end if
end if
end repeat
end tell

mt

TheNightPhoenix
Oct 8, 2009, 01:36 PM
What's the error message?

This script works on my setup. What I can get to distinguish my iDisk from my other mounted volumes is that the iDisk is the only disk that has NFS formatting, so I trap that.

If you have other disks with NFS formatting and you want them ejected, the only thing I can suggest is hardcoding the names of your drives in the script.

tell application "Finder"
repeat with myEntry in list disks
if startup of disk myEntry is false then
if ejectable of disk myEntry is false then
tell application "System Events" to set theFormat to format of disk myEntry
if theFormat is not NFS format then
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & myEntry)
end if
else
eject disk myEntry
end if
end if
end repeat
end tell

mt

error "Finder got an error: Volume idisk on disk4s2 failed to unmount: 'FileSyncAgent says so!'" number 1


So close, Though the good news is I've now bought a book on Apple script, :)

I'm wondering if we can filter out the idisk by filtering out disks with certain names? This way I could at a later date stop it ejecting other disks too.

mysterytramp
Oct 8, 2009, 08:13 PM
Maybe this?

tell application "Finder"
repeat with myEntry in list disks
if startup of disk myEntry is false then
if ejectable of disk myEntry is false then
if the URL of disk myEntry contains "BOOTCAMP" then
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & myEntry)
end if
else
eject disk myEntry
end if
end if
end repeat
end tell

mt

TheNightPhoenix
Oct 14, 2009, 02:44 AM
Maybe this?

tell application "Finder"
repeat with myEntry in list disks
if startup of disk myEntry is false then
if ejectable of disk myEntry is false then
if the URL of disk myEntry contains "BOOTCAMP" then
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & myEntry)
end if
else
eject disk myEntry
end if
end if
end repeat
end tell

mt


I ende up using the script that worked without the iDisk active and deactivated my iDisk, I'd never used the iDisk such anyway.

Thanks for all your help!