I’ve been up all night trying to create a script that will make/return a list of all mounted disks, verify that two specific disks are mounted then unmount them.
Script Editor will compile but after it creates the mounted disk list - only as a comment as seen in the replies widow - it skips the “if” and nested “tell” statements, and finishes with the end tell that closes the “tell application “System Event” statement without completing the “end run”.
I’ve tried many different ways but they either fail to compile or give the same result I’ve been getting with the version posted here. Suggestions on how to fix this are greatly appreciated.
on run
tell application "Finder"
activate
end tell
tell application "System Events"
get the name of every disk
--> Protion being skipped
if list contains {"PlexMedia", "For Edit"} then
tell application "Finder"
eject "PlexMedia"
eject "For Edit"
end tell
end if
-->end portion being skipped
end tell
end run
Script Editor Reply:
tell application “Finder”
activate
end tell
tell application “System Events”
get name of every disk
→ {“Maintosh HD”, “vm”, “home”, “PlexMedia”, “For Edit”}
end tell
Model: MBP mid 2012 13 inch
AppleScript: version 2.11 (208)
Browser: Safari 605.1.15
Operating System: macOS 10.15
list is a reserved word in AppleScript. It’s strongly discouraged to use it as variable name.
And your logic will unmount both disks only if the list – that’s the meaning of the reserved word – contains both items. If only one of the disks is currently mounted nothing will happen.
And you are going to eject a literal string. Put the keyword disk in front of the name
This is one possible solution
tell application "Finder"
set allDisks to name of every disk
set disksToUnmount to {"PlexMedia", "For Edit"}
repeat with aDisk in allDisks
if disksToUnmount contains contents of aDisk then
eject disk aDisk
end if
end repeat
end tell
or – as there are only two disks to unmount – this simpler version
tell application "Finder"
set allDisks to name of every disk
if allDisks contains "For Edit" then eject disk "For Edit"
if allDisks contains "PlexMedia" then eject disk "PlexMedia"
end tell
Or the minimalist version, if a disk is not mounted the thrown error will be ignored.
tell application "Finder"
try
eject disk "PlexMedia"
end try
try
eject disk "For Edit"
end try
end tell
Hi GreggInKC. Welcome to MacScripter.
Stefan’s answered your query. I just wanted to point out that MacScripter’s tags for posting AppleScript code are actually [applescript] and [/applescript]. There’s a button for them above the text window when you post. They produce a box with a clickable link to open the script in a reader’s default editor. I’ve edited the tags for you in your post.
Thank you Nigel. I wondered why it didn’t creat the box. Thanks to you Stefan for multiple options. I knew the answer was probably simple but it just was not coming to me. I will work with these today and post the finished working script when I get done.
i have code for 2 methods of eject - eject all and eject except… at one time, one of my computers had an sd card that lived in the side slot, so i never wanted to eject that.
these were built in probably catalina or around that time, and i have since moved on from that laptop, but i believe everything still works.
eject all:
tell application "Finder" to eject (every disk whose ejectable is true and local volume is true)
eject except:
my exceptions = jetdrive = the sd card that lived in the side. startup disk. home = what i call my user. net is net.
set exceptionsList to {"JetDrive", "startup disk", "home", "net"}
tell application "Finder"
set diskList to the disks
repeat with mountedDisk in diskList
if name of mountedDisk is not in exceptionsList then
eject mountedDisk
end if
end repeat
end tell