Jump to content

[Solved] Mount / Unmount a shared network drive


MEs

Recommended Posts

Hi,

 

After using a few workflow downloaded here and on Packal (Google Translate, OS X Toolbox, Call or SMS contact for example), I have been creating some workflow of my own like a file filter and now to launch my apps, files and drives when I get to work.

 

MOUNTING PART

 

This part works, including a Run NSAppleScript to mount 2 volumes. Here it is :

tell application "Finder"
    mount volume "smb://name-pc/Folder A"
    mount volume "smb://name-pc/FolderB"
end tell

As you can see, the first shared folder has a space in its name. But for mounting, it didn't seem to be a problem to mount them. But, I am always getting the pop-up window asking for username & password. Both of them are already written (and the "Remember in Keychain"is selected..) so all I have to do is click OK.

 

I was wondering if there is a way to avoid this pop-up ? I for example used a simple shortcut prior to using Alfred and when clicking it, I didn't get the pop-up, which sounds logic since it is stored in my keychain.

 

If not, how can I make Alfred to click OK for me ?

 

UNMOUNTING PART

 

Unfortunately, unmounting is not working by juste changing mount to unmount. So I found another way as written here :

do shell script "umount /Volumes/Folder A"
do shell script "umount /Volumes/FolderB"

But Folder A is not unmounted...

 

I even found an existing workflow in this board (here) but the unmount function of is also not capable of un mounting this particular folder. But it shows up when I type "unmount" keyword as "Folder%20A"

 

So I tried those commands in my NSAppleScript :

do shell script "umount /Volumes/Folder%20A/"

do shell script "umount /Volumes/Folder\ A/"

None of these made it unmount.

 

But this one works in my mac Terminal :

umount /Volumes/Folder\ A

So I tried to add a Terminal Command action, but this one opens up the terminal when done.

 

Is there a way to do it without getting the terminal app to actually launch ?

If not, any idea on the correct syntax for the AppleScript command here ?

 

Regards

Link to post

Yes. Just use a Run Script Action with Language = /bin/bash

 

It's generally a better idea to also use Run Script Actions to run AppleScript, too, by setting Language = /usr/bin/osascript (AS)

 

The reason is that Run NSAppleScript runs the script on Alfred's main thread, which blocks Alfred till the script finishes. The normal Run Script action runs in the background, so you can keep using Alfred.

 

However, because AppleScript is weird, sometimes one will work but the other won't.

Link to post

Hi,

 

Thank you for your help. It works perfectly using /bin/bash indeed.

 

On the mounting part, any idea how I can automatically say Connect for the credential pop-up to mount the shared folders ? Since Alfred 3.1 is out, I thought Key Combo would do the job, but it isn't. I placed the key combo "Return" after the NSAppelScript that mount the shared volume. Unfortunately, it is like it doesn't type Enter. Maybe I haven't understood the Key Combo use properly.

 

Is there any other way to simulate an "Enter" keystroke or a click on the Connect button ?

I guessed here using NSAppeScript is a better option, so the workflow will wait for the click or keystroke to be done before the other actions in the workflow and therefore won't loose the focus on the pop-up window. Correct me if this is not the best use.

 

FYI I tried these so far in my AppleScript, none worked out :

tell application "System Events"
	click button "Connect"
end tell


tell application "Finder"
	click button "Connect"
end tell

tell application "Finder"
	keystroke return
end tell

Regards

Link to post

I tried to add it in the same NSAppleScript but it didn't work

 

It kinda worked by using an open /bin/bash command to mount the shared volumes & a /usr/bin/osascript for the code you mentioned.

BIN/BASH

open  "smb://nef01-pc/ScanNEF01"


OSASCRIPT

tell application "System Events" to key code 36 

To have them both mounted, I have to put the first Script mounting a drive followed by the osascript to tape Return, then the Script to mount the 2nd volume and again the script for key code 36.

Sometimes it doesn't work, depends on how long it takes for the Pop Up to show up. Adding a delay in between didn't help.

 

The downside is that I used the "open" bash function, which actually opens finder windows of each volumes mounted. All considered, not a much better result here.

 

 

And I couldn't find any other command that could mount the volume using /bin/bash Language.

 

 

I will keep trying until I figure something that works actually every time :)

Link to post

Hi,

 

Thank you for the link. In fact I already stumbled on it while looking for some answers, since there are not that many messages on forums speaking of this specific idea to use terminal command in this matter after 2013. Not sure why...

 

So using the mount_smbfs function wasn't a solution by itself, since I didn't have the right to write on the /Volumes mountpoint. I finally tried to change the chmod of /Volumes to 777 even if it's not a perfect solution. Do it at your own risk if you need to.

 

My final /bin/bash has been as following :

mkdir /Volumes/Folder\ A
mount_smbfs -N //user:password@server/Folder%20A /Volumes/Folder%20A

To add some explanation if someone wants to use it, the -N option is used to avoid the Connect op-up window.

The mkdir function will create a folder, that will be replaced by the volumes as soon as it's mounted. When unmounted, the folder will not be there anymore. Which is why you need to keep the mkdir line.

If the shared folder you need to mount has a space in it's name, you will need to add a \ before the space in the mkdir function and replace by %20 in the mount_smbfs function.

 

If you have multiple folder to add, juste repeat those 2 lines, replacing the informations as needed.

 

This is the only way I could come up with so far to have a real workflow working, because after this script, I added a few applications to launch, 2 files as well and a script to hide the app after they are opened, and a few tabs to open in my work navigator.

 

By the way, the code to hide an app as a NSAppleScript :

tell application "System Events" to tell process "Microsoft Excel" to set visible to false

Thanks again for your help deanishe  :)

Link to post

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...