Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

daz31

macrumors newbie
Original poster
May 27, 2015
1
0
Hi,

I'm a beginner at applescripting and I'm trying to get a script to run at my work to map network drives to the mac. Each user has their own individual login. I'm currently getting syntax errors all over the place.

thanks for the help in advance.

Code:
-- Collect Username
set prompt to "Please enter your username:"
set dialogResult to display dialog prompt buttons {"Cancel", "OK"} default button 2 default answer "" with icon 1
set theUsername to text returned of dialogResult 
 
-- Collect Password
set prompt to "Please enter password:"
set dialogResult to display dialog prompt buttons {"Cancel", "OK"} default button 2 default answer "" with icon 1 with hidden answer
set thePassword to text returned of dialogResult

-- Define shares
set mapdrive1 to "smb://example\" & theUsername & ":" & thePassword & "@server.example.com/share""

-- Mount Volumes
mount volume mapdrive1
 

Attachments

  • unnamed.jpg
    unnamed.jpg
    88.9 KB · Views: 186
Last edited by a moderator:
I'm not familiar with AppleScript, but the problem could be \" - at least in C this means "put a double quote into the string".

mapdrive1 is therefore being set to smb://example"theUsername and the following : is confusing the parser.

Try changing the line to:

Code:
set mapdrive1 to "smb://example\\" & theUsername & ":" & thePassword & "@server.example.com/share"
 
There's no need to worry about collecting a username and/or password. When you try to mount the drive Finder will prompt for it if you don't have credentials saved to the Keychain.

Try the following:

Code:
-- Mount I: drive
tell application "Finder"
	mount volume "smb://example.com/public/IntraDeptDocs"
end tell

I also found it useful to ping the server before mounting the drive to avoid error messages when away from the office or not connected to the VPN.

Try using this code at the top of your AppleScript:

Code:
--Check to see if you are on the network
set err to true
set tries to 0
repeat while err is true
	try
		do shell script "/sbin/ping -o -c 1 server.example.com"
		set err to false
	on error
		set tries to tries + 1
		if (tries = 5) then
			display dialog "You are not connected to the corporate network. Please connect to [SSID name], a network cable or the VPN."
			return
		end if
		delay 2
	end try
end repeat

Hope that helps.

Chris
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.