speedyrazor
speedyrazor

Reputation: 3205

Mounting SMB network share on desktop

I am trying to mount a smb network share onto the desktop via python, I don't want the share to be mounted in a folder, but were all the other mounted shares are (if I use 'connect to Server' in OSX I want my python mount to be mounted in the same location). Here is the current python code:

directory = os.path.expanduser('~/Desktop')
directory = os.path.normpath(directory)
os.system("mount_smbfs //server/servershare " + directory)

When I run the above, something strange happens. In finder, my home, which has the icon of a house and my username changes to the mount name, it screws it up a bit.

Upvotes: 3

Views: 9724

Answers (2)

Dadr
Dadr

Reputation: 21

If you had desired to also open up a Finder window on the desktop, then this is another solution and one I wound up using myself.

import os

os.system("open -g smb://Server/Share")
#perform a timeout loop checking for finished attachment
if os.path.exists("/Volumes/Share"):
  # I use a for loop around this to sleep a second and try 20 times.
  # it needs to handle waiting for a server to wake from sleep and then attach
#Do my important functions
os.system("umount /Volumes/Share")

The pros are:

  1. Uses the keychain credentials for the current user
  2. Will wake up a sleeping Mac to attach to the smb share
  3. Will automatically create/delete the attachment directory in /Volumes
  4. (for others) this can work equally well in a shell script

The cons are:

  1. It uses Finder, so will open a window with the location on your desktop. If you don't mess with the window, it will close on its own when you umount
  2. It returns before the network is attached, so you need code to check for the mounted volume, sleep, and eventually move forward or error out.
  3. Repeating 1: If you are trying to run in the background and someone else is at the console, then popping open a Finder window really rots. You can use -g to leave the window behind other on the desktop, but if you want to run hidden in the background then Ivan X's solution is preferred.

On the other hand, if you want a new Finder window then this works well.

Upvotes: 0

Ivan X
Ivan X

Reputation: 2195

If you want to do this this the kosher, Finder-like way, do it in AppleScript via shell via Python:

os.system("osascript -e 'mount volume \"smb://server/servershare\"'")

You don't need anything else -- there's no mount point. This is identical to choosing "Connect To Server", and the resulting volume will show up in /Volumes as expected.

If you need to specify a username and/or password, you can do so:

os.system("osascript -e 'mount volume \"smb://server/servershare\" \
as user name \"myUserName\" with password \"myPassword\"'")

If you want to do it your original way using mount_smbfs, I think you want directory to be a folder you create in /Volumes, e.g. /Volumes/mySmbVolume, though I've never tried to do it this way. As you have it written, you're replacing your actual Desktop folder with the volume you're mounting. You could, however, make a folder inside Desktop for and use that for directory, and it might work. However, I'd do it like I wrote it to be most standard with the usual Mac way of doing things.

Upvotes: 5

Related Questions