|
| 1 | +use AppleScript version "2.4" -- Yosemite (10.10) or later |
| 2 | +use scripting additions |
| 3 | + |
| 4 | +-- CONFIGURATION |
| 5 | + |
| 6 | +-- The title to show when all ejectable disks ejected successfully |
| 7 | +set safeMessage to "Safe to disconnect" |
| 8 | + |
| 9 | +-- The title to show when one or more ejectable disks did not eject |
| 10 | +set failMessage to "Some disks did not eject" |
| 11 | + |
| 12 | +-- Show drives that were skipped (likely not ejectable, or in the skip list) |
| 13 | +set showSkipped to false |
| 14 | + |
| 15 | +-- A list of names of drives that shouldn't be ejected by this script |
| 16 | +-- |
| 17 | +-- Examples: |
| 18 | +-- set drivesToSkip to {"External file storage"} |
| 19 | +-- set drivesToSkip to {"Some network location", "CD Drive"} |
| 20 | +set drivesToSkip to {} |
| 21 | + |
| 22 | +-- // END CONFIGURATION |
| 23 | + |
| 24 | +tell application "Finder" |
| 25 | + -- APPLICATION ACTIONS |
| 26 | + |
| 27 | + -- Working lists |
| 28 | + set ejected to {} |
| 29 | + set notEjected to {} |
| 30 | + set skipped to {} |
| 31 | + |
| 32 | + -- Get every disk, and convert to a list (so it doesn't change while we're working) |
| 33 | + set allDisks to every disk as list |
| 34 | + |
| 35 | + repeat with currentDisk in allDisks |
| 36 | + set currentDiskName to name of currentDisk as string |
| 37 | + |
| 38 | + if currentDisk is ejectable and not (drivesToSkip contains currentDiskName) then |
| 39 | + eject currentDisk |
| 40 | + |
| 41 | + if currentDisk exists then |
| 42 | + -- Tried to eject, but disk still exists |
| 43 | + copy currentDiskName to end of notEjected |
| 44 | + else |
| 45 | + -- Eject succeeded |
| 46 | + copy currentDiskName to end of ejected |
| 47 | + end if |
| 48 | + else |
| 49 | + -- Either not ejectable, or in the skip list |
| 50 | + copy currentDiskName to end of skipped |
| 51 | + end if |
| 52 | + end repeat |
| 53 | + |
| 54 | + -- // END APPLICATION ACTIONS |
| 55 | + |
| 56 | + -- REPORTING |
| 57 | + |
| 58 | + -- Convert the working lists to comma-delimited strings |
| 59 | + set saveTID to AppleScript's text item delimiters |
| 60 | + set AppleScript's text item delimiters to {", "} |
| 61 | + set ejected to ejected as string |
| 62 | + set notEjected to notEjected as string |
| 63 | + set skipped to skipped as string |
| 64 | + set AppleScript's text item delimiters to saveTID |
| 65 | + |
| 66 | + -- Piece together the alert |
| 67 | + set alertMessage to "" |
| 68 | + |
| 69 | + -- First item: show disks that failed to eject (if any) |
| 70 | + if length of notEjected is greater than 0 then |
| 71 | + set alertMessage to alertMessage & "Could not eject " & notEjected |
| 72 | + end if |
| 73 | + |
| 74 | + -- Second item: show disks that ejected (if any) |
| 75 | + if length of ejected is greater than 0 then |
| 76 | + if length of alertMessage is greater than 0 then |
| 77 | + -- Add a couple of newlines if a previous message exists |
| 78 | + set alertMessage to alertMessage & " |
| 79 | +
|
| 80 | +" |
| 81 | + end if |
| 82 | + set alertMessage to alertMessage & "Ejected " & ejected |
| 83 | + end if |
| 84 | + |
| 85 | + -- Third item: show skipped disks (if any, and showSkipped enabled) |
| 86 | + if showSkipped is true and length of skipped is greater than 0 then |
| 87 | + if length of alertMessage is greater than 0 then |
| 88 | + -- Add a couple of newlines if a previous message exists |
| 89 | + set alertMessage to alertMessage & " |
| 90 | +
|
| 91 | +" |
| 92 | + end if |
| 93 | + set alertMessage to alertMessage & "Skipped " & skipped |
| 94 | + end if |
| 95 | + |
| 96 | + -- Change the title based on whether notEjected is populated |
| 97 | + if length of notEjected is 0 then |
| 98 | + set title to safeMessage |
| 99 | + else |
| 100 | + set title to failMessage |
| 101 | + end if |
| 102 | + |
| 103 | + -- Activate, to ensure that the dialog shows in the foreground |
| 104 | + activate |
| 105 | + |
| 106 | + -- And we're done. Show the alert. |
| 107 | + display alert title message alertMessage |
| 108 | + |
| 109 | + -- // END REPORTING |
| 110 | +end tell |
0 commit comments