5

I am trying to write an AppleScript that will check if a network drive (in this case, my Time Capsule) is mounted and, if not, mount it. I've figured out how to mount the Time Capsule, but I am at a loss over how to have the script check whether it is mounted first and just exit if it is, or mount it if not.

tell application "Finder"
mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"

end tell

Fbx_Steve
  • 71
  • 1
  • 1
  • 5
  • I'm not at my Mac, but volumes get mounted at `/Volumes` so something along the lines of `do shell script "ls /Volumes" ` should get you started. The command `mount` in the Terminal shows a list of mounted volumes too. So try mounting your Time Capsule and unmounting it and seeing the difference. – Mark Setchell Mar 29 '15 at 18:33

3 Answers3

8

Does that the job?

set mountedDiskName to "AirPort Time Capsule"
set diskIsMounted to false

tell application "System Events" to set diskNames to name of every disk
if mountedDiskName is in diskNames then
    set diskIsMounted to true
end if

if diskIsMounted then

    log "Disk Found, unmounting now..."
    do shell script "diskutil unmountDisk" & space & quoted form of mountedDiskName

else

    log "Disk Not Found, mounting now…"
    mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"

end if


Update I thought you want to unmount the disk when mounted but that was wrong :) here a shorter version:

tell application "System Events" to set diskNames to name of every disk
if "AirPort Time Capsule" is in diskNames then
    display dialog "Disk already mounted" buttons {"OK"} default button 1 with icon 1
else
    mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end if

…or, if you want the disk name in the dialog:

set diskName to "AirPort Time Capsule"
tell application "System Events" to set diskNames to name of every disk
if diskName is in diskNames then
    display dialog quoted form of diskName & " is already mounted." & return buttons {"OK"} default button 1
else
    mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
end if
dj bazzie wazzie
  • 3,472
  • 18
  • 23
  • That worked-thanks. I changed it up just a bit, though. After "if diskIsMounted then" I deleted your two lines and replaced them with: display dialog "Disk already mounted" – Fbx_Steve Mar 29 '15 at 22:04
  • Nice, thanks for the reply. Ah yes, I misread your question and assumed you wanted it to unmount when mounted. –  Mar 29 '15 at 22:39
3

You don’t have to check through a list of all disks. You can just ask if the disk you want exists.

tell application "Finder"
    if not (disk "Airport Time Capsule" exists) then
        mount volume "afp://AirPort%20Time%20Capsule._afpovertcp._tcp.local"
    end if
end tell
Simon White
  • 686
  • 5
  • 9
  • Where does this mount it? I need it somewhere Finder can access it, so I can tell iTunes where the location of the library is. – user717452 Aug 01 '16 at 20:30
1

It seems it doesn't matter whether a share is already mounted or not when using the mount command on Finder with apple script. Just mount it anyway, Finder handles this correctly (it doesn't mount it a second time nor does it complain loudly).

Adrian Zaugg
  • 318
  • 3
  • 10