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