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

Eject nearly any disk via AppleScript System
I'm a very keyboard-based person; I use a keyboard shortcut whenever I can. David Pogue tells of KIAFTMA -- the Keyboard Is Always Faster Than the Mouse Association. So the fact that the Eject key is restricted to the optical drive frustrates me.

To solve the problem, I created an AppleScript to make this easy for me. It calls on the disk powers of System Events and Finder; that way, you can eject any disk (except volumes over intranets like a home network) with a keystroke or two. Here is the code I used:
tell application "System Events" -- I don't target the Finder
  set diskNames to every disk -- gets the list
  set diskCount to count disks -- this is important for list 'triage'
  if diskCount = 0 then -- if an empty list
    beep
  else if diskCount = 1 then -- if one item to eject
    tell application "Finder" to eject (item 1 of diskNames) -- gets the first - and only - item of diskNames; Sys Events cannot eject disks; I could have added a line before
  else if diskCount > 1 then -- if 2 or more items to eject
    set disksToEject to choose from list diskNames with prompt "Select a disk to eject:" OK button name "Eject" with multiple selections allowed
    if disksToEject is not false then -- if you didn't cancel; this avoids the system and you confusion
      tell application "Finder"
        repeat with theDisk in disksToEject -- every disk you chose
          eject theDisk -- duh!
          -- a whole lot of "end blocks"
        end repeat
      end tell
    end if
  end if
end tell
(* Ejector by KOMPILEsoft *)
[robg adds: I tried testing this one, but I can't get it to work on my Mac. It works fine on the author's Mac, though, so I'm publishing it under the assumption it's something about my machine. Please post your experiences.]
    •    
  • Currently 1.38 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (8 votes cast)
 
[2,629 views]  

Eject nearly any disk via AppleScript | 1 comments | Create New Account
Click here to return to the 'Eject nearly any disk via AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Eject nearly any disk via AppleScript
Authored by: renaultssoftware on Apr 04, '10 04:37:05PM
I know. Something's wrong with it. So I'll add the new code below.
on run
	tell application "System Events"
		set diskNames to the name of every disk whose ejectable is true
		set diskCount to count diskNames
		if diskCount = 0 then
			beep
		else if diskCount = 1 then
			try
				if item 1 of diskNames does not contain "Time Machine" then
					tell application "Finder"
						eject item 1 of diskNames
					end tell
				end if
			on error
				beep
			end try
		else if diskCount ≥ 2 then
			set newDiskNames to choose from list diskNames with prompt "Choose disks to eject:" with title "Eject Media" OK button name "Eject" with multiple selections allowed
			set newDiskCount to count newDiskNames
			if newDiskCount = 0 then
				beep
			else if newDiskCount = 1 then
				try
					if item 1 of newDiskNames does not contain "Time Machine" then
						tell application "Finder"
							eject item 1 of newDiskNames
						end tell
					end if
				on error
					beep
				end try
			else if newDiskCount ≥ 2 then
				repeat with thisDisk in newDiskNames
					try
						if thisDisk does not contain "Time machine" then
							tell application "Finder"
								eject thisDisk
							end tell
						end if
					on error
						beep
					end try
				end repeat
			end if
		end if
	end tell
end run
on open theDisk
	repeat with thisDisk in theDisk
		try
			tell application "Finder"
				eject thisDisk
			end tell
		on error
			beep
		end try
	end repeat
end open


[ Reply to This | # ]