Syntax error on mapping network drives

Discussion in 'Mac Programming' started by daz31, May 27, 2015.

  1. daz31, May 27, 2015
    Last edited by a moderator: May 27, 2015

    daz31 macrumors newbie

    Joined:
    May 27, 2015
    #1
    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
     

    Attached Files:

  2. Nermal Moderator

    Nermal

    Staff Member

    Joined:
    Dec 7, 2002
    Location:
    New Zealand
    #2
    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"
    
     
  3. cbott, May 28, 2015
    Last edited: May 28, 2015

    cbott macrumors newbie

    Joined:
    Jan 26, 2012
    Location:
    Michigan
    #3
    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
     

Share This Page