Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to make a script that ejects all connected external drives and does not move on until they are all disconnected.

The main problem I'm having is being able to set the variable vol_count to the result of the following script:

tell application "Terminal"
    cd /Volumes
    ls | wc -l
end tell

I also have not tested that script, but I assume that's how you run a terminal command from within an applescript.

Follow up question:

How would I then have it eject all but two disks (my hard drive is partitioned into two disks)?

on run {}

    tell application "Finder"
        set vol_count to do shell script "cd /Volumes; ls | wc -l"

        eject the disks

        repeat until vol_count is equal to 2
            set vol_count to do shell script "cd /Volumes; ls | wc -l"
        end repeat
    end tell
end run
share|improve this question
up vote 3 down vote accepted


set dontEject to {"Mac OS X", "Time Machine", "Media", "home", "net"}
set myDisks to list disks

repeat with aDisk in myDisks
    if aDisk is not in dontEject then tell application "Finder" to eject aDisk
end repeat
share|improve this answer
This is the best answer. – CajunLuke Jan 10 '13 at 19:08
Wow, thanks! That's perfect! – JShoe Jan 10 '13 at 19:51

You can do something like this:

set vol_count to do shell script "cd /Volumes; ls | wc -l"
share|improve this answer

That is not how you run a terminal command in AppleScript. Try something like

set myvar to do shell script "ls ~"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.