Eject Disc Script not working, help!

Discussion in 'Mac Programming' started by TheNightPhoenix, Oct 3, 2009.

  1. macrumors 6502

    Joined:
    Dec 16, 2005
    #1
    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!
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Try throwing it a try block:

    Code:
    tell application "Finder"
    	try
    		eject disk "BOOTCAMP"
    	end try
    end tell
     
  3. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #3
    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
     
  4. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #4
    :( Sadly It's the same issue. "--> error number 0"

    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!
     
  5. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #5
    I'm going to be stubborn and suggest you run one more test:

    Code:
    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
     
  6. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #6
    Result:
    error "The variable theAlias is not defined." number -2753 from "theAlias" :confused:
     
  7. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #7
    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
     
  8. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #8
    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?
     
  9. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #9
    It's an internal disk? Hmmm.

    Try this:

    Code:
    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
     
  10. macrumors newbie

    Joined:
    Jun 4, 2008
    #10
    try this:

    Code:
    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
    
     
  11. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #11
    false!
     
  12. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #12
    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
     
  13. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #13
  14. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #14
    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.
     
  15. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #15
    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:

    Code:
    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
     
  16. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #16
    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
     
  17. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #17
    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.

    Code:
    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
     
  18. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #18
    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.
     
  19. macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #19
    Maybe this?

    Code:
    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
     
  20. thread starter macrumors 6502

    Joined:
    Dec 16, 2005
    #20

    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!
     

Share This Page