16

I am writing an Automator "script" that rsyncs media on my 10.6.3 MacBook Pro to my Ubuntu 10.10 HTPC. I can make Automator run my shell script for rsync commands, but I can't make Automator mount the three volumes on the HTPC (folders for music, videos, and pictures).

I automatically mount these volumes when I login (these computers connect via a wifi network), but sometimes the HTPC volumes get unmounted, so I'd like to remount by default.

Is there a way to mount the volumes in Automator? I am open to shell scripting, too. Thanks!

hairboat
  • 2,903
  • 18
  • 51
  • 72
  • Jason, can you explain what should I do on Mac to add this to 'login items', I mean:
    1. How to create this script?
    2. what should I do to run it on startup on my Mac?

    Thank you in advance!

    – sergio1974 Mar 01 '20 at 11:17

2 Answers2

9

I build automator workflows like this all the time. You only need two actions, and they're both Files & Folders actions.

1) Get Specified Servers. This will let you build a list of shares to connect to. If you can map it from Finder -> Go -> Connect to server, you can use this.

2) Connect to Servers. This will connect to any servers passed to it (either from get specified servers or from ask for servers).

Ben Wyatt
  • 2,044
  • +agreed: this is exactly how i do it in an rsync workflow I use for syncing an iTunes library. Make sure you use Eject Volumes if you don't want the network drive to stay connected after the workflow completes. – Robert S Ciaccio Oct 19 '10 at 18:55
  • 1
    Eject Volumes is also handy if you want a "reverse" workflow. I use the procedure I outlined above as part of a workflow I run when I get to work (open programs, connect to servers and even remote into a virtual machine). At the end of the day, I have another one that disconnects all my servers and quits all my programs. – Ben Wyatt Oct 19 '10 at 19:05
8

I use the following applescript to mount directories in conjunction with MarcoPolo so network shares are automatically mounted when I get to both my office and home.

You'll need to change USERNAME, PASSWORD, SERVER/SHARENAME and possibly smb:// depending on your server type.

tell application "Finder"
    try
        mount volume "smb://USERNAME:PASSWORD@SERVER/SHARENAME"
        delay 1
    end try
end tell

UPDATE: An option without MarcoPolo: You can ping the server first and only try to connect if you get a response. You can then add this script into your Login Items

(Let's say you are trying to connect to a server named "some_server")

-- (0) Check to see if there server exists by pinging it
set max_retry to 60
set k to 0
repeat while (do shell script "ping -c 1 some_server") contains "100% packet loss"
    delay 5
    set k to k + 1
    if k > max_retry then error "Server is not responding for predefined period." number 8000
end repeat

-- (1) It exists, mount the volume tell application "Finder" try mount volume "smb://USERNAME:PASSWORD@some_server/SHARENAME" delay 1 --Optional, was added due to slow network issues end try end tell

Jason
  • 1,819
  • Why the delay 1? and i seem to be able to mount drives without the line tell application "Finder". I'm just poking around because my volume-mounting automator app has been really slow, and a bit less reliable, since upgrading to Big Sur – Jacob Lee Feb 01 '21 at 19:30
  • 2
    Hard to think this answer is > 10 years old now. The delay 1 was there so the script would run, connect then run an additional (not included in above) copy script. The delay made sure the volume was copied before starting the script. That was on a then slow office network. – Jason Feb 02 '21 at 00:29
  • @Jason: Yes, amazing long-term consistency with Apple Script. I don't think this solution is useful in my case however as I need something that will work when I'm not logged in (Finder isn't available AIUI). – Seamus Jun 07 '22 at 00:24
  • @Seamus How about a simple shell script? This might work: https://superuser.com/questions/471306/mount-network-volume-from-script – Jason Jun 08 '22 at 12:09