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

10.5: Eject stubborn disk images via AppleScript System 10.5
Leopard sometimes can be a little reluctant to eject disks and disk images in the Finder. It doesn't matter if you click the Eject button in the Finder's sidebar, drag the disk to the Trash, or right-click and choose Eject, the Finder just silently ignores you. You can open Disk Utilty though and eject it fine that way (unless there are files open on the image, of course). This AppleScript will eject those troublesome disks:
(*
    Eject Disks © RickoKid 2008
    Version 0.1
  
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*)

set prefaceMessage to ""
set confirm to false

-- check if current selection in Finder contains any ejectable disks
set ejectables to my findEjectable(selection of application "Finder")
if (count of ejectables) is greater than 0 then
  -- Ejectable disks are selected in Finder, add them to the eject list
  set ejectList to ejectables
  set prefaceMessage to "Note: Ejecting disks selected in Finder." & return & return
else
  -- No ejectable disks in Finder selection; look for all ejectable disks on the system ...
  try
    tell application "Finder" to set allEjectables to name of every disk whose ejectable is true
  on error
    -- Finder throws an error (tantrum?) if it can't match the 'whose' condition, ie when there are no disks to match
    display alert "No ejectable disks" message "Couldn't find any ejectable disks on your system!" as informational buttons {"OK"} giving up after 10
    return "Error: No ejectable disks."
  end try
  
  -- if ejectable disks are found ...
  if (count of allEjectables) is 1 then
    set ejectList to allEjectables
    set confirm to true
  else if (count of allEjectables) is greater than 1 then
    set ejectList to choose from list allEjectables with title "Eject Disk" with prompt "Choose which disk(s) to eject" OK button name "Eject" with multiple selections allowed without empty selection allowed
  end if
end if

my kickoffEject(ejectList, prefaceMessage, confirm)

on open droppedDisks
  set ejectables to findEjectable(droppedDisks)
  my kickoffEject(ejectables, "Note: Ejecting dropped disks." & return & return, false)
end open

on findEjectable(selectedList)
  set ejectables to {}
  tell application "Finder"
    repeat with selectedItem in selectedList
      if class of selectedItem is alias then
        set nameOfItem to name of selectedItem
        set matchingDisks to (every disk whose name is nameOfItem and ejectable is true)
        repeat with matchItem in matchingDisks
          set ejectables to ejectables & {name of matchItem}
        end repeat
      else if class of selectedItem is disk and ejectable of selectedItem then
        set ejectables to ejectables & {name of selectedItem}
      end if
    end repeat
  end tell
  return ejectables
end findEjectable

on kickoffEject(ejectList, preface, confirm)
  set textDelimiters to AppleScript's text item delimiters
  set AppleScript's text item delimiters to return
  -- if disks are chosen (don't try if Cancel is clicked) or 1 is confirmed or disks are selected in Finder
  set messages to {}
  set moreInfo to {}
  
  if (count of ejectList) is greater than 0 then
    if confirm then
      set ejectIt to display alert "Eject" message "Are you sure you want to eject the following disk(s)?" & return & return & (ejectList as text) as warning buttons {"Cancel", "Eject"} default button "Eject" cancel button "Cancel" giving up after 20
      if button returned of ejectIt is "Cancel" then return "User cancelled."
    end if
    
    set AppleScript's text item delimiters to return & tab
    repeat with ejectDisk in ejectList
      try
        set ejectResult to (do shell script "touch /tmp/" & ejectDisk & "-report.txt; hdiutil detach '/Volumes/" & ejectDisk & "' &> /tmp/" & ejectDisk & "-report.txt &")
        repeat while ejectResult is equal to ""
          delay 0.5
          set ejectResult to (do shell script "cat  /tmp/" & ejectDisk & "-report.txt")
        end repeat
        do shell script "rm /tmp/" & ejectDisk & "-report.txt"
        if ejectResult contains "error" then error ejectResult
        set messages to messages & {"Ejected disk \"" & ejectDisk & "\" successfully."}
        set moreInfo to moreInfo & {"Success: disk \"" & ejectDisk & "\": " & return & tab & (paragraphs of ejectResult as text)}
      on error errorMsg
        set messages to messages & {"Error: disk \"" & ejectDisk & "\" could not be ejected."}
        set moreInfo to moreInfo & {"Fail: disk \"" & ejectDisk & "\":" & return & tab & errorMsg}
      end try
    end repeat
    
    set AppleScript's text item delimiters to return
    
    if (count of messages) is not 0 then
      set report to display alert "Ejection Results" message preface & (messages as text) as informational buttons {"More info ...", "Close"} default button "Close" giving up after 60
      if button returned of report is "More info ..." then
        set moreDetailedInfo to {}
        repeat with eachMessage in moreInfo
          if eachMessage contains "error" then
            set diskDev to do shell script "mount | grep " & word 3 of eachMessage & " |awk '{print $1}'"
            set openFiles to paragraphs of (do shell script "lsof -Fc " & diskDev & " |  sed -ne '/c/ s/c//p'")
            if (count of openFiles) is greater than 0 then
              set AppleScript's text item delimiters to return & tab & tab
              set eachMessage to eachMessage & return & tab & "Processes open on disk:" & return & tab & tab & (openFiles as text)
              set AppleScript's text item delimiters to return
            end if
          end if
          set moreDetailedInfo to moreDetailedInfo & {return & eachMessage}
        end repeat
        display alert "Ejection Details" message (moreDetailedInfo as text) as informational buttons {"OK"} default button "OK"
      end if
    end if
  end if
  set AppleScript's text item delimiters to textDelimiters
end kickoffEject
The script will let you eject troublesome disks quickly if the Finder is ignoring your requests. It will also tell you which applications are stopping the disk from being ejected, as opposed to the Finder's generic "something is open on that disk, but I'm not telling you what!" You can find a ready-to-run version of the script in this post on my site.

[robg adds: When you run the AppleScript, a list of mounted disks will appear, including FireWire drives, CD/DVDs, and disk images. Select the one you'd like to eject then click the Eject button. I didn't have any 'stuck' devices to test this with, but it worked fine on my three test runs with a FireWire disk, a CD, and a disk image.]
    •    
  • Currently 2.00 / 5
  You rated: 1 / 5 (8 votes cast)
 
[20,646 views]  

10.5: Eject stubborn disk images via AppleScript | 16 comments | Create New Account
Click here to return to the '10.5: Eject stubborn disk images via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Eject stubborn disk images via AppleScript
Authored by: luigi193 on May 21, '08 08:04:34AM

What a great hint!!! Theres been a million times that a HDD won't eject and I don't know what program is using it... this script is awesome!



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: Darnell.Turner on May 26, '08 01:42:11PM

10.5.3: Wont work with FireWire drives on my MacBook Pro.
Always anwers "no ejectable drives found".



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: ctierney on May 21, '08 08:42:19AM
If you need to do this from a shell script, you could try this (where $VOLNAME is the name of your disk):
hdiutil detach `mount | sed -ne 's|^\(/dev/[^ ]*\).*/Volumes/'"$VOLNAME"'.*mounted by.*|\1|p'` -force


[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: asmeurer on May 21, '08 12:10:28PM

The freeware program Sloth will tell you all the files you have open on your system. A simple search for a disk name reveals all open files from it.



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: auco on May 21, '08 01:26:38PM

Unfortunately this script didn't work for me. It returned that the "resource is busy" ;-(

However, there's a simple command to force eject very stubborn disks (maybe a little bit more safely than simply unplugging)

in Terminal type:
hdiutil eject -force /Volume/volumeNameHere



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: ctierney on May 21, '08 02:00:28PM

I was under the impression that eject (synonym for detach) requires a device name. Does it also work with a /Volumes path?



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: hamarkus on May 21, '08 04:56:05PM

Tried it on a mounted drive, produced a dialogue saying it had ejected it but the drive was still there. Ironically, ejecting it in the Finder worked without problems.



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: bkwest918 on May 21, '08 05:17:40PM

Do you realize how utterly stupid it is to release an apple script under the GPL... that is the most retarded thing I have seen today.

/b



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: robg on May 22, '08 05:44:22AM

AppleScript is code, right? Code is copyrightable, right? So why is the GPL a bad choice for an author to use for his/her code, regardless of what language it is written in?

-rob.



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: RickoKid on May 22, '08 11:21:19AM

<sarcasm>Releasing open source code under an open source licence ... that is retarded!</sarcasm>

I guess you wouldn't consider posting opinions but not explaining your reasoning retarded huh?

---
Visit my technology (and Apple in particular) blog at http://tumbleseed.torrfamily.org/



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: gxw on May 21, '08 06:06:58PM

Script does not see mounted CIFS shares as ejectible volumes :(



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: RickoKid on May 22, '08 01:06:25PM
As per your suggestion, I've updated the script to recognise network volumes.

---
Visit my technology (and Apple in particular) blog at http://tumbleseed.torrfamily.org/

[ Reply to This | # ]

10.5: Eject stubborn disk images via AppleScript
Authored by: RickoKid on May 22, '08 11:17:37AM

Yes, it does work with a path (as in /Volumes/Disk), and if you look at the source for the script you'll see that's exactly how it does it!

---
Visit my technology (and Apple in particular) blog at http://tumbleseed.torrfamily.org/



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: ctierney on May 22, '08 12:24:55PM

Referencing by mount point doesn't seem to be supported in 10.3.9 which what I'm using. But then this hint was filed under 10.5.

--
Cole



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: RickoKid on May 22, '08 11:53:14AM

This hint has been marked 10.5 only (presumably because it's a workaround for a bug in the Leopard Finder), but it will work in 10.4, and can be useful for quickly ejecting lots of disks, and finding out what process is blocking a disk being unmounted.

---
Visit my technology (and Apple in particular) blog at http://tumbleseed.torrfamily.org/



[ Reply to This | # ]
10.5: Eject stubborn disk images via AppleScript
Authored by: RickoKid on Sep 03, '09 07:03:51AM
The script has been updated to version 0.3 to detect more stuck discs.

---
Visit my technology (and Apple in particular) blog at http://tumbleseed.torrfamily.org/

[ Reply to This | # ]