How to Keep a Network Drive Mounted on Mac OS X with DLI_ReMounter JavaScript

How to Keep a Network Drive Mounted on Mac OS X

How to Keep a Network Drive Mounted on Mac OS X using a JavaScript Apple Script Editor Application

In today’s blog post I’ll show you how to keep a network drive mounted on Mac OS X using a simple custom JavaScript script in Apple’s Script Editor. I often perform network rendering using our DreamLight Constellation network render controller for LightWave 3D’s ScreamerNet. When network rendering the remote render nodes need to maintain constant access to the shared network drive. They need access to the network volume to read and write command files, scene files, scene assets and final renders. Mac OS X network performance has usually been rock solid. I’ve encountered a problem recently though in Mac OS X 10.11.6 El Capitan. Sometimes the remote render nodes will drop the connection to the shared network disk volume. When this happens the the render node fails to save the currently rendering frame and then drops out of the render farm. I then have to manually remount the shared network drive and reinitialize the remote render node. On a recent project this has become a real pain and was slowing down the overall progress.


How to Keep a Network Drive Mounted on Mac OS X with Matching File Sharing and Router Server Names

In addition to the volume connection often being dropped, re-connections often failed if I just double-clicked on the server’s name in the Finder’s sidebar. I then had to use Go->Connect to Server and enter the server’s IP address. I noticed was that there were two different names for the server I was attempting to connect to. One was set in the File Sharing preferences panel in the Computer Name text field. That setting determined the main server name that showed up in the Finder’s sidebar as expected. But there was another name listed below File Sharing: On that didn’t match. I couldn’t find a matching setting anywhere on the Mac.

How to Keep a Network Drive Mounted on Mac OS X using a JavaScript Apple Script Editor Application

It turns out the second name was coming from the network router which had nicknames set for each connected device. So I set the server’s device name on the router to match the server’s name as set in File Sharing.

How to Keep a Network Drive Mounted on Mac OS X using a JavaScript Apple Script Editor Application

After I set both names to match the Mac would still periodically drop the connection to the network volume. However, when the names matched, I was able to use a simple double-click on the server’s name in the Finder sidebar to reconnect.

Additional research into how to keep a network drive mounted on Mac OS X showed others having the same problem. At that point I figured it might be easier to just write a simple script to remount the drive whenever it dismounted. So I wrote the following JavaScript script DLI_ReMounter in Apple’s Script Editor to detect when the volume was disconnected and just remounted it. Once I wrote this JavaScript I haven’t had another disconnected render node in my Mac based DreamLight Constellation render farm.


How to Keep a Network Drive Mounted on Mac OS X with DLI_ReMounter JavaScript

How to Keep a Network Drive Mounted on Mac OS X - DLI_ReMounter JavaScript

In order to use this script first mount the shared network drive volume that you wish to keep mounted on the remote rendering Mac. Be sure to use unique names for any shared disk volumes. Mac OS X can handle duplicate names, but it adds index numbers internally which may cause issues. So to eliminate that as a potential problem it’s best to rename with unique names any disks that you wish to share on the network.

Mounting the Shared Network Disk Volume

On the remote rendering Mac (A MacBook Pro in this case) navigate to the Device’s top level in the Finder (4x2GHz_MacBookPro). Make a note of the disk volume name (WORK 3TBR1) and the shared server name (12x3GHz_MacPro) as seen in the Finder.

  • Copy and paste the DLI_ReMounter JavaScript (below) into Apple’s Script Editor set to use JavaScript
  • Edit the user variables volName (WORK 3TBR1) and the serverName (12x3GHz_MacPro) to match your volume and server names
  • Edit tIdleSeconds to the number of seconds between each time it checks for a disconnect to remount the drive.
  • Export with File Format set to Application with Stay open after run handler checked.

How to Keep a Network Drive Mounted on Mac OS X using a JavaScript Apple Script Editor Application

  • Double-click the exported application to run it.
  • Eject the shared volume or dismount the shared sever.
  • The running DLI_ReMounter JavaScript application should then remount the volume and may ask you to enter your username and password. Do so and be sure to click Remember this password in my keychain. This way you won’t need to re-enter this information whenever the script attempts to remount the shared disk volume.

How to Keep a Network Drive Mounted on Mac OS X using a JavaScript Apple Script Editor Application


DLI_ReMounter JavaScript Text for Copy/Paste

The following JavaScript is provided free for educational purposes to use AT YOUR OWN RISK. Be sure to back up all systems and data before use. Michael Scaramozzino and DreamLight Incorporated make no warranties whatsover regarding this script. For educational purposes only. USE AT YOUR OWN RISK!

// ***********************************************
// DLI_ReMounter JavaScript for Mac OS X
// by Michael Scaramozzino - DreamLight.com
// ***********************************************
// Michael Scaramozzino and DreamLight Incorporated 
// make no warranties whatsoever regarding this script. 
// For educational purposes only. USE AT YOUR OWN RISK!
// ***********************************************
// USAGE:

// Open in Apple's Script Editor
// Edit the volName and serverName USER VARIABLES 
// Export As application
// With 'Stay open after Run handler' checked.
// Double-click to launch application.

// ***********************************************
// USER VARIABLES

var tIdleSeconds = 30; 					// set to seconds to wait between checks
var volName = "MikeS_MBP_SYS" 				// set volume name to mount
var serverName = "4x22GHz_MacBookPro" 			// set server name

// ***********************************************
// APPLICATION HANDLERS

var app = Application.currentApplication();		// Link to current application
app.includeStandardAdditions = true;			// Activate StandardAdditions
var system = Application('System Events');		// Load System Events


function idle() { 					// Called at designated idle intervals
	checkVolumes();		
    return tIdleSeconds; 				// next idle interval call
};

// ***********************************************
// UTILITY HANDLERS

function checkVolumes() {
	var volMounted = false; 			// start with mount flag false

	var volList = system.disks(); 			// get list of mounted volumes from system
	for (i = 0; i < volList.length; i++) { 		// check each mounted volume
		if (volList[i].name() == volName){ 	// volume name matches
			volMounted = true; 		// volume is mounted
			break;				// end loop
		};
	};
	
	if ( ! volMounted ) { 				// if not mounted, try to mount it
		try {
			app.displayNotification( "Mounting: '" + volName + "'" );
			app.mountVolume( volName, {onServer: serverName} );
		} catch (err) {
			app.displayNotification( 'Mount failed: ' + err );
		};
	};
};

If you enjoyed this tutorial please consider making a donation to fund more!



DreamLight Constellation Network Render Controller

DreamLight Constellation 3D Icon LogoDreamLight® Constellation is our cross-platform network render controller that combined with DLI_SNUB-Launcher™ makes configuring, launching and controlling LightWave 3D ScreamerNet LWSN for standalone, batch, network and internet rendering, drag-and-drop-dead-easy™.

Created by Michael Scaramozzino - LightWave 3D Artist Profile
Author of Creating a 3D Animated CGI Short & Mastering LightWave ScreamerNet


DreamLight wrote the book: Creating a 3D Animated CGI Short. See what we can do for you.

 

11 Responses

  1. Mat P
    | Reply

    Thanks for the script. I too needed to keep multiple volumes alive and I modified the script like this:

    // ***********************************************
    // DLI_ReMounter JavaScript for Mac OS X
    // by Michael Scaramozzino – DreamLight.com
    // ***********************************************
    // Michael Scaramozzino and DreamLight Incorporated
    // make no warranties whatsoever regarding this script.
    // For educational purposes only. USE AT YOUR OWN RISK!
    // ***********************************************
    // USAGE:

    // Open in Apple’s Script Editor
    // Edit the volName and serverName USER VARIABLES
    // Export As application
    // With ‘Stay open after Run handler’ checked.
    // Double-click to launch application.

    // ***********************************************
    // USER VARIABLES

    var tIdleSeconds = 30; // set to seconds to wait between checks
    var vol1Name = “Movies” // set volume name to mount
    var vol2Name = “Photos” // set volume name to mount
    var vol3Name = “Books” // set volume name to mount
    var vol4Name = “iTunes” // set volume name to mount
    var serverName = “DiskStation” // set server name

    // ***********************************************
    // APPLICATION HANDLERS

    var app = Application.currentApplication(); // Link to current application
    app.includeStandardAdditions = true; // Activate StandardAdditions
    var system = Application(‘System Events’); // Load System Events

    function idle() { // Called at designated idle intervals
    checkVolumes(vol1Name);
    checkVolumes(vol2Name);
    checkVolumes(vol3Name);
    checkVolumes(vol4Name);
    return tIdleSeconds; // next idle interval call
    };

    // ***********************************************
    // UTILITY HANDLERS

    function checkVolumes(volName) {
    var volMounted = false; // start with mount flag false

    var volList = system.disks(); // get list of mounted volumes from system
    for (i = 0; i < volList.length; i++) { // check each mounted volume
    if (volList[i].name() == volName){ // volume name matches
    volMounted = true; // volume is mounted
    break; // end loop
    };
    };

    if ( ! volMounted ) { // if not mounted, try to mount it
    try {
    app.displayNotification( "Mounting: '" + volName + "'" );
    app.mountVolume( volName, {onServer: serverName} );
    } catch (err) {
    app.displayNotification( 'Mount failed: ' + err );
    };
    };
    };

    • Thanks Matt!
      I’m glad the script was useful!
      Your modifications look OK on a quick perusal.
      I haven’t run it myself, so for others who want to use it YMMV.
      The indentation got lost when the comment was posted…

  2. Dustin
    | Reply

    Thanks for this. I have some scripts that work with my Synology NAS and I kept having to reconnect. This works great. I did modify the script a bit to loop through a list of drives.

    • Great thanks, I’m glad it was useful Dustin! If you wouldn’t mind sharing the changes you made feel free to post them. A few others were interested in revising it for multiple drives and might find that useful as well. Thanks!

  3. Andrew
    | Reply

    Thank you for sharing this script. It is quite helpful. I do have a question for you if you don’t mind. I have 3 drives on my media server that I like to keep connected and am looking for the best way to do that. Is there an easy modification to this script or should I just make 1 separate script/app per drive?

    Thanks!

    • Thanks Andrew! I’m glad it was useful.
      If you’re not familiar with coding you could just use three versions of the script, each edited/named separately.

      Otherwise you could edit the script to use three different volNames such as volName1, volName2 and volName3 at the top. Then edit the code in the loop to check for all three of those rather than just checking one.

      • Morty
        |

        Thank you for the script, this is exactly what I’m looking for.
        I’m also interested in having only one script for multiple drives, but I’m not that familiar with coding. Can you give us example how the script should be with 2 drives?
        Also, are there any way to have this running as an application that are not showing in the dock?

      • Michael Scaramozzino
        |

        Hi Morty,

        You should just be able to use two different copies of the script with each set for a different drive. If I have some extra time I’ll take a look but that should work for now.

        Thanks,
        -MikeS

  4. Jim Myers
    | Reply

    I’ll add that and report back the results. THX

  5. Jim Myers
    | Reply

    Michael – I keep getting an Error on line 45 – Can’t get object (-1728)
    This happens after several hours of running. I have only changed the server and mount name in your script, nothing else has been changed.
    Great script, does exactly what i need, just need to figure out the error msg.

    • Hi Jim,

      I’m glad you find the script useful. That’s strange that you’re getting an error after a few hours. I haven’t seen that here.

      Line 45 is this:
      if (volList[i].name() == volName){

      So it looks like volList[i] may become undefined for some reason?
      you could try adding something like this before that line:
      app.displayNotification( “DEBUG: ‘” + volList[i] );
      to see what’s in there at that point.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.