AppleScripting -Finder

From nswccWiki
Jump to: navigation, search

A simple AppleScript that automatically finds all mounted volumes (ejectable disk images), and then proceeds to eject them one by one. It will not force an unmount of any volume in use and will not unmount the startup volume (always represented the first icon on your desktop). This is not documentation of executable code or PseudoCode.

-- ---------------------------------------------------------------------------------
-- Example for Finder,  Eject all Ejectable Volumes, Ian W. Parker 2007
-- ---------------------------------------------------------------------------------
tell application "Finder"
	set myName to ""
	--get all mounted volumes information
	set all_disks to every disk -- enumerate names of mounted volumes as a list
	if (count of all_disks) is greater than 1 then -- we have more than startup disk
		repeat with next_disk in all_disks
			-- accumulate names of visible volumes (ie mounted volumes)
			set myName to myName & {name of next_disk}
			try --  to remove detachable media (may refuse if file in use)
				if next_disk is ejectable then eject next_disk -- eg. CD-ROM, USB
			on error
				--- error condition recovery code, or ignore
			end try
		end repeat
	end if
end tell
-- ------------------------------------------------------------------------------------------
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox